Comparison of malloc and new

Abstract: This paper analyzes the difference between the two malloc and new.

1, the memory location in which the application

  • malloc is a concept c language, application is the heap memory space. Computer operating system heap is allocated out of a special area of ​​memory, dynamic memory allocation for a program.
  • c ++ is a new concept of space application called free store. For the free store is a new concept in, and can be understood as a new space for all applications, it can be called the free store.

Note: The free store is not necessarily a heap, there may be static storage area.

2, return type

  • When the new operator memory allocation successful, returns a pointer to the object type, the object type in strict matching, without the need for the type of conversion, it is in line with new type of operator safety.
  • malloc memory allocation is successful return void *, need to cast the void * pointer converted to the type we need.

3, allocation failure return value

  • When the new memory allocation fails, it will throw an exception bac_alloc, it does not return NULL;
  • Return NULL when allocating memory failed malloc.

4, if you need to specify the size of the application memory

  • Not need to specify the size of the memory block when the new operator for memory allocation, the compiler assumes calculated based on the type information.
  • malloc the need to explicitly indicating that the memory size required.

5, whether to call the constructor and destructor

Using the new operator will allocate memory when the object is subjected to three steps:

  • Step: call operator new function (for operator new array is []) allocate a large enough, the original, unnamed memory space for storing objects of a specific type.
  • Step two: Run the appropriate compiler constructor to construct the object, and its incoming initial value.
  • Third: the object construction is completed, returns a pointer to the object.

Undergoes two steps when using the delete operator to release the object memory:

  • The first step: the calling object destructor.
  • Step two: the compiler calls operator delete (or operator delete []) function to release memory space.

In summary, the constructor new / delete objects will call / destructor to complete the constructor / destructor object.

However, it does not call malloc constructor / destructor.

6, the processing for the array

  • C ++ provides new [] and delete [] array to specifically handle Type: new support for the array are reflected in it will call the constructor initializes each element of an array, for each object call the destructor release objects. Note that delete [] to the new [] supporting the use of, or will find an array of objects partial release of the phenomenon, caused by a memory leak.
  • C language malloc, you know it and to put on a piece of memory array or something else, anyway, it will give you an original memory, giving you a memory address on the bin. So if you want to dynamically allocate an array of memory, we also need to manually customize the size of the array.

7, it is possible to re-allocate memory intuitively

After using malloc memory allocation, if found insufficient memory in use, it can be used to re-allocate memory function realloc achieve expanded memory. realloc first determine whether the current pointer memory is sufficient contiguous space, if any, in situ expanded memory address can be allocated, and returns the old address pointer; when it does not, according to the new allocation of space specified size, the original from beginning to end data copied to the newly allocated memory area, and then release the original memory area. Without such an intuitive new facilities to expand the memory.

8, can be overloaded

new can be overloaded, and malloc can not be overloaded.

Guess you like

Origin www.cnblogs.com/lzy820260594/p/11563721.html