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 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.
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.
The implementation of new Delete actually calls the malloc and free functions.
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. alloc 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, 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, 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 needs to manually calculate the number of bytes
2 new is type-safe, but 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 specified during compilation.
New operator consists of two steps: Operator new and construct
3 operator new corresponds to malloc, but operator new can be reloaded. You can customize memory allocation policies, or even do not allocate memory, or even allocate it to non-memory devices. Malloc is powerless.
4 New will call constructor, but malloc cannot; Delete will call destructor, but free cannot.
5 malloc/free requires support for database files, while new/delete does not.
New and malloc