Dynamic memory management

Posted by zzlong on Tue, 25 Jan 2022 16:48:12 +0100

catalogue

1. Why does dynamic memory management exist

2. Introduction to dynamic memory function

2.1 malloc

2.2 calloc

 2.3 realloc

3 common errors in dynamic memory allocation i

3.1 dereference of NULL pointer

3.2 cross border access to dynamic development space

3.3 free release for non dynamic development memory

3.4} use free to release a part of dynamic memory

         3.5 release the same dynamic memory multiple times

3.6 dynamic memory forgetting release (memory leakage)

1. Why does dynamic memory management exist

The memory development method we already know is

int arr[100]..int a.

However, in the actual project, this memory management method is extremely inconvenient. For example, the address book made before has opened up a lot of space at once, but the elements inside are not actually used so much, and the memory will be wasted. If the memory is too small, it is certainly not enough. Here, c language provides dynamic memory.

2. Introduction to dynamic memory function

2.1 malloc

void* malloc ( size_t size );
This function requests a block of memory Continuous availability And returns a pointer to this space.
If the development is successful, it returns a pointer to the developed space.
If the development fails, a NULL Pointer, therefore malloc The return value of must be checked.
The type of the return value is void* , so malloc The function does not know the type of open space, which is used by the user himself
To decide.
If parameter size by 0 , malloc The behavior of the standard is undefined and depends on the compiler.
C Language provides another function free , which is specially used for dynamic memory release and recovery. The function prototype is as follows:
void free ( void* ptr );
free Function is used to free dynamic memory.
If parameter ptr The space pointed to is not dynamically opened up, so free The behavior of the function is undefined.
If parameter ptr yes NULL Pointer, the function does nothing.
malloc and free All declared in stdlib.h In the header file.
for instance
int main()
{
 //Code 1
 int num = 0;
 scanf("%d", &num);
 int arr[num] = {0};
 //Code 2
 int* ptr = NULL;
 ptr = (int*)malloc(num*sizeof(int));
 if(NULL != ptr)//Determine whether the ptr pointer is null
 {
 int i = 0;
 for(i=0; i<num; i++)
 {
 *(ptr+i) = 0;
 }
 }
 free(ptr);//Freeing the dynamic memory pointed to by ptr
 ptr = NULL;//Is it necessary?
 return 0; }

In the last step above, ptr=NULL is actually necessary, because after the ptr space is released, the ptr still points to that address, but the elements inside are not defined. That is, the hotel check-out problem.

2.2 calloc

Another memory development function is calloc

void* calloc ( size_t num , size_t size );
The function is to num Size is size The element opens up a space and initializes each byte of the space to 0 .
And function malloc The only difference is calloc Each byte of the requested space is initialized to full before returning the address 0 .
for instance:

 2.3 realloc

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. That's for a reasonable reason
When waiting for memory, we will flexibly adjust the size of memory. that realloc Function can be used to dynamically open up the memory size
Adjustment of.
The function prototype is as follows:
void* realloc ( void* ptr , size_t size );
ptr Is the memory address to be adjusted
size New size after adjustment
The return value is the adjusted memory starting position.
This function will also move the data in the original memory to a new location on the basis of adjusting the size of the original memory space new Space.
realloc There are two situations when adjusting memory space:
situation 1 : there is enough space behind the original space
situation 2 : there is not enough space after the original space
situation 1
When this is the case 1 To expand the memory, directly add space after the original memory, and the data in the original space will not change.
situation 2
When this is the case 2 When there is not enough space after the original space, the expansion method is to find another continuous space of appropriate size on the heap space. In this way, the function returns a new memory address.
Due to the above two situations, realloc You should pay attention to the use of functions.
int main()
{
	int *p=(int *)calloc(10,sizeof(int));
	if (p != NULL)
	{
		printf("Memory development succeeded\n");
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", p[i]);
	}
	//There is not enough memory and capacity needs to be increased. The capacity increase here is developed in the original calloc, and it is increasing to 20 shaping
	int* ptr = (int*)realloc(p, 80);
	if (ptr != NULL)
	{
		p = ptr;
	}
	for (i = 10; i < 20; i++)
	{
		p[i] = i;
	}
	for (i = 0; i < 20; i++)
	{
		printf("%d ", p[i]);
	}
	free(p);
	return 0;
}

3 common errors in dynamic memory allocation i

3.1 dereference of NULL pointer

void test()
{
 int *p = (int *)malloc(INT_MAX/4);
 *p = 20;//If the value of p is NULL, there is a problem
 free(p);
}

3.2 cross border access to dynamic development space

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;//Cross border access when i is 10
 }
 free(p);
}

3.3 free release for non dynamic development memory

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

3.4} use free to release a part of dynamic memory

void test()
{
 int *p = (int *)malloc(100);
 p++;
 free(p);//p no longer points to the beginning of dynamic memory
}

3.5 release the same dynamic memory multiple times

void test()
{
 int *p = (int *)malloc(100);
 free(p);
 free(p);//Repeated release
}

3.6 dynamic memory forgetting release (memory leakage)

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

Forgetting to release dynamically opened space will cause memory leakage,

It should be noted that dynamic memory must be released correctly;

Topics: C Back-end