Difference between malloc and new in C

Source: Internet
Author: User
1. New is the operator in C ++, and malloc is a function in C.

2. New not only allocates memory, but also calls class constructor. Similarly, delete calls class destructor, while malloc only allocates memory, does not initialize class members, or free does not call destructor.

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

4. Comparison of new and malloc Efficiency
New has three letters, malloc has six letters
New can be considered as the execution of the malloc and constructor.
The new pointer directly carries the type information.
Malloc returns the void pointer.

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, 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. 3, because
This C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can complete memory cleaning and release. Note that new/delete is not
Is a library function. 4. c ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory new
Is an operator, and what "+", "-", "= "... has the same status. malloc is a memory allocation function for you to call. new is a reserved word and does not require header file support
Support for the. malloc header file library function. New
An object is created. malloc allocates a memory. New objects. You can treat them as common objects and use member functions to access them. Do not directly access its address space.
Malloc is allocated with a memory area, which can be accessed with a pointer and moved inside.

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.

Pair
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.
Number. 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.

First, let's take a look at how malloc/free and new/delete implement dynamic memory management of objects. See the following example.

Class
OBJ
{

Public
:
OBJ (
Void
) {Cout
<
"Initialization"
<
Endl ;}

~
OBJ (
Void
) {Cout
<
"Destroy"
<
Endl ;}

Void
Initialize (
Void
) {Cout
<
"Initialization"
<
Endl ;}

Void
Destroy (
Void
) {Cout
<
"Destroy"
<
Endl ;}
};


Void
Usemallocfree (
Void
)
{
OBJ
*
A
=
(OBJ
*
) Malloc (sizeof (OBJ ));
//
Apply for dynamic memory


A
->
Initialize ();
//
Initialization

//
...


A
->
Destroy ();
//
Clear jobs


Free ();
//
Release memory


}


Void
Usenewdelete (
Void
)
{
OBJ
*
A
=

New
OBJ;
//
Apply for dynamic memory and initialize

//
...


Delete;
//
Clear and release memory


}

The class OBJ function initialize simulates the constructor function, and the function destroy simulates the destructor function. In the usemallocfree function,
Since malloc/free cannot execute constructor and destructor, you must call the member functions initialize and destroy to complete initialization and clearing. Function
Usenewdelete 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 internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to them.

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 to manage dynamic memory.
For example
If you use free to release the "New Dynamic Object", this object may cause program errors because the Destructor cannot be executed. 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.

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.