New and delete, malloc and free Application analysis in C + + _c language

Source: Internet
Author: User

Generally speaking, the use and difference between the two pairs of new/delete and malloc/free are often examined in the interview of C/s + +, and if the basic questions are not answered, it is estimated that it is difficult to interview. This article is to New/delete and malloc/free these two pairs of use and the difference comparatively simple analysis, for everybody's reference.

One, new and delete

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

1.new expression

The standard library defines several overloaded versions of the operator new function, and versions that do not use the noexcept description may throw Bad_alloc exceptions when the memory allocation fails, while those used do not throw exceptions.

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 a three-step operation:

①.new expression calls the standard library function of operator new (or operator new[) above, which allocates a large, raw, unnamed memory space to store objects of a particular type (or an array of objects).
②. The compiler runs the appropriate constructors to construct these objects and pass in the initial values for them.
③. The object is allocated a space and is constructed to complete, returning a pointer to the object.

For the operator new function or operator new[] function, its return type must be void*, the type of the first parameter must be size_t and the parameter cannot contain a default argument. When the compiler calls the operator new or operator new[] function, the number of bytes required to store the specified object or an array of the specified object is passed to the size_t parameter.

2.delete expression

The standard library also defines several overloaded versions of the operator delete function, and the noexcept specifier indicates that a null pointer is returned instead of throwing a Bad_alloc exception when the memory allocation fails.

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 a two-step operation:

①. Performs a destructor for the object that the pointer refers to or an element in the array being referred to.
②. The standard library functions that the compiler calls operator delete (or operator delete[) free up memory space.

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

II, malloc and free

malloc and free are standard library functions in C/s + + and are used to request dynamic memory and release memory.

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

The malloc function accepts a size_t that represents the number of bytes to allocate, returns a pointer to the allocated space, or returns a null pointer if the allocation fails. The free function accepts a void*, which is a copy of the pointer returned by malloc and returns the associated memory to the system. Note: The malloc function does not call the constructor to initialize the memory, nor does the free function automatically invoke the destructor.

The difference between new and malloc, delete and free

int *p1 = new int;   The list is not initialized, so there is no initialization 
int *p2 = new int ();  Null initialization list, performing zero initialization, so initialize to 0 
int *p3 = new int (3);//Non-null initialization list, perform value initialization, so initialize to 3 
//NOTE: For built-in types that do not have constructors, new will not initialize with no Initial list (empty list "()" also counted) 
int *p4 = new int[100];   The allocation size is 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, malloc () is a library function.

②.new calls the constructor, and malloc () does not.

③.new Returns a pointer of the specified type, while malloc () returns void*.

④.new automatically calculates the space that needs to be allocated, and malloc () needs to manually compute the number of bytes.

⑤.new can be overloaded, while malloc () cannot.

Delete P1; 
delete [] p2; 
Free (p3); 
Free (p4);

The difference between delete and free:

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

②.Delete calls the destructor, and free () does not.

③.Delete can be overloaded, and free () cannot.

Summarize:

malloc and free are standard library functions in C/s + +, and new and delete are the operators of C + +. For objects that are not of the built-in data type, Maloc/free cannot meet the requirements of dynamic objects. Objects are created with the constructor automatically executed, and the destructor is automatically executed before the object dies. Because Malloc/free is a library function and not an operator, it is not possible to impose malloc/free on tasks that perform constructors and destructors without the compiler controlling permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.