Dynamic memory allocation

Keywords: C

  • Formal parameters of stack local variable function

  • Heap dynamic memory allocation reference data type

  • Static area global variable static variable

malloc and free

  • The pointer of void * type will be returned after successful development

  • Failed to open. NULL returned

  • The parameter is the size of the open space

  • free(p) after reclaiming space p, p will still point to this space. For safety, p should point to NULL

  • The opening space malloc(size) size is not defined to be equal to 0 in the standard of c, so the execution standards of various compilers are different, so it is recommended that the size should not be 0

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>

int main() {
	//Apply for 10 spaces of type int
	int *p = (int*)malloc(10 * sizeof(int));//The return value of malloc is void *, so it is cast
	if (p == NULL)
	{
		printf("%s",strerror(errno));
	}
	else
	{
		//Successful development
		for (int i = 0; i < 10; i++)
		{
			*(p + i) = i;
		}
		for (int i = 0; i < 10; i++)
		{
			printf("%d \n",*(p+i));
		}
		//Recycle space
		free(p);
		p = NULL;
	}
	return 0;
}

calloc

  • An array member is initialized to 0

  • The first parameter is to open up several elements, and the second parameter is the space size of each element

  • A null pointer will be returned if the development fails

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main() {

	int* p = (int*)calloc(10,sizeof(int));
	if (p == NULL)
	{
		printf("%s",strerror(errno));
	}
	else {
		for (int i = 0; i < 10; i++)
		{
			printf("%d \n",*(p+i));
		}

		free(p);
		p = NULL;
	}
	return 0;
}

ralloc

Adjust the size of dynamic space

  • If the back of the original development space is enough, directly add the size of the development space behind the original space. If it is not enough, reopen a space and copy the value in the original space to the new space

  • Successful development returns the first address of the space. Failed development returns a null pointer (null)

  • The re opened space cannot be directly given to the original space. If the development fails, the original space will not be found (judgment and intermediate variables need to be added)

  • realloc can also realize the function of malloc. The first parameter is passed to NULL

  • The second parameter is the last desired space size

//The space opened at one time can not meet the demand
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main() {
	int* p = (int*)malloc(20);
	if (p == NULL)
	{
		printf("%s \n",strerror(errno));
	}
	else {
		for (int i = 0; i < 5; i++)
		{
			*(p + i) = i;
		}

		int* pp = (int*)realloc(p, 40);
        if (pp == NULL)
		{
			printf("%s \n",strerror(errno));
		}
        else{
			p = pp;
		}

		for (int i = 5; i < 10; i++)
		{
			*(p + i) = i;
		}

		free(p);
		p = NULL;
	}
	return 0;
}

Common dynamic memory space development errors

  • Dereference of null pointer - no judgment of development failure

  • Cross border access to dynamic memory space ---- note that the unit of space is bytes. How do you want to use it? Note that the number of bytes occupied by each type is better to use n * sizeof (type)

  • free release of non dynamic open space

  • Use free to release part of the dynamic development space ----------- if the development space is assigned to the pointer * p, it is better not to use * p + + when reusing, because free(p) p is not the desired value when releasing

  • The same memory space is released multiple times ---- here, pointing to a null pointer to the released space can be avoided

  • Dynamic development memory forgets to release (memory leak) -- pay attention to the pointer assigned to the development space. If it is a local variable, it must be released in its scope

Flexible array

The last element in the structure in c99 is allowed to be an array of unknown size    Find the size of the structure that does not contain the size of the flexible array

#include<stdio.h>
#include<string.h>
struct st
{
	int i;
	int arr[];//int arr[0];
};
int main() {

	struct st s1;
	return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct st
{
	int a;
	//int* arr;//int arr[0];arr[]
	int arr[];
};
int main() {

	struct st* s1 = (struct st*)malloc(sizeof(struct st) + 4 * sizeof(int));//Here's 4 when you want to open up the flexible array size
	if (s1 == NULL)
	{
		printf("%s\n", strerror(errno));
	}
	else {
		s1->a = 100;
		for (int i = 0; i < 3; i++)
		{
			s1->arr[i] = i;
		}
		s1->arr[3] = 0;
	}


	//If the flexible array size is found to be insufficient
	struct st* s2 = (struct st*)realloc(s1, sizeof(struct st) + 4 * sizeof(int) + 4 * sizeof(int));
	if (s2 != NULL)
	{
		s1 = s2;

		for (int i = 3; i < 7; i++)
		{
			s1->arr[i] = i;
		}

	}
	else {
		printf("%s\n", strerror(errno));
	}

	free(s1);
	s1 = NULL;
	return 0;
}

Posted by MrXander on Sat, 11 Sep 2021 23:48:32 -0700