Dynamic memory management (literacy explanation)

Foreword: The three magic weapons for learning data structures well:Pointers, structures, and dynamic memory management. Pointers have been discussed in detail before. , after everyone has read it, basically there is no problem with pointer methods

1 Why is it necessary to develop dynamic memory?

Because the dynamic memory space is allocated in the heap area and can be released, but the memory space allocated in the stack area is dead and cannot be released.

2 How to use dynamic memory reasonably?

First you have to learn to allocate memory, and then release the memory under appropriate conditions.

3 Applications of dynamic memory

(1) First, let’s learn about the functions of dynamic memory

one:malloc

Next let's put an array in the heap area

There are two situations in which malloc opens up space.

1 The development fails and a null pointer is returned.

2 If the development is successful, an address pointing to a good space will be returned.

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr[10] = { 0 };
	int* p = &arr;
	p =(int*)malloc(sizeof(arr)*sizeof(int));
	//这里要判断一下,空间开辟是否失败,如果失败malloc返回的就是空指针
	if (p == NULL)
		return 1;
	for (int i = 0; i < 10; i++)
	{
		p[i] = i;
		printf("%d ", p[i]);
	}
	//这里malloc开辟的空间用完了记得释放
	free(p);
	//然后把指针p置空
	p = NULL;
	return 0;
}

Let me show you the debugging effect:

It becomes a null pointer

two:calloc

There is only one difference between calloc and malloc. Before returning the address, all the contents pointed to by the pointer will be initialized to 0.

int main()
{
	int* p = (int*)calloc(10,sizeof(int));
	//好习惯是每天养成的,记住一定要判断是否是空指针
	if (p == NULL)
		return 1;
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", p[i]);
	}
	free(p);
	p = NULL;
	return 0;
}

three:realloc

rThe advantage of ealloc is that it makes the space management of dynamic memory very flexible. If you feel that the applied memory space is too large, it can be expanded, and if it is small, it can be reduced.

Expansion is divided into two situations:

(1) If the space behind the original space is large enough, then the original address will be returned.

(2) If the space behind the original space is not enough, then it is necessary to open up a new space, take out the content of the original address and put it in the new space that has been opened now, and the starting address of the new address will be returned.

int main()
{
	int* p = (int*)calloc(10,sizeof(int));
	//好习惯是每天养成的,记住一定要判断是否是空指针
	if (p == NULL)
		return 1;
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", p[i]);
	}
	//现在我认为我的空间开辟小了想要扩容
	p = (int*)realloc(p, sizeof(int) * 15);//后面的sizeof(int) * 15是新空间的大小,单位是字节
	//然后我继续打印
	for (int i = 10; i < 15; i++)
	{
		p[i] = i;
		printf("%d ", p[i]);
	}
	free(p);
	p = NULL;
	return 0;
}

Finally, let me mention that if the pointer in front of realloc is a null pointer, then the function of realloc is equivalent to malloc.

Guess you like

Origin blog.csdn.net/2301_79811170/article/details/134843085