C Language Notes - Chapter 1 Dynamic Memory Management

Keywords: C

Introduce: Why use dynamic memory allocation

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

Space opened up above:

1.Space development size is fixed.
2.When declaring an array, you must specify the length of the array, and the memory it needs is allocated at compile time.
Sometimes the size of space we need is only known when the program is running (for example, by entering the size of an array), and the way that space is opened up at the compilation of the array is not enough.

1. Dynamic Memory Functions

Introduce header file

#include <stdlib.h>

 

1.malloc

Functions provided in C:

void* malloc (size_t size);

The function requests a contiguous free space from memory and returns a pointer to that space. Request space in the heap area and return the starting address without initializing the space.

Success returns a pointer to the open space.
If the development fails, the NULL pointer is returned.
The return value is of type void*, so the malloc function does not know the type of open space and requires a cast when used.
If the parameter size is 0, malloc's behavior is standard undefined, depending on the compiler.

2.free

C also provides the function free for dynamic memory release and recycling.

The free function is used to release dynamically exploited memory.
When the space pointed to by the parameter ptr is not dynamically exploited, the behavior of the free function is undefined.
When the parameter ptr is a NULL pointer, the function does nothing.

void free (void* ptr);

Example:

int main()
{
	//Request Space
	int* a=(int*)malloc(sizeof(int) * 100);
	if (a == NULL)
	{
		return -1;
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(a + i)=i;
	}
	//release
	free(a);
	a = NULL;

	return 0;
}

Now that you have free(a), why do you want a = NULL?

Because you want to leave the used pointer empty, disconnect the pointer from memory completely.

3.calloc

 void* calloc (size_t num, size_t size);

Elements of num size and size size size size open up a space on the heap area and initialize each byte of space to zero.
The difference from malloc is that calloc initializes each byte of the requested space to zero before returning the address.

Example:

int main()
{

	int* p = (int*)calloc(10, sizeof(int));
  //int* p = (int*)calloc(4000000000, sizeof(int));
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return-1;
	}
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i;
		printf("%d ", *p);
	}
	//release
	free(p);
	p = NULL;

	return 0;
}

Result 1: Successfully opened memory and initialized to 0

Result 2: To open up memory that is too large, such as the contents in the comment, 40,000,000 int bytes, fails.

4.realloc

void* realloc (void* ptr, size_t size);

The realloc function can be used to dynamically open up memory size adjustments.

ptr is the memory address to be adjusted
New size after size adjustment
The return value is the memory start position after the adjustment.
On the basis of adjusting the size of the original memory space, this function also moves the data in the original memory to a new space.

realloc adjusts memory space in two ways:

1. There is enough space after the original memory. To expand the memory, add space directly after the original memory. The data of the original space will not be changed. The original address is still returned.

2. There is not enough space in the original memory, find another suitable size of continuous space on the heap space to use, the function returns a new address. That is, copy the data from the original memory to free up the old memory space.

Example 1:

int main()
{

	int* p = (int*)calloc(10, sizeof(int));
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		return-1;
	}
	else
		return -1;

	int i = 0;
	for (i = 0; i < 10; i++)
	{
		*(p + i) = i;
		printf("%d ", *p);
	}
	printf("\n");
	//Increase space
	int* a;
	a=(int *)realloc(p, 20 * sizeof(int));
//To place the return value of realloc with a new variable a.
	if (a != NULL)
	{
		p = a;
	}
	for (i = 0; i < 20; i++)
	{
		*(p + i) = i;
		printf("%d ", *(p+i));
	}
	//release
	free(p);
	p = NULL;

	return 0;
}

Example 2:

int *ptr = malloc(100);
if(ptr == NULL)
{
     exit(EXIT_FAILURE); 
}

//Code 1
ptr = realloc(ptr, 1000);//Wrong! The null pointer will be returned to ptr and the original space will not be found.
To reuse it, use the field pointer 

//Code 2
int*p = NULL;
p = realloc(ptr, 1000);
if(p != NULL)
{
    ptr = p;
}

free(ptr);

Thank you for seeing you here! Please click on your favorite support ~Welcome to your comments and friendly discussions with bloggers! Updating...

Posted by fourlincoln10 on Fri, 24 Sep 2021 09:07:52 -0700