Blog.csdn.net/Damn_Yang/a…
Malloc /free and new/delete 2. Design a class that can only create objects on the heap 3. Design a class that can only create objects on the stack 4. 5. Memory leaks
1. Malloc /free and new/delete
Malloc/Free and new/delete have one thing in common: they both request space from the heap and need to be released manually.
The differences are:
1. Malloc and free are functions; new and delete are operators
2. The space requested by malloc cannot be initialized, while new can be initialized
3. When applying for space, malloc needs to manually calculate the size of the space and pass it. New only needs to follow the type of the space
4. Malloc returns void*, which must be strong when used. New is not needed because new is followed by the space type
5. When malloc fails to apply for space, it returns NULL, so it must be nulled. New is not required, but new needs to catch exceptions
6. Malloc /free can only apply for space of built-in type, but cannot apply for space of custom type, because it does not call the constructor and destructor, while new can. New will call the constructor to complete the object construction after applying for space, and delete will call the destructor to complete the clearing of resources in the space before releasing space
7. The space malloc requested must be on the heap, not necessarily new, because operator new can be reimplemented
8. New/DELETE is slightly less efficient than malloc/free because the underlying new/ DELETE encapsulates malloc/ Free
2. Design a class that can only create objects on the heap
Method: Privatize the constructor
1. Make class constructors private and copy constructors private. Prevent others from calling copy to generate objects on the stack
2. Provide a static member function in which the heap is created
Here is the code implementation
#include
using namespace std; class HeapOnly { public: static HeapOnly* CreateObject() { return new HeapOnly; } void Print() { cout << “HeapOnly ” << this << endl; } private: HeapOnly()// Constructor private {} // prevent copy of HeapOnly(const HeapOnly&); }; int main() { HeapOnly::CreateObject()->Print(); return 0; } 3. Design a class that can only create objects on the stack
We can only create objects on the stack, not on the heap, so we can only create objects on the stack, so we can only create objects on the stack, so we can only create objects on the heap
#include
using namespace std; class StackOnly { public: StackOnly() {} void print() { cout << “StackOnly ” << this << endl; } private: void* operator new(size_t size); void operator delete(void* p); }; int main() { StackOnly obj; obj.print(); return 0; } 4. Singleton mode (often used in interviews)
This in my another blog has a detailed introduction to https://blog.csdn.net/Damn_Yang/article/details/83755390
5. Memory leaks
1. What is memory leak
Memory leak refers to the failure of a program to free memory that is no longer used because of negligence or error. A memory leak is not the physical disappearance of memory, but rather the loss of control over memory allocated by an application due to a design error.
2. Classification of memory leaks
In C/C++ programs we are generally concerned with two types of memory leaks
Heap memory leak
Heap memory refers to the program execution according to the need to be allocated by malloc/calloc/realloc/new from the heap allocation of a block of memory,
After using it, you must call the corresponding free or delete to delete it. If a design error in the program results in this portion of memory not being freed, then this portion of memory can no longer be used, resulting in a heap memory leak
System Resource Leakage
It refers to the use of resources allocated by the system, such as sockets, file descriptors, pipes, etc., which are not released by the corresponding functions, resulting in a waste of system resources, which can seriously reduce system performance and unstable system execution
How do I detect memory leaks
Under Linux memory leak detection: blog.csdn.net/gatieme/art…
Using a third-party tools under Windows: blog.csdn.net/GZrhaunt/ar…
Other tools: www.cnblogs.com/liangxiaofe…
How do I avoid memory leaks
1. Good design specifications at the early stage of the project, develop good coding specifications, and remember to release the memory space applied for matching. Ps: This ideal state. But if you run into an exception, even if you’re careful, you can still have a problem. You need the next smart pointer to manage it.
2. Use RAII ideas or smart Pointers to manage resources.
3. Some internal company specifications use private memory management libraries implemented internally. The library comes with memory leak detection options.
4. Use the memory leak tool to detect faults.
Ps: But many tools are unreliable or expensive.
To sum up:
Memory leaks are very common and can be solved in two ways: 1. Such as intelligent Pointers. 2, after checking the wrong type. Such as leak detection tools. — — — — — — — — — — — — — — — — — — — — – the author: Damn_Yang source: CSDN, blog.csdn.net/Damn_Yang/a… Copyright notice: This article is the blogger’s original article, reprint please attach the blog link!