[C++] Basic operations of memory management, implementation principles of new and delete, and operator new and operator delete functions


foreword

The division of memory in the program:
insert image description here

  1. The stack is also called the stack – non-static local variables/function parameters/return values, etc., the stack grows downward.
  2. Memory-mapped segments are efficient I/O mappings for loading a shared dynamic memory bank. Users can use the system interface to create shared shared memory for inter-process communication.
  3. The heap is used for dynamic memory allocation when the program is running, and the heap can grow upwards.
  4. Data segment – ​​stores global and static data.
  5. Code Segment – ​​Executable code/read-only constants.

One, new, delete operation built-in type

The C language memory management method can continue to be used in C++, but it is powerless in some places, and it is more troublesome to use, so C++ has proposed its own memory management method: dynamic memory management through new and delete operators.
insert image description here
insert image description here
insert image description here

Note: To apply for and release the space of a single element, use the new and delete operators, to apply for and release continuous space, use new[] and delete[], note: use them together.

2. new/delete manipulates custom types

insert image description here

insert image description here

3. operator new and operator delete functions

new and delete are operators for users to apply and release dynamic memory . operator new and operator delete are global functions provided by the system . new calls the operator new global function at the bottom layer to apply for space, and delete uses the operator delete global function at the bottom layer to release space .

operator new: This function actually uses malloc to apply for space . When malloc successfully applies for space, it returns directly; if the space application fails, try to implement countermeasures for insufficient space. If the countermeasures are set by the user, continue to apply, otherwise an exception is thrown .
operator delete: This function finally releases space through free

4. New/delete implementation principle

The principle of new

  1. Call the operator new function to apply for space
  2. Execute the constructor on the requested space to complete the construction of the object

The principle of delete

  1. Execute the destructor on the space to complete the cleanup of resources in the object
  2. Call the operator delete function to release the object's space

The principle of new T[N]

  1. Call the operator new[] function, and actually call the operator new function in operator new[] to complete the application of N object spaces
  2. Execute the constructor N times on the requested space

The principle of delete[]

  1. Execute N times of destructors on the released object space to complete the cleanup of resources in N objects
  2. Call operator delete[] to release space, actually call operator delete in operator delete[] to release space

insert image description here

insert image description here
insert image description here

4. The difference between malloc/free and new/delete

What malloc/free and new/delete have in common is that they all apply for space from the heap and need to be released manually by the user. The differences
are:

  1. malloc and free are functions, new and delete are operators
  2. The space requested by malloc will not be initialized, but new can be initialized
  3. When malloc applies for space, you need to manually calculate the size of the space and pass it on. New just needs to follow it with the type of space. If there are multiple objects, specify the number of objects in []
  4. The return value of malloc is void*, which must be forced when used, and new does not need it, because new is followed by the type of space
  5. When malloc fails to apply for space, it returns NULL, so it must be judged as empty when using it, new does not need it, but new needs to catch exceptions
  6. When applying for a custom type of object, malloc/free will only open up space, and will not call the constructor and destructor, while new will call the constructor to complete the initialization of the object after applying for space, and delete will call the destructor before releasing the space Complete the cleanup of resources in the space

Guess you like

Origin blog.csdn.net/m0_74774759/article/details/131074378