Differences between malloc and new

Source: Internet
Author: User

 Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory.

 For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.

 Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database.

Function.

 Memory leakage can be checked out for malloc or new. The difference is that new can indicate the row of the file, and malloc does not have this information.

Let's take a look at how malloc/free and new/delete implement dynamic memory management of objects.

Class OBJ
{
Public:
   OBJ (void) {cout <"initialization" <Endl ;}
   ~ OBJ (void) {cout <"Destroy" <Endl ;}

   Voidinitialize (void) {cout <"initialization" <Endl ;}

   Voiddestroy (void) {cout <"Destroy" <Endl ;}
};

Void usemallocfree (void)
{
   OBJ * A = (OBJ *) malloc (sizeof (OBJ )); // Apply for dynamic memory
   A-> initialize ();                      // Initialization

   //...

   A-> destroy (); // Clear the job
   Free ();      // Release the memory
}

Void usenewdelete (void)
{
   OBJ * A = newobj; // apply for dynamic memory and initialize

   //...

   Deletea;         // Clear and release the memory
}

 The class OBJ function initialize simulates the constructor function, and the function destroy simulates the destructor function. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. The usenewdelete function is much simpler.

 Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/Delete. Because the "object" of the internal data type does not have a process of structure and analysis

Malloc/free is equivalent to new/Delete.

  Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? This is because C ++ programs often call C functions, and C Programs can only use

Malloc/free manage dynamic memory.

If you use free to release the "New Dynamic Object", this object may cause program errors because it cannot execute the destructor. --- (Never used)

If you use Delete to release the "dynamic memory applied by malloc", theoretically

The program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.

**************************************** **************************************** *************

From high quality c ++/C Programming Guide
1. malloc and free are standard library functions of C ++/C, and new/delete are operators of C ++. They can be used to apply for dynamic memory and release memory.
2. For non-Internal data objects, using malloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct.

Function. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.
3. The C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function.
4. c ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory.

In short:

New is an operator. It has the same status as what "+", "-", "=.
Malloc is a memory allocation function for you to call.
New is a reserved word and does not need to be supported by header files.
Malloc requires support for the header file library function.

New creates an object,
Malloc allocates a piece of memory.
You can use a new object as a common object to access it using a member function instead of directly accessing its address space.
Malloc is allocated with a memory area, which can be accessed with a pointer and moved inside.

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.