The function for dynamically allocating memory in the C language is the malloc () function, and the corresponding memory-deallocation function is the function. Their function prototypes were:
void *malloc ( size_t size );// Header file is Stdlib.h or Malloc.hvoid free ( void *memblock );
The
parameter size is the allocated byte size (not the number of elements!). )。
The allocation of level two pointers is often confusing ... Now make a summary: the
C language to the two-level pointer to the memory allocation, the first allocation of the outer pointer memory space (hold pointer value), and then allocate the internal pointer to the memory space (holding the object value), the memory is released in order to release the internal pointer to the address space first, Finally, the address space referred to by the outermost pointer is released.
is explained under a specific program:
float** p = (float* *) malloc (240 * sizeof (float));//Assign an address space to the outer pointer p //and assign an address space to the inner pointer int i;for (i = 0; i < 240; i++) { p[i] = (float*) malloc (1216 * sizeof ( float));} ...... //free Memory for (i = 0; i < 240; i++) { free (p[i]);// Frees the memory pointer to the address space}//finally releases the address space that the outer pointer refers to free (p);
This article is from the "xinyi_xft" blog, declined reprint! malloc and free
in the
C language