In- depth C + + New/delete,malloc/free parsing
1.malloc and free are standard library functions for c++/c languages, and New/delete are operators of C + +. They can all be used to request dynamic memory and free memory
2. For objects of non-intrinsic data types, light Maloc/free cannot meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. By Malloc/free is a library function and not
arithmetic not within the control of the compiler, and the task of executing constructors and destructors cannot be imposed on Malloc/free.
3. Therefore, the C + + language requires an operator new that can perform dynamic memory allocation and initialization, with an operator delete that can perform cleanup and release of memory work. Note New/delete is not a library function.
4.c++ programs often call C functions, whereas C programs can only use Malloc/free to manage dynamic memory .
5.New can be thought of as the execution of malloc plus constructors. The new pointer is directly with the type information. The void* pointer is returned by malloc.
New Delete actually calls the Malloc,free function on the implementation
6.new object You can use it as an ordinary object, access it with a member function, do not directly access its address space; malloc allocates a memory area, accesses it with pointers, and can move pointers inside.
7.new establishes an object; Alloc allocates a piece of memory.
***************************************
Same point: can be used to request dynamic memory and free memory
***************************************
Different points:
(1) The operating objects are different.
malloc with Free is the standard library function for the c++/c language, New/delete is the operator of C + +. For objects that are not internal data classes, light Maloc/free cannot satisfy the requirements of dynamic objects. object to automatically execute the constructor while it is being created.
Object The destructor should be executed automatically before it dies. Because Malloc/free is a library function and not an operator, it is not possible to impose malloc/free on the tasks that execute constructors and destructors without the compiler's control permissions.
(2) The usage is also different.
The prototype of the function malloc is as follows:
void * malloc (size_t size);
Use malloc to request a block length integer type of memory, the program is as follows:
INT*P = (int *) malloc (sizeof (int) * length);
We should focus on two elements: "Type conversion" and "sizeof".
malloc the type of the return value is void *, so the type conversion is explicitly performed when malloc is called, and the void * is converted to the desired pointer type.
The malloc function itself does not recognize what type of memory to request, it only cares about the total number of bytes in memory.
The prototype of the function free is as follows:
void free (void * memblock);
Why is the free function not as complex as the malloc function? This is because the type of the pointer p and the amount of memory it refers to is known beforehand, and the statement free (p) frees the memory correctly. If p is a null pointer, then free
P No matter how many times the operation is done. If p is not a null pointer, then the free operation of P for two consecutive times causes the program to run incorrectly.
Key points of use of New/delete
Operator new is much simpler to use than the function malloc, 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 safety check functionality. For objects that are not internal data types, new initializes the initialization work while creating the dynamic object. If the object has more than one constructor, the new statement
There can also be many forms.
If you create an array of objects with new, you can only use the parameterless 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 while assigning the initial value 1
When releasing an array of objects with delete, be careful not to lose the symbol ' [] '. For example
delete[]objects; The right usage
deleteobjects; Incorrect use of
The latter is equivalent to delete objects[0], missing another 99 objects.
***************************************
1 new automatically calculates the space that needs to be allocated, and malloc needs to manually calculate the number of bytes
2 new is type-safe, and malloc is not, for example:
int* p = new Float[2]; Errors are indicated at compile time
int* p = malloc (2*sizeof (float)); Cannot indicate error at compile time
The new operator is made up of two steps, namely operator new and construct
3 operator new corresponds to malloc, but operator new can be overloaded, and you can customize the memory allocation policy without even allocating memory, even to non-memory devices. And malloc could do nothing.
4 new will call constructor, and malloc cannot; Delete will call destructor, and free cannot.
5 Malloc/free to library file support, New/delete do not.
In-depth C + + New/delete,malloc/free parsing