Why there are dynamic memory allocation, dynamic memory functions (malloc function, free function, calloc function, realloc function)

1. Currently we know how to use memory

Insert picture description here

2. Why is there dynamic memory allocation

As we have learned above, the way of opening up the space has two characteristics: so it has the problem that the space is large, wasted, and small is not enough, so dynamic memory allocation is used.
(1) 空间开辟的大小是固定的
(2) 必须指定数组的长度

3. Dynamic memory functions

3.1 malloc and free

(1) malloc function

Insert picture description here
The malloc function applies for a continuous available space from the memory and returns a pointer to this space.
• 如果开辟成功,返回一个指向开辟好空间的指针
• 如果开辟失败,返回一个NULL指针,因此malloc的返回值一定要做检查

(2) free function

Insert picture description here
The free function is used to release dynamically opened memory
• 如果参数p指向的空间不是动态开辟的(eg:数组),那free函数的行为是未定义的;
• 如果参数p是NULL指针,则函数什么事都不用做;

(3) Illustrate how to use malloc function and free function

a. Open up 10 plastic-sized spaces, and put 0-9 in

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	//向内存申请10个整形的空间
	int* p = (int*)malloc(10 * sizeof(int));
	//开辟空间失败,打印失败原因
	if (p == NULL)
	{
    
    
		//errno:Last error number
		printf("%s\n", strerror(errno));
	}
	//开辟空间成功,将0-9放入
	else
	{
    
    
		for (int i = 0; i < 10; i++)
		{
    
    
			*(p + i) = i;
		}
		for (int i = 0; i < 10; i++)
		{
    
    
			printf("%d ", *(p + i));
		}
	}
	//释放掉p指向的这段空间,但是指针p还是指向这段空间
	free(p);
	//防止野指针,需要将指针制空
	p = NULL;
	return 0;
}

Insert picture description here

b. Failed to open the space, and print the reason for the failed development

• INT_MAX Maximum shaping -> Right-click to go to the definition view
Insert picture description here

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	//向内存申请10个整形的空间
	int* p = (int*)malloc(INT_MAX);
	//开辟空间失败,打印失败原因
	if (p == NULL)
	{
    
    
		//errno:Last error number
		printf("%s\n", strerror(errno));
	}
	//开辟空间成功,将0-9放入
	else
	{
    
    
		for (int i = 0; i < 10; i++)
		{
    
    
			*(p + i) = i;
		}
		for (int i = 0; i < 10; i++)
		{
    
    
			printf("%d ", *(p + i));
		}
	}
	//释放掉p指向的这段空间,但是指针p还是指向这段空间
	free(p);
	//防止野指针,需要将指针制空
	p = NULL;
	return 0;
}

Insert picture description here

3.2 calloc

(1) calloc function

Insert picture description here
• calloc函数的功能是为num个大小为size的元素开辟一块空间,并且把空间的每个字节初始化为0
• calloc和malloc的区别在于calloc会在返回地址之前把申请的空间的每个字节初始化为0

(3) Illustrate how to use the calloc function

a. Open up 10 plastic-sized spaces and initialize them to 0

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	int* p = (int*)calloc(10, sizeof(int));
	if (p == NULL)
	{
    
    
		printf("%s\n", strerror(errno));
	}
	else
	{
    
    
		for (int i = 0; i < 10; i++)
		{
    
    
			printf("%d ", *(p + i));
		}
	}
	free(p);
	p = NULL;
	return 0;
}

Insert picture description here

Insert picture description here

3.3 realloc

(1) realloc function

Insert picture description here
• p是要调整的内存地址
• size是调整之后的大小
• 返回值为调整之后的内存起始位置

(2) There are two situations in which realloc adjusts the memory space:

• Case 1: There is enough space after the original space

If there is enough memory space to append after the space pointed to by p, append directly, and then return to p
Insert picture description here

#include<stdio.h>
#include<string.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;
		}
	}
	//得用一个新的变量来接受realloc函数的返回值
	//防止开辟失败返回NULL给p找不到之前的空间
	int*ptr = (int*)realloc(p,40);
	if (ptr != NULL)
	{
    
    
		p = ptr;
		for (int i = 5; i < 10; i++)
		{
    
    
			*(p + i) = i;
		}
		for (int i = 0; i < 10; i++)
		{
    
    
			printf("%d ", *(p + i));
		}
	}
	free(p);
	p = NULL;
	return 0;
}

Insert picture description here
Insert picture description here

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

If the space pointed to by p does not have enough memory space to be added, the realloc function will find a new memory area, open up a space that meets the demand, and copy the data in the original memory to the new space, releasing the old Memory space, and finally return to the newly opened memory space address
Insert picture description here

#include<stdio.h>
#include<string.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;
		}
	}
	//得用一个新的变量来接受realloc函数的返回值
	//防止开辟失败返回NULL给p找不到之前的空间
	int*ptr = (int*)realloc(p,4000);
	if (ptr != NULL)
	{
    
    
		p = ptr;
		for (int i = 5; i < 10; i++)
		{
    
    
			*(p + i) = i;
		}
		for (int i = 0; i < 10; i++)
		{
    
    
			printf("%d ", *(p + i));
		}
	}
	free(p);
	p = NULL;
	return 0;
}

Insert picture description here
Insert picture description here

Note: A new variable must be used to accept the return value of the realloc function to prevent the development failure from returning NULL to p to find the previous space

Guess you like

Origin blog.csdn.net/weixin_50886514/article/details/113832159