The difference and connection between C++ memory distribution & malloc-free-new-delete

Table of contents

1. Memory distribution

1.1 Memory distribution map:

 1.2 Why should bss and data be distinguished?

1.3 What is the difference between the heap and the stack

 二、malloc、free;new、delete

2.1 How are new and delete implemented, and the similarities and differences between new and malloc

2.2 Since there is malloc/free, why does C++ still need new/delete


1. Memory distribution

1.1 Memory distribution map:

 1.2 Why should bss and data be distinguished?

When the program is compiled, no space will be allocated for the data in the .bss section , but only the size of the space required for the data will be recorded. When the program is executed, memory will be allocated for the data in the .bss segment. In this way, part of the memory space can be saved, further reducing the size of the executable program.

1.3 What is the difference between the heap and the stack

The heap is a section of memory dynamically opened up by malloc and new , which is managed and released by the programmer himself. The stack is memory created and managed by the compiler itself, and is generally used to store function parameters and local variables.

Due to the frequent development and release of heap space, there will be problems of memory fragmentation.

The growth space of the heap is upward, and the address is getting larger and larger, while the stack is downward, and the address is getting smaller and smaller. The stack space is very small, only 8M, while the heap has 4G.

 二、malloc、free;new、delete

2.1 How are new and delete implemented, and the similarities and differences between new and malloc

new and delete are the encapsulation of malloc and free . When new an object, it will first call malloc to allocate space, and then execute the object's constructor to initialize the space. delete will first call the object's destructor, and then call free to release the space. block space.

The biggest difference between them is 1. new will call the constructor to initialize the space. 2. new does not need to specify the size of the resource. 3. The return value of new is a pointer of object type, while malloc is a pointer of void *. 4. If the call of new fails, an exception will be thrown, and if the call of malloc fails, NULL will be returned. 5. new is an operator, malloc is a function, and operator new can be overloaded.

2.2 Since there is malloc/free, why does C++ still need new/delete

These four functions are used to apply and release memory. new/delete mainly encapsulates malloc/free so that it can better apply for and release C++ object resources , otherwise you have to malloc a space, and then manually call the constructor.

And malloc/free is a compiled library function, which is also compatible with C language and cannot be easily modified, so it is encapsulated to implement new/delete.

Guess you like

Origin blog.csdn.net/flyingcloud6/article/details/129839081