Detailed explanation of memory management functions malloc, calloc, realloc

1.malloc

When we want to open up a dynamic memory space, we need to use dynamic memory functions, such as

char* p;

When we want to use the memory under address p, we need to use the malloc function

void* malloc(size_t size);

Note that the return type of the malloc function is (void*), and the formal parameter is the number of bytes to open up space. So to use the malloc function, you must cast the return value to the desired type, such as

char* p1=(char*)malloc(10);//在p1这个地址下开辟10个字节空间,可以存放10个char型数据
int* p2=(int*)malloc(20);//在p2这个地址下开辟20个字节空间,可以存放5个int型数据

 Note that the malloc function sometimes fails to open up memory space, and returns a null pointer ( NULL ) at this time. Therefore, to use the malloc function better, you must also check whether the memory is successfully opened. The code is as follows

int* p=(int*)malloc(20);//开辟内存空间
if(p==NULL)
{
    printf("%s",strerror(errno));//打印开辟失败的原因
}

What you need to know is that the memory space opened up by the malloc function will not be automatically initialized, and the random values ​​​​are placed in it.

	int* p = (int*)malloc(20);//开辟内存空间
	if (p == NULL)
	{
		printf("%s", strerror(errno));//打印开辟失败的原因
	}
	else
	for (int i = 0; i <5 ; i++)//访问新开辟的内存
	{
		printf("%d", (int)*(p + i));
	}

The result is as follows

 

 The malloc function is finished, but there is a very important point that has not been mentioned. We know that creating an array is equivalent to applying for a continuous space in the memory space. The memory space applied for by the array is on the stack. When the array leaves the scope, it will be automatically destroyed and the memory will be released.

However, the memory space requested by the malloc function here is on the heap, and the scope will not be destroyed. The memory must be released manually, otherwise it will cause a memory leak! (Even if the OS will automatically reclaim the memory after exiting the program, it is still a good habit to release it manually)

To release memory manually, you need to use the free function. The free function will release the allocated memory, and the first address of the memory will become a wild pointer, so the first address must be set to NULL. code show as below

int* p = (int*)malloc(20);//开辟内存空间
	if (p == NULL)
	{
		printf("%s", strerror(errno));//打印开辟失败的原因
	}
    //使用

    //手动释放空间
	free(p);
	p = NULL;

 2.calloc

Unlike malloc, the calloc function will automatically initialize the newly opened memory space to all 0s

void* calloc(size_t num,size_t size);

size_t num: the number of development

size_t size: the size of the developed type

For example, open up 5 integer spaces

#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<stdio.h>
int main()
{
	int* p = (int*)calloc(5,4);//开辟内存空间
	if (p == NULL)
	{
		printf("%s", strerror(errno));//打印开辟失败的原因
	}
	else
		//使用

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

3.realloc

The realloc function is used to open up a larger space when the original space is insufficient

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

void* ptr: the first address of the original space

size_t size: new space byte size

The realloc function will encounter two situations when opening up new space

  1. There is enough space behind the original space, and a new space will be opened up behind the original space, and the returned address is the same as the original address.
  2. Insufficient space behind the original space:
  • realloc will find a bigger space
  • Copy the original data to the new space
  • free up old space
  • Return the address of the new space (different from the original address)
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<stdio.h>
int main()
{
	int* p = (int*)calloc(5,4);//开辟内存空间
	if (p == NULL)
	{
		printf("%s", strerror(errno));//打印开辟失败的原因
	}
	else
		//使用
		for (int i = 0; i < 5; i++)
		{
			printf("%d ", *(p + i));
		}
	p=realloc(p, 40);//扩大内存空间,将20字节增到40字节
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", *(p + i));
	}
	free(p);
	p = NULL;
	return 0;
}

 

 

Guess you like

Origin blog.csdn.net/2301_76144863/article/details/129979308