Dynamic memory management - underlying exploration of new and delete

Original link: https://blog.csdn.net/qq_38646470/article/details/79824464
Column link: https://blog.csdn.net/column/details/20026.html
[TOC]
#1.new/delete and The relationship between operator new/operator delete and malloc/free
first write a chestnut to see the usage of new and delete:

int main()
{
    int *p0 = new int;
    int *p1 = new int(10);
    int *p2 = new int[10];
    int *p3 = (int *)malloc(sizeof(int)*10);

    delete p0;
    delete p1;
    delete[] p2;
    free(p3);
    return 0;
}

write picture description here
Take a look at the relationship between them:
write picture description here

#2. The underlying processing mechanism of new
write picture description here

#3. The difference between new/delete and malloc/free
1) malloc/free is the standard library function of C/C++ language, new/delete is the operator of C++

    malloc and free are standard library functions of C++/C language, and new/delete are operators of C++. For objects of non-internal data classes, maloc/free alone cannot meet the requirements of dynamic objects. When an object is created, it automatically executes the constructor, and before the object dies, it automatically executes the destructor. Since malloc/free is a library function and not an operator, it is not within the control of the compiler, and the task of executing the constructor and destructor cannot be imposed on malloc/free.
2) The usage is also different.

The prototype of the function malloc is as follows:
void malloc(size_t size);
● malloc applies for a block with a length of size bytes
● the type of the return value of malloc is void
, so when calling malloc, it is necessary to explicitly perform type conversion, and convert void to all Required pointer type.
● The malloc function itself does not recognize the type of memory to be applied for, it only cares about the total number of bytes of memory.
The prototype of the function free is as follows:
void free( void
memblock );
● If p is a NULL pointer, then no matter how many times free operates on p, there will be no problem.
● If p is not a NULL pointer, then free operation on p twice will cause the program to run incorrectly.

The operator new is much simpler to use than the function malloc, for example:
int p1 = (int )malloc(size);
int *p2 = new int[size];
● new has built-in sizeof, type conversion and type safety checking.
● For objects of non-internal data types, new completes the initialization while creating the dynamic object.
● If the object has multiple constructors, the new statement can also have multiple forms.

When using delete to free the object array, be careful not to lose the symbol '[]'. For example
● delete []p2; // correct usage
● delete p2; // incorrect usage
The latter is equivalent to delete p2[0], missing another size-1 object.

After all, we have to understand that the bottom layer of new is to call malloc, and the bottom layer of delete is to call free. When we use it in C++, we must understand the difference and connection between the two. When using new, it must be released with delete, and when using malloc, it must be released with free, so that the matching prevents crashes outside the program.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324513789&siteId=291194637