Common dynamic memory allocation functions and operators

Common dynamic memory allocation functions and operators

Function: Series C language function in the file stdlib.h provided

void * malloc(size_t)
void * calloc(size_t,size_t)

malloc does not initialize release will be initialized to zero calloc space, so in the pursuit of efficiency, the use of malloc

void * realloc(void * p,size_t size)

Step realloc function space applications:
1, the size of space applications size bytes
2, the value of p is just copied into the application space
3, p is released, return just the application space

Operator: new delete

int * pi = new int;

delete pi;

int * pi = new int [5];

delete [] pi;

※ When dynamic memory allocation by instantiating objects, new / delete will automatically call the constructor / destructor, and malloc / free not. Therefore, when the space allocated to the class, preferably using new / delete.

Published 77 original articles · won praise 23 · views 7555

Guess you like

Origin blog.csdn.net/Hots3y/article/details/100590631