Chaos IN C ++ :: Dynamic Resource Management Trap

zhaozj2021-02-16  51

Difficult: First read the following code, then answer the following questions.

Struct a

{};

int main ()

{

A * p = new a;

P-> ~ a ();

Free (p);

}

Question: What is the difference between new / delete and malloc / free?

A: New / Delete will initialize and destroy the objects to initialize and destroy the objects in the allocated memory, but Malloc / Free cannot. More importantly, New / Delete and Malloc / Free cannot be mixed.

NEW is allocated in the free storage area, and the delete is responsible for the release of the free storage area.

Malloc is allocated in the heap, and Free is responsible for release.

The access method of free storage and stacks is different, or it is related to the platform, if it is mixed, then it will cause the program to crash. There may be no problems on some compilers, but always believe this is wrong, not portable.

Now, is there a problem with the code?

转载请注明原文地址:https://www.9cbs.com/read-19340.html

New Post(0)