Allocation and release of storage space
Data structures for dynamic storage allocation
header File #include <stdlib.h>
Dynamic storage allocation with four functions {malloc(),calloc(), realloc()} , the dynamic storage release: Free().
1.malloc(), dynamically allocating a memory space
void *malloc (unsigned int size)
Use sizeof() to calculate the size of the application space
Example:int *p= (int *) malloc (sizeof (int))
Request a storage space of type int, and convert the assigned address space address into an int type address assigned to the defined pointer variable P.
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/5F/wKioL1dS5r2S60FaAAEB0FoXFdQ707.png-wh_500x0-wm_3 -wmp_4-s_2744750645.png "title=" Ab_%bis '%c ' o52{5c_x} (lx.png "alt=" Wkiol1ds5r2s60faaaeb0foxfdq707.png-wh_50 "/>
2.calloc (), dynamically allocating contiguous memory space
void *calloc (unsigned int n,unsigned int size);
Memory requests N-length storage space for size bytes, and returns the starting position of the storage space, n is the number of elements, and size is the element storage length.
Example: int*p= (int*) calloc (10,sizeof (int));
equivalent to int *p= (int *) Mallo (sizeof (int) *10);
Example: assigning 26-character characters to an array using the CALLOC and malloc functions
Char *ch1= (char*) calloc (26,sizeof (char));
Char *ch2= (char*) malloc (sizeof (char[26]);
3.realloc Change the size of the pointer to the space
void *realloc (void *prt,size_t size)
Cases:
Change a floating-point space size to an integer size
Fdouble= (double*) malloc (sizeof (double));
Iint=realloc (fdouble,sizeof (int));
Program: Double type to short
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/60/wKiom1dS5erQzhmAAADgcBUzkpk764.png-wh_500x0-wm_3 -wmp_4-s_2222202425.png "title=" $O 4CJIZ1WV (EN]) 0$q$$8wt.png "alt=" Wkiom1ds5erqzhmaaadgcbuzkpk764.png-wh_50 "/>
4.free (), freeing up storage space
void free (void *p)
No return value
Example: int *a;
*a= (int *) calloc (20,sizeof (int));
A=b;
b++;
Free (a);
This article is from the "11557552" blog, please be sure to keep this source http://11567552.blog.51cto.com/11557552/1786161
C allocation and release of storage space