C++ Notes - What is the second memory management?

 

Table of contents

1. Memory distribution

2. Comparison of C language and C++ memory management

1. C language

malloc/calloc/realloc和free

2. C++ memory management method

2.1 new/delete operation built-in type

2.2 new and delete operation custom type

3. operator new and operator delete functions 

4. Implementation principle of new and delete

4.1 Built-in types

Three, locate the new expression

4. Memory leak

1. What is a memory leak:

2. The hazards of memory leaks:

3. Classification

4. How to avoid memory leaks



1. Memory distribution



 

CCC memory management

 (picture from the Internet)

1. The stack is also called the stack, non-static local variables/function parameters/return values, etc., the stack grows downward.
2. The memory-mapped segment is an efficient I/O mapping method 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 - store global data and static data.
5. Code segment--executable code/read-only constant



 

2. Comparison of C language and C++ memory management



1. C language


malloc/calloc/realloc和free

void Test()
{
	int* p1 = (int*)malloc(sizeof(int));
	free(p1);
	
	int* p2 = (int*)calloc(4, sizeof(int));
	int* p3 = (int*)realloc(p2, sizeof(int) * 10);
	
	free(p3);
}

2. C++ memory management method



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


2.1 new/delete operation built-in type

 To apply and release space for a single element, use new and delete operators, apply and release continuous space, use new[] and
delete[]

2.2 new and delete operation custom type

class Test
{
public:
	Test()
		: _data(0)
	{
		cout << "Test():" << this << endl;
	}
	~Test()
	{
		cout << "~Test():" << this << endl;
	}
private:
	int _data;
};
void Test2()
{
	// 申请单个Test类型的对象
	Test* p1 = new Test;
	delete p1;
	// 申请10个Test类型的对象
	Test* p2 = new Test[10];
	delete[] p2;
}

When applying for a custom type of space, new will call the constructor, delete will call the destructor, but malloc and free will not


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 actually applies for space through malloc. If malloc
successfully applies for space, it will return directly. Otherwise, it will execute the countermeasures provided by the user for insufficient space. If the user provides such measures, it will continue to apply, otherwise it will throw an exception. operator delete finally releases space through free


 

4. Implementation principle of new and delete


4.1 Built-in types

If you apply for a built-in type of space, new and malloc, delete and free are basically similar, the difference is: new/delete applies for and releases the space of a single element, new[] and delete[] apply for continuous space, Moreover, new will throw an exception when it fails to apply for space, and malloc will return NULL.

4.2 Custom types

The principle of new
1. Call the operator new function to apply for space 2. Execute the constructor
on the applied 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 space of the object


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 applied 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, and actually call operator delete in operator delete[] to release space



Three, locate the new expression



Call the constructor to initialize an object in the allocated original memory space

Usage scenario:
positioning new expressions are generally used in conjunction with memory pools in practice. Because the memory allocated by the memory pool is not initialized, if it is an object of a custom type, it needs to use the definition expression of new to explicitly call the constructor for initialization. 

Format:
new (pointer) type or new (pointer) type (type initialization list)
 



4. Memory leak



1. What is a memory leak:

A memory leak is a situation in which a program fails to free memory that is no longer in use due to inadvertence or error. A memory leak does not
refer to the physical disappearance of memory, but rather the loss of control over the memory segment due to a design error after the application allocates a certain segment of memory, resulting in a waste of memory.


2. The hazards of memory leaks:

Memory leaks occur in long-running programs, which have a great impact, such as the operating system, background services, etc. Memory leaks will
lead to slower and slower responses, and eventually freeze

3. Classification

Heap memory leak (Heap leak)

Heap memory refers to a piece of memory allocated from the heap through malloc / calloc / realloc / new, etc. during program execution as needed, and must be deleted by calling the corresponding free or delete after use. Assuming that the design error of the program causes this part of the memory to not be released, then this part of the space will no longer be used in the future, and Heap Leak will occur.


system resource leak


Refers to the resources allocated by the system used by the program, such as sockets, file descriptors, pipes, etc., which are not released using the corresponding functions, resulting in a waste of system resources, which can seriously lead to reduced system performance and unstable system execution

4. How to avoid memory leaks


1. Good design specifications in the early stage of the project, develop good coding standards, and match the requested memory space to release.
2. Use RAII ideas or smart pointers to manage resources.
3. If something goes wrong, use the memory leak tool to detect it.


 

 (The pictures in the text come from the Internet)

 

Guess you like

Origin blog.csdn.net/MuqiuWhite/article/details/129555286