To understand C How to allocate dynamic memory in language , We first need to understand what is dynamic memory allocation , We have already introduced global variables and local variables , Global variables are allocated to static storage areas in memory , Non static local variables ( Include formal parameters ) Is a dynamic storage area allocated in memory , This storage area is called a stack (stack) Area of . in addition to ,C The language also allows the creation of dynamic memory allocation regions , To store some temporary data , This data does not have to be defined in the declaration section of the program , You don't have to wait until the end of the function to release , But open it up whenever you need it , Release when not needed .
These data are temporarily stored in a special free storage area , Called heap (heap) area . You can apply to the system for space of the required size as required .
Because they are not defined as variables or arrays in the declaration section , Therefore, these data cannot be referenced by variable name or array name , Can only be referenced by pointer .

So how does the computer establish the dynamic allocation of memory ; The dynamic allocation of memory is realized through the library functions provided by the system , Mainly
malloc,calloc,free,realloc this 4 Functions . I will introduce them one by one .

one . use malloc Function to open dynamic storage area ;

The general form is ;

void *malloc(unsigned int size);

Its function is to allocate a length of size Continuous space of . Formal parameter size Type of is unsigned integer ( Negative numbers are not allowed here ). Value of this function ( Namely “ Return value ”) Is the address of the first byte of the allocated area , Or , This function is a pointer function , The returned pointer points to the first byte of the allocation field .

two . use calloc Function to open dynamic storage area

The general form is ;

void *calloc(unsigned n,unsigned size);

Its function is to allocate in the dynamic storage area of memory n Pieces of length size
Continuous space of , This space is usually large , Enough to hold an array . use calloc Function to open up dynamic storage space for a one-dimensional array ,n Is the number of array elements , The length of each element is size.
This is dynamic array . Function returns a pointer to the first byte of the allocated field ; If the assignment is unsuccessful , return NULL.

three . use realloc Function to reallocate dynamic storage

The general form is ;

void *realloc(void*p,unsigned int size);

If passed malloc Function or calloc Function gets the dynamic space , Want to change its size , Available recalloc Function reassignment .

use realloc Function will p The size of the pointed dynamic space changes to size.p The value of does not change , If reassignment is unsuccessful , return NULL.

four . use free Function to free dynamic storage

The general form is ;

void free(void*p);

Its purpose is to release pointer variables p Dynamic space pointed to , So that this part of the space can be reused by other variables . p Last call expected calloc or malloc Function return value .

free Function has no return value .

above 4 Declaration of functions in stdlib.h In header file , When using these functions, you should use “#include” Command handle
stdlib.h Include header files in program files . This is the basic dynamic memory allocation .

report / feedback