Compilation preprocessing and Dynamic Storage Allocation (2), compilation preprocessing
2. Dynamic Storage Allocation
Before that, the variables and arrays used to store data must be defined in the description section. C-compiled programs understand the size of the storage space they need by defining statements, and allocate appropriate memory space to them in advance. Once these spaces are allocated, they remain unchanged during the lifetime of the variable or array. Therefore, this allocation method is called static storage allocation ".
There is also a memory space allocation method called "Dynamic Storage Allocation" in C language: when space is required to store data during program execution, the specified memory space is obtained through "application; you can release unused space at any time. You can call the standard library functions provided by C language to achieve dynamic allocation, so as to get a specified amount of memory space or release the specified memory space.
The ansi c standard defines four functions for the Dynamic Allocation System: malloc, calloc, free, and realloc. When using these functions, the header file stdlib. h must be included at the beginning of the program.
2.1 malloc function and free function 2.1.1 malloc Function
The ansi c standard specifies that the return value type of the malloc function is void *, and the function is called in the form of malloc (size ). The type of size must be unsigned int.
The malloc function is used to allocate a storage area of size bytes. A base-type void address pointing to the first address of the storage area is returned. If there are not enough memory units for allocation, the function returns NULL ).
If the short int type data occupies 2 bytes and the float type data occupies 4 bytes of storage unit, the following program section points pi to a short int type storage unit, point pf to a float storage unit:
Short int * pi;
Float * pf;
Pi = (short *) malloc (2 );
Pf = (float *) malloc (4 );
If (pi! = NULL) * pi = 6;
If (pf! = NULL) * pf = 3.8;
In ansi c, the pointer returned by the malloc function is void * (valueless type). Therefore, when calling a function, you must convert it to the required type by force type conversion. In the above section, the * sign in the brackets when calling the malloc function is indispensable; otherwise, it is converted to the common variable type instead of the pointer type.
The storage unit dynamically allocated has no name and can only be referenced by pointers. Once the Pointer Points, the metadata storage unit and stored data cannot be referenced. The dynamic storage unit allocated through the malloc function has no initial values.
When dynamically applying for a bucket, if you cannot determine the number of bytes occupied by the data type, you can use the sizeof operator.
For example:
Pi = (int *) malloc (sizeof (int ));
Pf = (float *) malloc (sizeof (float ));
This is a common form. At this time, the system will calculate the number of bytes of the specified type. Using this form will facilitate program transplantation.
2.1.2 free function
The function is called in the following format:
Free (p );
Here, the pointer Variable p must point to the address allocated by the dynamic allocation function malloc or calloc. The free function releases the bucket referred to by pointer p so that this part of the space can be re-dominated by the system. This function does not return values.
2.2 calloc Function
The ansi c standard specifies that the type of the return value of the calloc function is void *, and the function is called in the form:
Calloc (n, size );
The n and size types are required to be unsigned int.
The calloc function is used to allocate continuous storage space for n data items of the same type. The length of each data item is size bytes. If the assignment is successful, the function returns the first address of the bucket; otherwise, null is returned. By calling the storage unit allocated by the calloc function, the system automatically assigns the initial value 0. For example:
Char * ps;
Ps = (char *) calloc (10, sizeof (char ));
The above function call statement opened up 10 consecutive char type storage units, directed by ps to the first address of the storage unit. Each storage unit can store one character.
Using the calloc function to dynamically open up the storage unit is equivalent to opening up a one-dimensional array. The first parameter of the function determines the size of the one-dimensional array, and the second parameter determines the type of the array element. The Return Value of the function is the first address of the array. Dynamic storage units opened up using the calloc function can be released using the free function.
2.3 write programs, use the malloc function to open up dynamic storage units, store the input three integers, and then output the three numbers in ascending order. 1 # include <stdio. h> 2 # include <stdlib. h> // malloc function header file 3 4 // swap function, sort the three integers in ascending order 5 void Exchange (int * pS, int * pM, int * pB); 6 7 // main function 8 int main () 9 {10 int * p1, * p2, * p3; 11 p1 = (int *) malloc (sizeof (int); 12 p2 = (int *) malloc (sizeof (int); 13 p3 = (int *) malloc (sizeof (int )); 14 15 printf ("Please enter three integer numbers. \ n "); 16 17 if (p1! = NULL & p2! = NULL & p3! = NULL) 18 {19 scanf ("% d", p1, p2, p3); 20 // call the Exchange function 21 Exchange (p1, p2, p3 ); 22 23 // * p1 <* p2 <* p324 printf ("% d", * p1, * p2, * p3 ); 25} 26 // release dynamic storage space 27 free (p1); 28 free (p2); 29 free (p3); 30 return 0; 31} 32 33 void Exchange (int * pS, int * pM, int * pB) 34 {35 int nn; // intermediate variable 36 37 if (* pS> * pM) 38 {39 nn = * pS; 40 * pS = * pM; 41 * pM = nn; 42} 43 if (* pS> * pB) 44 {45 nn = * pS; 46 * pS = * pB; 47 * pB = nn; 48} 49 if (* pM> * pB) 50 {51 nn = * pM; 52 * pM = * pB; 53 * pB = nn; 54} 55}MALLOC