【C language】☀️Dynamic memory management and related functions

content

1. Why there is dynamic memory allocation

Second, the dynamic memory function

 malloc and free

calloc

realloc


1. Why there is dynamic memory allocation

 There are two ways to open up space that we have learned before:

int main()
{
	int a = 5;
	char ch = 'b';
	int arr[20] = { 0 };
}

The above methods of opening up memory are all fixed and cannot be changed after the memory has been opened up. This way of opening up is not flexible enough .

Dynamic memory allocation in C language solves this problem

When learning C language, memory is usually roughly divided into three parts: stack area, heap area, and static area.

 Dynamically opening up memory is the space opened up on the stack

Second, the dynamic memory function

 malloc and free

malloc function prototype

void *malloc( size_t size );

Parameters: size_t size, is how much space you want to open up (in bytes), the type is unsigned integer

Return value: The pointer of type void* is to return the starting address of the opened space. It is uncertain what type of data this space will be used to store in the future, so it is of type void*; if the memory development fails, returns a null pointer (NULL)

Header file : <stdlib.h>

If the parameter size is 0, the behavior of malloc is undefined by the standard and depends on the compiler

The following example:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int* ps = (int*)malloc(100);
	if (ps == NULL)
	{
		perror("malloc:");
	}
	else
	{
		for (int i = 0; i < 50; i++)
		{
			*(ps + i) = i;
		}
		for (int i = 0; i < 50; i++)
		{
			printf("%d ", *(ps + i));
		}
	}
	return 0;
}

analyze:

Because we want to use the dynamically opened space to store integer data, we use an integer pointer to accept the return value, and we need to force type conversion, and then use an if statement to check whether the opening is successful, and if it fails, use perror to print the error Information, if successful, use this space later, assign 0-50 to the first 50 spaces, and print out

operation result:

 But this is not the end, because we have to return the space to the operating system after we use up the space for memory. Otherwise, this space will not be used by itself, but others will not be able to use it, and memory is still wasted, causing the problem of memory leaks . , here use free to release memory

free prototype

void free (void* ptr);

 use like this

free(ps);  //释放内存
ps = NULL;  //将指针置空

Because there is still the address of this space in the ps pointer after free(ps) releases the memory, we should empty ps in time , otherwise it will be illegal to use ps to access it later.

  • If the space pointed to by the parameter ptr is not dynamically allocated, the behavior of the free function is undefined
  • If the parameter ptr is a NULL pointer, the function does nothing

calloc

Function prototype:

void *calloc( size_t num, size_t size );

parameter:

size_t num This is to specify how many elements to store

size_t size specifies the size of each element of the element to be stored

This function will automatically initialize the opened space to 0

Return value: void* type, if the opening is successful, return the address of the opened space, if the opening fails, return a null pointer

Header file : <stdlib.h>

use:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int* ps = (int*)calloc(10,sizeof(int));
	if (ps == NULL)
	{
		perror("malloc:");
		return 0;
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", *(ps + i));
	}
	free(ps);
	ps = NULL;
	return 0;
}

realloc

This function is used to dynamically adjust the size of the opened memory. The emergence of the realloc function makes dynamic memory management more flexible

Function prototype:

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

Parameters : memblock, the address of the memory to be adjusted

        size, the size after adjustment

Return value : the starting address of the memory after adjustment void* type

Header file : <stdlib.h>

use:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int* ps = (int*)calloc(10,sizeof(int));
	if (ps == NULL)
	{
		perror("malloc:");
		return 0;
	}
	//调整大小
	int* ptr = (int*)realloc(ps, 20 * sizeof(int));
	if (ptr == NULL)
	{
		perror("realloc");
	}
	else
	{
		ps = ptr;
		ptr = NULL;
	}
	for (int i = 10; i < 20; i++)
	{
		*(ps + i) = i;
	}
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", *(ps + i));
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46531416/article/details/120492414