[C ++ in-depth analysis] 40, the difference between new and malloc

1 The difference between new and malloc

1. The attribute
new / delete is a C ++ keyword. malloc / free is a library function.

2. The parameter
new allocates memory in units of specific types. malloc needs to explicitly indicate the number of bytes of memory required.

3. When the
memory allocation of the return type new operator is successful, the pointer of the object type is returned, and no type conversion is required.
The malloc function succeeds and returns void *, which needs to be converted to the type we need by forced type.

4. Allocation failure When
new memory allocation fails, bac_alloc exception will be thrown. malloc returns NULL when it fails to allocate memory.

5. Constructing the destructor function
new can trigger the call of the constructor and can be initialized when applying for space. delete first calls the destructor, destroys the object before releasing the memory space.

malloc only allocates the required memory space, and free only returns the allocated memory space. Unable to complete the construction and destruction of class objects, not suitable for object-oriented development.

6. Overloading
C ++ allows overloading of the new / delete operator, and malloc does not allow overloading.

7. The memory area
new operator dynamically allocates memory space for objects from the free storage area, and the malloc function dynamically allocates memory from the heap. The free storage area is an abstract concept of C ++ based on the new operator. Any memory application made through the new operator is the free storage area. The heap is a special memory maintained by the operating system. The free storage area is not equal to the heap, and the object applied by new may not be located in the heap.

If you define a pointer variable above the function, then apply for a piece of memory in this function to let the pointer point to it. In fact, the address of this pointer is on the stack, but the content it points to is on the heap!

Programming experiment: the difference between new and malloc

// 40-1.cpp
#include<iostream>
using namespace std;
class Test
{
    int* mp;
public:
    Test()
    {
        cout << "Test::Test()" << endl;
        mp = new int(100);
        cout << *mp << endl;
    }
    ~Test()
    {
        delete mp;
        cout << "Test::~Test()" << endl;
    }
};
int main()
{
    Test* pn = new Test;
    Test* pm = (Test*)malloc(sizeof(Test));
    delete pn;
    free(pm);
    return 0;
}
  • new triggers the constructor, applying for space before initializing. delete first calls the destructor, destroys the object before releasing the memory space.
  • malloc only allocates memory, and free only returns allocated memory space. Unable to complete the construction and destruction of class objects.
  • So line 22 calls the constructor and line 24 calls the destructor. Lines 23 and 25 will not call the constructor destructor

Compile and run

$ g++ 40-1.cpp -o 40-1
$ ./40-1
Test::Test()
100
Test::~Test()

2 New / delete mixed with malloc / free

What if we mixed them?

2.1 new application for free release-memory leak

If the 24th line of the above code is changed to free (pn); that is, the free space applied by new will cause a memory leak. If space is applied in the constructor, free will not trigger the destructor and will not release the constructor. Space, causing memory leaks. The object is not destroyed, only the space is released.

Compile and run, you can see that the destructor is not called.

$ g++ 40-1.cpp -o 40-1
$ ./40-1
Test::Test()
100

2.2 malloc application delete release-destroy non-existent objects

If you change line 25 of the above code to delete pm; delete will call the destructor, malloc does not call the constructor to create the object, but the destructor is called here, which may cause problems.

Compile and run, there is no error reported here, but the destructor release of resources may bring far-reaching effects, do not use it like this.

$ g++ 40-1.cpp -o 40-1
$ ./40-1
Test::Test()
100
Test::~Test()
Test::~Test()

Therefore: new application space must be released using delete, malloc application resources must be free

3 Summary

1. new will trigger the constructor, delete will trigger the destructor
2. malloc / free is not suitable for object-oriented development

Published 298 original articles · praised 181 · 100,000+ views

Guess you like

Origin blog.csdn.net/happyjacob/article/details/104484218