You haven't learned dynamic memory yet? Then learn it soon! ! !

insert image description here

1. Why does dynamic memory allocation exist

After we have determined which storage class we need to use, we will select the scope and storage period of the automatic variable according to the established memory management rules. But sometimes when we need to implement some more flexible operations, we need to use dynamic memory allocation.

2. Introduction to dynamic memory functions

2.1 malloc and free

The C language provides a dynamic memory allocation function:

void malloc(size_t size);

This function applies for a contiguous available space from memory and returns a pointer to this space

  • If the allocation is successful, return a pointer to the allocated space
  • If the opening 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 the opened space, and the user can decide by himself when using it.
  • What happens to malloc if the parameter size is 0 depends on the compiler

The amount of dynamically allocated memory will only increase, so we need to release it after we use it up, so C language provides another function free, which is dedicated to the release and recycling 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, the behavior of the free function is undefined
  • If the parameter ptr is a NULL pointer, the function does nothing

2.2 calloc

The 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 up a space for num elements of size size, and initialize each byte of the space to 0
  • The difference from the function malloc is that calloc will initialize each byte of the requested space to 0 before returning the address

2.3 realloc

The emergence of the realloc function makes dynamic memory management more flexible

Sometimes we find that the space we applied for in the past is too small, and sometimes we decide that the space we applied for is too large. In order to design the memory reasonably, we need to make flexible adjustments to the size of the memory, and the realloc function can do it Adjusting the size of dynamically allocated memory

The function prototype is as follows:

void* realloc(void* ptr,size_t size);
  • ptr is the memory address to adjust
  • 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:

Case 1: There is enough space after the original space

Case 2: There is not enough space after the original space

How to improve realloc in two situations when adjusting the memory space:

Case 1: When it is case 1, if you want to expand the memory, you can directly add space after the original memory, and the data in the original space will not change

Case 2:

In situation 2, when the original space cannot be put down, it will be expanded to use another continuous space of appropriate size on the heap space, so that the function returns a new memory address

3. Common dynamic memory errors

3.1 Dereference operation on NULL pointer

void test()
{
    
    
	int *p=(int*)malloc(INT_MAX/4);
	*p=20;//如果p的值是NULL,就会有问题
    free(p);
}

3.2 Out-of-bounds access to dynamically allocated spaces

void test()
{
    
    
	int i = 0;
	int* p = (int*)malloc(10 * sizeof(int));
	if (NULL == p)
	{
    
    
		exit(EXIT_FAILURE);
	}
	for (i = 0; i <= 10; i++)
	{
    
    
		*(p + i) = i;//当i是10的时候越界访问
	}
	free(p);
}

3.3 Use free to release non-dynamic memory

void test()
{
    
    
	int a=10;
	int *p=&a;
	free(p);
}

3.4 Use free to release part of a dynamically allocated memory

void test()
{
    
    
	int *p=(int *)malloc(100);
	p++;
	free(p);//p不再指向动态内存的起始位置
}

3.5 Multiple releases of the same dynamic memory

void test()
{
    
    
	int *p=(int *)malloc(100);
	free(p);
	free(p);//重复释放
}

3.6 Dynamically open up memory and forget to release it (memory leak)

void test()
{
    
    
    int *p=(int *)malloc(100);
    if(NULL!=p)
    {
    
    
        *p=20;
	}
}
int main()
{
    
    
    test();
    while(1);
}

Forgetting to release the dynamically opened space that is no longer used will cause memory leaks

Guess you like

Origin blog.csdn.net/weixin_51799303/article/details/128747617