[C language] ☀️ Dynamic memory management and related functions

Keywords: C C++ Programming

catalogue

1, Why is there dynamic memory allocation

2, Dynamic memory function

  malloc and free

calloc

realloc

1, Why is there dynamic memory allocation

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

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

The above ways of developing memory are that after developing memory, the developed memory is fixed and cannot be changed. This way of developing memory is not flexible enough

Dynamic memory development in C language solves this problem

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

  Dynamic memory development is the space developed on the stack

2, Dynamic memory function

  malloc and free

malloc function prototype

void *malloc( size_t size );

Parameter: size_t size is how much space (in bytes) you want to open up. The type is unsigned integer

Return value: a pointer of void * type returns 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 void * type; If memory development fails, a NULL pointer (NULL) is returned

Header file: < stdlib. H >

If the parameter size is 0, malloc's behavior is standard and undefined, depending on the compiler

Examples are as follows:

#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;
}

analysis:

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

Operation results:

  But this is not over, because we have to open up space for memory and return it to the operating system after it is used up. Otherwise, this space will not be used by ourselves, but others can't use it. It is still wasting memory, causing the problem of memory leakage. Here, use free to release memory

free prototype

void free (void* ptr);

  Use it like this

free(ps);  //Free memory
ps = NULL;  //Set pointer to null

After free(ps) releases the memory, there is still the address of this space in the pointer ps, so we should set ps to null in time, otherwise it is illegal to use ps to access it later

  • If the space pointed to by the parameter ptr is not dynamically opened up, 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 );

Parameters:

size_t num   This specifies how many elements to store

size_t size   Specifies the size of each element to store

This function will automatically initialize the opened space to 0

Return value: void * type. If the development succeeds, the address of the developed space will be returned. If the development fails, the null pointer will be returned

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 memory size. The appearance of realloc function makes dynamic memory management more flexible

Function prototype:

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

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

        Size, adjusted size

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

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;
	}
	//Resize
	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;
}

Posted by r.smitherton on Fri, 22 Oct 2021 20:10:50 -0700