Dynamic memory management and dynamic memory functions of C language


1. Why is there dynamic memory allocation?

Before learning dynamic memory management methods, we need to discuss a question first, why do we need dynamic memory allocation?
First, observe the following code:

int i = 20;
char arr[10]={
    
    0};

It can be observed that the above code opens up 4 bytes and 10 bytes of space on the stack space respectively, but the above method of opening up space has two characteristics:

  1. The size of the space is fixed.
  2. When an array is declared, the length of the array must be specified, and the memory it needs is allocated at compile time.
    But usually we only know the need for space when the program is running, so the way to open up space when the array is compiled cannot be satisfied. At this time, dynamic memory development is needed.

Second, the introduction of dynamic memory functions

1. malloc and free

C language provides a dynamic memory development function—>malloc function:

void* malloc(size_t size);

This function applies for a contiguous space from the memory and returns a pointer (and the address of the first element) of this space only.
#Also note that this space is opened up in the heap area. Unlike the stack area, the heap area will only be released after it is actively released or the program ends normally; and the local variables defined in the stack area are at the end of the function call. It will be automatically destroyed afterwards.

Next, try the malloc function:

char* ptr = (char*)malloc(1024*1024*1024);
printf("%p\n", ptr);
free(ptr);
return 0;

Put a breakpoint at free (ptr) and observe the system memory. You can see that the program in Figure 12-9 has opened up 1G of space and successfully output the address.
Insert picture description here
Insert picture description here
After removing the breakpoint, continue to run the program and see that the 1G memory has been released.
Insert picture description here
For the malloc function:

  • If the development is successful, a pointer to the opened space is returned.
  • If the development fails, a NULL pointer is returned, so the return value of malloc must be checked.
  • The type of the return value is void*, so the malloc function does not know the type of space opened up, and the user decides when it is used.
  • If the parameter size is 0, the behavior of malloc is undefined by the standard and depends on the compiler.

The C language provides another function free, which is specially used for the release and recovery of dynamic memory. The function prototype is as follows:

void free(void* ptr);

The free function is used to release dynamically allocated memory.

  • If the space pointed to by the parameter ptr is not dynamically opened up, the behavior of the free function is undefined.
  • If the parameter ptr is a NULL pointer, the function does nothing.
    Both malloc and free are declared in the stdlib.h header file.

2.calloc function

C language also provides a function called calloc, which is also used for dynamic memory allocation. The prototype is as follows:

void* calloc(size_t num,size_t size);
  • The function of the function is to open a space for num elements of size size, and initialize each byte of the space to 0;
  • The only difference with the function malloc is that calloc will initialize each byte of the requested space to 0 before returning the address;

3.realloc function

  • The emergence of the realloc function makes dynamic memory management more flexible.
  • Sometimes we find that the space applied for in the past is too small, and sometimes we feel that the space applied for is too large. In order to use the memory reasonably, we need to adjust the size of the memory flexibly. The realloc function can adjust the size of the dynamically opened memory. The function prototype is as follows:
void* realloc(void* ptr,size_t size)
  • ptr is the memory address to be adjusted.
  • The new size after size adjustment.
  • The return value is the adjusted memory starting position.
  • On the basis of adjusting the size of the original memory space, this function will also move the data in the original memory to the new space.
  • There are two situations when realloc adjusts the memory space:

Situation 1: There is enough space after the original space.
Situation 2: There is not enough space after the original space.
In case 1, to expand the memory, add space directly after the original memory, and the data in the original space will not change.
In case 2, when there is not enough space after the original space, the method of expansion is to find another contiguous space of suitable size on the heap space to use. This function returns a new memory address. (The actual process is: open up new space-copy the contents of the old space-release the old space), so for case 2, only need to release the new space.

Guess you like

Origin blog.csdn.net/weixin_47460769/article/details/112800601