How to dynamically allocate memory in C language

Table of contents

malloc

calloc

realloc

free


 

Nowadays, there are two ways to open up memory that we are familiar with. One is to define local variables in the stack area, and the other is to define global variables and static variables in the static area, and their sizes cannot be changed at will. And this article will introduce the memory allocation method that can change the size at any time.

The header files for all functions introduced in this article are: stdlib.h

malloc

First, let's get to know the first function malloc:

void* malloc (size_t size);

 It can open up a continuous space of size bytes for the user in the heap area, and return a first address of the memory block.

  1. If the allocation is successful, a pointer to the allocated space is returned.
  2. If the opening fails, a NULL pointer is returned, so the return value of malloc must be checked.
  3. The type of the return value is void*, so the malloc function does not know the type of the opened space, and the user can decide by himself when using it.
  4. If the parameter size is 0, the behavior of malloc is undefined by the standard and depends on the compiler.

 The general usage method is:

int* p = NULL;
p = (int*)malloc(4 * sizeof(int));
if (p == NULL)
{
    perror("malloc");
    return 0;
}
//判断完成后再使用malloc开辟的内存

calloc

void* calloc (size_t num, size_t size);

This is the second function introduced today. It will open up a piece of memory in the heap area for you and initialize it according to the number of data you need (num) and the size of each data (size unit: byte) .

 malloc:                                                                        calloc:

char* p1 = NULL;                                 char* p2 = NULL;
p1 = (char*)malloc(4 * sizeof(char));            p2 = (char*)calloc(4 , sizeof(char));

 It can be seen that calloc initializes the allocated memory to 0;

realloc

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

If the memory you opened up for the first time is not enough space in the subsequent use process, you can use the realloc function besides re-opening. This function will change the old space according to the address of the old space and the size of the new space you pass in. size, and then returns the first address of the new space. If the development fails, NULL will be returned.

Note: If the following space is insufficient, a new space will be opened and the value in the old space will be copied to the new space.

 

free

void free (void* ptr);

Note that the memory opened by malloc, calloc, and realloc will not disappear automatically. The free function means that when the user passes the first address of the dynamically allocated memory to it, it will reclaim the memory.

There are only two situations in which dynamically allocated memory is reclaimed:

  1. Will be recycled when the program ends;
  2. Actively use the free function to reclaim the requested memory.
  • The free function is used to release dynamically allocated memory.
  • If the space pointed to by the parameter ptr is not dynamically opened, the behavior of the free function is undefined.
  • If the parameter ptr is a NULL pointer, the function does nothing.

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/131726678