One, foreword

If you have studied C, you will know that in C, malloc() is used to dynamically allocate memory, and free() is used to free memory. These two functions are still available in C++, but C++ adds two new keywords: new and delete. New is used to dynamically allocate memory, and delete is used to free memory. We can use it like this:

int* p = new int; delete p; Int * pArray= new int[20]; delete[] pArray;

The new operator extrapolates the required space based on the following data type. Delete []; delete[]; delete[]; delete[]; delete[]; New application space when they apply for 4 bytes, used to store the number of objects, when the return address is 4 bytes, backward migration when the delete will see the number of objects on the memory, according to the number of the destructor call several times, so as to fully clean up all objects take up memory, so for the built-in types if new [], Use DELETE to release, no effect; However, if a custom type is used, the destructor is called only once with the delete release, and there may be cases where the object is not destructed.

New allocates memory on a free store (either on the heap or on a static store, depending on the details of the operator new implementation, depending on where it allocates space for objects), which must be freed manually, or the operating system must recycle it until the program finishes running. To avoid memory leaks, the new and delete, new[], and delete[] operators should come in pairs, and should not be mixed with the C malloc() and free() operators. In C++, it is better to use new and delete to manage memory than C’s malloc and free. Constructors and destructors are called automatically, so that memory is freed at the end of a program’s run.

Malloc = new, delete = new, delete = new, delete = free

1, new and delete

When new operates, memory is allocated through the operator new function, and one or more constructors are called to build objects for the allocated memory. New has built-in sizeof, type conversion, and type safety checking. For objects that are not of internal data types, new creates a dynamic object and initializes it. If the object has multiple constructors, then the statement of new can also take multiple forms. Such as:

int *p= new int[100](1); // Create 100 dynamic objects and assign an initial value of 1

During delete, one or more destructors are called for the memory to be freed, and memory is freed through the operator delete function.

2, malloc and free

The function prototype of malloc is

void *malloc(long NumBytes)

We can see from the prototype that when we call malloc, we need to specify the number of bytes of allocated memory. On successful allocation, we return the corresponding pointer to the starting position of the allocated memory block. Otherwise, the null pointer is returned.

The function prototype of free is:

void free(void *p)

To return the space previously allocated with malloc to the program or operating system, which frees the memory space claimed by malloc. The free function frees the memory that the pointer points to (not the pointer itself, which does not delete the pointer itself), where the pointer must point to the first address of the freed memory space. If p is not a NULL pointer, then operating free on p twice in a row will cause the program to run in error.

3. Essential difference

1) new allocates memory automatically. 2) new allocates memory automatically. 3) For user-defined objects, malloc/free is a standard library function. Malloc /free does not meet the requirements of dynamically managing objects. Because malloc/free is a library function and not an operator, it is no longer under the control of the compiler, and the task of executing constructors and destructors cannot be imposed on malloc/free.

New /delete can be overloaded, malloc/free cannot; The underlying implementation of new/delete is based on malloc/free; After malloc allocates memory and finds that it is not enough, it can be expanded or reduced by realloc function.

When malloc applies for memory and returns memory address, it is necessary to check NULL, because the application fails to return NULL; New throws an exception bac_alloc when memory allocation fails. Therefore, we can add an exception handling mechanism to new, such as:

int* p = new(std::nothrow) int[10];

Why do we keep malloc and free? C++ programs often call C functions, and C programs can only manage dynamic memory using malloc/free. If you use free to free a dynamic object created by new, the object will cause a program error because the destructor cannot be executed. If the DELETE frees the dynamic memory requested by malloc, there may be no error, but the program is not readable.