Dynamic Memory and related functions

malloc - Application Memory
calloc - initialized to 0
realloc - modify the memory size (expand, shrink)
as Free - free memory, memory leaks

the malloc:
int * ARR = (int) the malloc (nsizeof (int));
the malloc function only of space applications to call, to call returns the first address space, a strong need to turn into a type of data required;

calloc:
Dynamic Application integer units 100, and each cell is 0
int * ARR = (int) the malloc (100sizeof (int));
for (int I = 0; I <100; I ++) //
{
ARR [I ] = 0;
}
is equivalent to
int * arr = (int) calloc (100, sizeof (int)); // the array is initialized to 0
Note:
Memset (ARR, 1,100sizeof (int)); // can likewise Memset the array is set to 0, but this function is provided only 0

realloc :
found deficiencies arr space of the original application, apply for more space
int * brr = (int) malloc (200sizeof (int)); // create more space
for (int i = 0; i <100; i ++) // copy original data
{
BRR [I] = ARR [I];
}
Free (ARR); // release the original memory
// update address
ARR = BRR;
BRR = NULL;

Is equivalent to
arr = (int *) realloc ( arr, 200 * sizeof (int)); // **, arr number of bytes of the new application is the original memory address, back to

free:
the memory space using the free release of unused

Published 13 original articles · won praise 3 · Views 631

Guess you like

Origin blog.csdn.net/weixin_43873172/article/details/88579283