On the c ++ new, delete and malloc and free

  malloc and free standard library function C ++ / C language, new / delete the C ++ operator. They can be used to apply dynamic memory and free memory. For non-intra data type of the object, light maloc / free can not meet the requirements of the dynamic object. To automate the object constructor will be created and the object to be automatically executed destructor before dying. Since malloc / free library function is not an operator, the control is not within the purview of the compiler is not able to perform the constructor tasks and destructor imposed malloc / free. So C ++ language required to complete a dynamic memory allocation and initialization of operator new, to be able to complete a clean-up and operator delete frees the memory work.

malloc and new:

  malloc is a library function c language, its function is to open up a continuous designated memory space (note: calling malloc must specify the space opened up) in the memory and returns a pointer to the allocated memory (the pointer to void *, void * pointer needs to be converted by the mandatory conversion into the type we need).

// use case 
int * PTR = ( int *) the malloc ( the sizeof ( int )) // allocate a space type int size 
float * ptr2 = ( float *) the malloc ( the sizeof ( int )) // allocate space float type Size

In general malloc always successfully allocated a contiguous memory space, but running out of memory or the memory is too fragmented and not enough time to open up continuous space of fixed size, malloc call fails, this time malloc returns a null pointer NULL, so every time when using the malloc in case it should be first determined to prevent the return pointer is empty.

 

  new is a manipulator (or keyword) c ++ provided. Which is also used to allocate memory. It calls the process can be broken down into three steps:

    1, calling operator new (reloadable function) allocate memory

    2, the call object in the allocated memory constructor that the object

    3, which returns a pointer to pointer memory space

  It is worth mentioning that the principle of internal memory space is opened up new calls malloc function, which calls the process is encapsulated in the new operator inside, it can be said new malloc is based on a series of re-packaging function. And malloc differ is that after the new open space in the operator new, call the object's constructor will construct objects in the space finally return the object, but malloc is only responsible for open space, is not responsible for the object is constructed; and the new allocation after the failure is not space but returns a null pointer throw a  std :: bad_alloc exception.

 

delete given free:

  And the role of free and delete new and delete the above-mentioned opposite. free function just passed a point of a pointer to the allocated memory, this memory can be automatically freed. And why calling malloc need to pass to open up space and free space do not need to release it passed? Actually, the reason is that when malloc allocation of space, not only in accordance with the size of the incoming parameters allocated space, but also insert some additional information (commonly known as the cookie) in the memory segment head and tail, which contains information about the memory size. Such free function only needs to read the information, we will know to release the memory size, scope and so on.

 

  And delete the calling process and new contrary, it calls the process also can be broken down:

    1, the call object destructor

    2, call operator delete

  delete the new corresponds to the principle of free up memory space also call free, its invocation is encapsulated within the operator delete, delete a package that is a series of functions on the basis of free. First delete call the destructor of the object, and then release the call free region underlying memory.

 

Knowledge of c ++ memory management can read a series of books and videos designate McNair teacher, which parse c ++ very thorough internal principle, each read can read new insights.

Guess you like

Origin www.cnblogs.com/LEEYATWAH/p/11577587.html