C language memory allocation related knowledge

One, memory allocation
1, the type of memory allocation:

In C/C++, the memory is divided into five areas, namely the stack area, the heap area, the global/static storage area, the constant storage area, and the code area.

Static memory allocation: compile-time allocation. Including: global, static global, and static local variables.

Dynamic memory allocation: allocation at runtime. Including: stack (stack): local variables. Heap (heap): Variables used in C language are dynamically allocated in memory. (malloc or calloc, realloc, free function)

2. Memory allocation of variables:

Stack area (stack): refers to the storage area where variables are allocated by the compiler when needed and automatically cleared when not needed. For example, when a function is executed, the formal parameters of the function and local variables in the function are allocated in the stack area. After the execution ends, the formal parameters and local variables are unstacked (automatically released). The stack memory allocation operation is built into the instruction set of the processor, which is efficient but the allocated memory space is limited.

Heap area (heap): Refers to the storage area that is manually allocated and released by the programmer. If the programmer does not release this memory, the memory will always be occupied until the end of the program operation and will be automatically recovered by the system. In C language, malloc and free application are used and free up space.

Static storage area (static): The storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, and this space is released by the system after the program finishes running.

Constant storage area (const): The constant string is stored here, such as the "ABC" string is stored in the constant area, and the ones stored in the constant area are read-only and not writable. The global variables modified by const are also stored in the constant area, and the local variables modified by const are still on the stack.

Program code area: store the binary code of the source program.

Second, the comparison between the heap and the stack
Application method: the stack is managed by the compiler, and the allocation and release of the heap is managed by the programmer.

Application size: The stack is a data structure that grows toward lower addresses, and it is a continuous memory. The memory that can be obtained from the stack is small, and the size is determined during compilation; the heap is a data structure that grows toward higher addresses, and it is a discontinuous storage Space, memory acquisition is more flexible and larger.

Storage content in stack and heap:

Stack: When a function is called, the first thing that is pushed into the stack is the address of the last instruction in the main function, and then the parameters of the function. In most c compilers, the parameters are pushed into the stack from right to left. Then there are local variables in the function (static variables are not pushed into the stack). When the function call ends, the local variables are popped out of the stack first, then the parameters, and finally the pointer on the top of the stack points to the address where it was first stored, which is the main function. The next instruction in , the program starts to run from this point;

Heap: Generally, one byte is used to store the size of the heap at the head of the heap, and the specific content in the heap is arranged by the programmer.

Three, dynamic memory allocation
1.malloc function;

Function prototype: void * malloc (size_ t size) ;

Function:

1. Create a contiguous heap memory of size size.

2. size indicates the size (number of bytes) of memory allocated on the heap.

3. The return value of the function is a pointer, pointing to the first address of the memory just opened.

4. If memory allocation fails, a null pointer is returned, that is, the return value is NULL.

5. When the memory is no longer used, the free () function should be used to release the memory block

6. The header file <stdlib.h> or <malloc.h> must be included when using

2. calloc function;

Function prototype: void * calloc(size_ tn, size t size);

Function:

1. Allocate n continuous spaces with a length of si ze in the dynamic storage area of ​​the memory,

2. The function returns a pointer to the starting address of the allocation;

3. If the allocation is unsuccessful, return NULL.

4. When the memory is no longer used, the free () function should be used to release the memory block.

5. The header file <stdlib.h> or <malloc.h> must be included when using

3. realloc function;

Function prototype:

void * realloc(void * mem_ address, size_ t newsize) ;

Function:

1. Re-allocate new memory size (large or small) for variables with existing memory;

2. First judge whether the current pointer has enough continuous space, if so, expand the address pointed to by mem_address, and return mem_address;

3. If the space is not enough, first allocate space according to the size specified by newsize, copy the original data from the beginning to the end to the newly allocated memory area, and then release the memory area pointed to by the original mem_address (note: the original pointer is automatically released, no need to use free), while returning the first address of the newly allocated memory area. That is, the address of the memory block is reallocated.

4. If the reallocation is successful, return a pointer to the allocated memory;

5. If the allocation is unsuccessful, return NULL.

6. When the memory is no longer used, the free () function should be used to release the memory block

7. The header file <stdlib.h> or <malloc.h> must be included when using

4. free function.

Function prototype: void free (void *ptr) ; //ptr is the memory pointer to be released.

free(): Release the memory space of the pointer variable on the heap area, but cannot release the memory space on the stack. free should be used in pairs with malloc (calloc, realloc).

Notice:

If malloc (calloc, realloc) is more than free, it will cause a memory leak;

If malloc (calloc, realloc) is less than free, it will cause secondary deletion, destroy memory, and cause the program to crash.

Guess you like

Origin blog.csdn.net/qq_41290252/article/details/115734371