Application analysis of new and delete malloc and free in C++

  • 2020-04-02 02:36:53
  • OfStack

In general, the use and distinction of new/delete and malloc/free pairs are often examined in C/C++ interviews. If you can't answer such basic questions, you will be very sad. This paper is a simple analysis of the use and difference between new/delete and malloc/free, for your reference.

One, new and delete

New and delete are C++ operators that dynamically allocate and free memory.

1. New expressions

The standard library defines several overloaded versions of the operator new function, and a version that does not use noexcept may throw a bad_alloc exception in the event of a memory allocation failure, while the one used does not.


void* operator new ( size_t ); 
void* operator new[]( size_t );  
void* operator new ( size_t , const nothrow_t& ) noexcept; 
void* operator new[]( size_t , const nothrow_t& ) noexcept;

When we use the new expression, we actually perform three operations:

The new expression calls the standard library function of the above operator new (or operator new[]), which allocates a chunk of raw, unnamed memory large enough to store a particular type of object (or an array of objects).
The compiler runs the corresponding constructor to construct these objects and passes in an initial value for them.
The object is allocated space and constructed, returning a pointer to the object.

For the operator new function or the operator new[] function, its return type must be void*, the first parameter must be size_t and must not contain default arguments. When the compiler calls the operator new or operator new[] function, it passes the number of bytes needed to store the specified object or an array of specified objects to the size_t parameter.

2. Delete the expression

The standard library also defines several overloaded versions of the operator delete function, and the noexcept specifier means to return a null pointer in the event of a memory allocation failure instead of throwing a bad_alloc exception.


void operator delete ( void* ) noexcept; 
void operator delete[]( void* ) noexcept; 
void operator delete ( void* , const nothrow_t& ) noexcept; 
void operator delete[]( void* , const nothrow_t& ) noexcept; 

When we use the delete expression, we actually perform two operations:

(1). The pointer to the object or the array of the elements to perform the corresponding destructor.
The compiler calls the standard library function of operator delete (or operator delete[]) to free up memory space.

For the operator delete function or operator delete[] function, their return type must be void, and the type of the first parameter must be void*. Executing a delete expression calls the corresponding operator function and initializes the void* parameter with a pointer to the memory to be freed.

Two, malloc and free

Malloc and free are standard library functions in C/C++ and are also used to request dynamic memory and free memory.


void* malloc( size_t size ); 
void free( void* ptr ); 

The malloc function takes size_t, which represents the number of bytes to be allocated, and returns a pointer to the allocated space. If the assignment fails, a null pointer is returned. The free function accepts a void*, which is a copy of the pointer returned by malloc, and free returns the relevant memory to the system. Note: the malloc function does not call the constructor to initialize memory, and the free function does not automatically call the destructor.

The difference between new and malloc, delete and free


int *p1 = new int;   //There is no initialization list, so there is no initialization
int *p2 = new int();  //Null initializes the list, zero initializes, so zero is initialized
int *p3 = new int(3); //Non-null initializer list, performs value initializer, so initializes to 3
//Note: for built-in types without constructors, whether new is initialized depends on whether there is an initial list (an empty list "()" counts)
int *p4 = new int[100];   //Allocate size to sizeof(int)*100;
 
int *p5 = (int*)malloc(sizeof(int)*128); 
double *p6 = (double*)malloc(sizeof(double)*12); 

The difference between new and malloc:

New is an operator, and malloc() is a library function.

New calls the constructor, but malloc() does not.

New returns a pointer to the specified type, while malloc() returns void*.

. New automatically calculates the space to be allocated, while malloc() manually calculates the number of bytes.

New can be overridden, but malloc() cannot.


delete p1; 
delete [] p2; 
free(p3); 
free(p4);

The difference between delete and free:

Delete is an operator, free() is a library function.

. Delete will call the destructor, but free() will not.

. Delete can be overridden, but free() cannot.

Conclusion:

Malloc and free are standard library functions in C/C++, and new and delete are C++ operators. For objects without built-in data types, maloc/free alone cannot meet the requirements of dynamic objects. The constructor is executed automatically when the object is created, and the destructor is executed automatically before the object dies. Since malloc/free is a library function, not an operator, and is not within the compiler's control, you cannot impose malloc/free on the task of performing constructors and destructors.


Related articles: