Difference between new and malloc

Source: Internet
Author: User

1. malloc and free are c ++/CStandard library functions, New/delete is C ++'sOperator. They can be used to apply for dynamic memory and release memory.

2. For non-Internal data objects, the use of 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. Because malloc/free is a library function rather than an operator, it is not controlled by the compiler and cannot impose the tasks of executing constructor and destructor on malloc/free.

3. 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:New/delete is not a Database Function.
4. c ++ProgramC functions are often called, while C Programs can only use malloc/free to manage dynamic memory.
5.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.

New/delete actually calls the malloc and free functions in implementation.

6. 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 allocates a memory area, you can use the pointer and move the pointer in it.

7.New creates an object, and malloc allocates a piece of memory.

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

Similarities: Can be used to apply for dynamic memory and release memory

Differences:
(1) The operation objects are different..
Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. For non-Internal data objects, the use of 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. Since malloc/free is a library function rather than an operator, it is not controlled by the compiler and cannot impose malloc/free on tasks that execute constructors and destructor.

(2) The usage is also different..
The following is a prototype of the malloc function:
Void * malloc (size_t size ); 
Use malloc to apply for an integer-type memory with a length. The program is as follows:
Int * P = (int *) malloc (sizeof (INT) * length );
We should focus on two elements: "type conversion" and "sizeof ".
The type returned by malloc is void *. Therefore, you must explicitly convert the type when calling malloc and convert void * to the required pointer type.
The malloc function does not recognize the type of memory to be applied for. It only cares about the total number of bytes in the memory.

The prototype of function free is as follows:
Void free (void * memblock ); 
Why isn't the free function as complicated as the malloc function? This is because the pointer p type and the memory capacity it refers to are known in advance, and the statement free (p) can correctly release the memory. If P is a null pointer, then free

P is normal no matter how many operations are performed. If P is not a null pointer, the free operation on P will cause a program running error.

Usage of new/delete
The new operator is much easier to use than the malloc function, for example:
Int * P1 = (int *) malloc (sizeof (INT) * length );
Int * P2 = new int [length];
This is because new has built-in sizeof, type conversion, and type security check functions. For non-Internal data objects, new completes initialization while creating dynamic objects. If an object has multiple constructors, the new statement can also have multiple forms.

If you use new to create an object array, you can only use the non-parameter constructor of the object. For example
OBJ * objects = new OBJ [100]; // create 100 Dynamic Objects
Cannot be written
OBJ * objects = new OBJ [100] (1); // create 100 dynamic objects and assign initial value 1
When releasing an object Array Using Delete, do not lose the symbol '[]'. For example
Delete [] objects; // correct usage
Delete objects; // incorrect usage
The latter is equivalent to delete objects [0], and 99 Other objects are missing.

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

1 new automatically calculates the space to be allocated, while malloc manually calculates the number of bytes
2 new is type-safe, whereas malloc is not. For example:
int * P = new float [2]; // indicates an error during compilation
int * P = malloc (2 * sizeof (float )); // The error cannot be identified during compilation.
new operator consists of two steps, operator new and construct
3 operator new corresponds to malloc, but operator new can be reloaded and can be customized for memory allocation policies, the memory is not even allocated, or even allocated to non-memory devices. Malloc is powerless.
4 New will call constructor, but malloc cannot. Delete will call destructor, instead of free.
5 support for malloc/free library files, new/delete is not required .

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.