catalogue
1, C/C + + memory distribution
2, Dynamic memory management in C language
3, C + + memory management mode
4, operator new and operator delete functions
The difference between malloc/free and new/delete:
1, C/C + + memory distribution
1. In these areas, the pile is very large
2. From the figure, the stack is similar to the heap. The actual stack is very small, generally only 8M (under LINUX), so the recursive call depth is too deep, which will lead to stack overflow
3. The data segment and code segment are not very large, because there is not much data
int globalVar = 1; static int staticGlobalVar = 1; void Test() { static int staticVar = 1; int localVar = 1; int num1[10] = { 1, 2, 3, 4 }; char char2[] = "abcd"; char* pChar3 = "abcd"; int* ptr1 = (int*)malloc(sizeof (int)* 4); int* ptr2 = (int*)calloc(4, sizeof(int)); int* ptr3 = (int*)realloc(ptr2, sizeof(int)* 4); free(ptr1); free(ptr3); }
2, Dynamic memory management in C language
What is the difference between malloc/calloc/realloc?
calloc is equivalent to malloc+memset(0) open space + initialization
realloc is to expand the space of malloc or calloc
3, C + + memory management mode
class A { public: A(int a = 0) :_a(a) { cout << "A()" << endl; } ~A() { cout << "~A" << endl; } private: int _a; }; int main() { int* p1 = (int*)malloc(sizeof(int)); free(p1); int* p2 = new int; delete p2; A* p3 = (A*)malloc(sizeof(A)); free(p3); A*(p4) = new A; delete p4; return 0; }
For custom types, new/delete will not only open / free space, but also call constructors and destructors, while malloc and free will not
4, operator new and operator delete functions
new T -- 1: apply for space - > operator new (throw exception if the application for space fails) 2: Constructor
operator new is used to encapsulate malloc - "" failed to apply for memory, throwing an exception
delete T -- 1: call t's destructor 2: operator delete(p)
class Stack { public: Stack(int capacity = 4) :_a(new int[capacity]) , _size(0) , _capacity(capacity) { cout << "Stack(int capacity = 4)" << endl; } ~Stack() { delete[] _a; _size = _capacity = 0; cout << "~Stack()" << endl; } }; int main() { Stack st; Stack* ps = new Stack; delete ps; return 0; }
5, Common interview questions
The difference between malloc/free and new/delete:
Features and usage:
1. malloc and free are functions, and new and delete are operators
2. When malloc applies for space, it needs to manually calculate the space size and pass it. new only needs to follow the space type
3. The return value of malloc is void *, which must be forcibly rotated when used. New is not required, because new is followed by the type of space
Differences between underlying principles:
When applying for custom type objects, malloc/free will only open up the space and will not call the constructor and destructor. new will call the constructor to initialize the object after applying for the space, and delete will call the destructor to clean up the resources in the space before releasing the space
How to handle errors:
When malloc fails to apply for space, it returns NULL, so it must be NULL when using it. New does not need it, but new needs to catch exceptions
Memory leak:
int main() { char* p = new char[1024 * 1024 * 1024]; return 0; }
There is a memory leak in the above program, 1G at a time, but multiple leaks seem to have no impact on our system
After a process ends normally, the mapped memory will be released, so we did not actively release the above program, but the process ends, so it seems that there is nothing wrong with memory leakage? Because the process ends normally, it will be released.
Not really:
First: if the process does not end normally, there may be some resources that have not been released.
Second: the server program that runs for a long time will stop only when it is upgraded
Memory leaks can lead to less and less available memory, slower and slower programs, and even hang up - accidents.
Third: Internet of things devices: floor sweeping robots, etc. the memory is very small and can not withstand the toss of memory leakage.
C + + needs to actively release memory. java does not need to actively release memory. There is a garbage collector in the java background, which takes over the memory release
How to prevent memory leaks:
1. Smart pointer
2. Memory leak detection tool : memory leak detection tool