functions in dynamic memory

The functions in dynamic memory include malloc, calloc, realloc, and free functions

The header file is # includ<malloc.h>

1. malloc function, dynamically apply for memory

calloc(number of elements * number of bytes)

malloc(n*sizeof(int) means: apply for n bytes of int type memory, store it in the heap, the addresses are continuous, and the first address is returned.

int * p = ( int * ) malloc (n * sizeof (int) ) means: an integer pointer is defined to store the first address of the requested memory. Red

The part of int * indicates the meaning of forced transfer, because the memory requested by malloc returns void type, that is to say, how many bytes are needed, the system will divide

How many bytes of memory are allocated, and you don't care what type it is. When you need what type, you can force it to what type.

Reason for application failure: The application memory is too large and cannot be satisfied, and a null pointer is returned. To prevent application failure you can do the following:

int *p = (int *)malloc(n*sizeof(int));//equivalent to int p[n];
	assert(p != NULL);
	if(p == NULL)
	{
		return ;
	}

2, calloc function, dynamically apply for memory, and assign it to 0

  calloc(number of elements, number of bytes)

int *arr = (int *)calloc(10,sizeof(int));
The following operations are equivalent to calloc
	int *arr = (int *)malloc(10*sizeof(int));
	for(int i=0;i<10;i++)
	{
		arr[i] = 0;
	}

3. The realloc function expands or shrinks the requested memory

realloc (previously specified memory block, new size)

p = (int *)realloc(p,20*sizeof(int));

The following code is equivalent to realloc
	int *q = (int *)malloc(20*sizeof(int));
	for(i=0;i<10;i++)
	{
		q[i] = p[i];
	}
	p = q
	q = NULL;

4. Free function, release memory, if not release, memory leaks

free does not need length because there is header and trailer information

	int *p1 = (int *)malloc(10*sizeof(int));
	free(p1);//Show(p1,10);,

The length information should be given when the show function is used, but the free function does not need to be stored because of the requested memory.

Information, the role of the tail is the role of seamless bonding. When using the free function, it will automatically search the header information to get the length information.

Reason for free crash:

(1) Out of bounds. Missing write sizeof or realloc The second parameter is wrongly written as the number of new bytes

intmain()
{
	int *p = (int *)malloc(10*sizeof(int));
	for(int i=0;i<=10;i++)
	{
		p[i] = 0;
	}
	free(p);
	return 0;
}

(2) Modified the pointer pointing, p++ cannot find the length information in the header

intmain()
{
	int *p = (int *)malloc(10*sizeof(int));
	for(int i=0;i<10;i++)
	{
		*p = 0;
		p++;
	}
	free(p);
	return 0;
}

(3) Repeatedly release the same segment of memory

intmain()
{
	int *p = (int *)malloc(10*sizeof(int));
	for(int i=0;i<10;i++)
	{
		p[i] = i;
	}
	int *q = (int *)malloc(20*sizeof(int));
	for(int i=0;i<10;i++)
	{
		q[i] = p[i];
	}
	free(p);
	p = q
	q = NULL;
	
	free(p);
	free(q);
	return 0;
}

(4) Release non-dynamic memory

intmain()
{
	int arr[10];
	free(arr);
	return 0;
}

Whether free can be released to empty at the same time, it cannot be accessed again because it becomes a wild pointer after release. I define a Free function to achieve it. At the same time, it is explained that if free wants to have this function, it will be more troublesome to use.

void Free(int **p)
{
	free(*p);
	*p = NULL;
}
int main()
{
	int *p = (int *)malloc(20);
	printf("%d\n",p);
	//free(p);
	Free(&p);
	printf("%d\n",p);//使用free(p)后变为野指针
	p = NULL;
	free(p);
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324702144&siteId=291194637