The memory area of C++ is divided into five areas, namely heap , stack , free storage area , global/static storage area and constant storage area .
(1) Stack is a piece of memory space that exists in a certain scope. For example, when you call a function, the function itself forms a stack for the parameters it receives and the address it returns. Any variable declared within the function body, the memory block used by it is taken from the above stack.
(2) Heap (heap), or system heap, refers to a piece of global memory space provided by the operating system, and the program can obtain a number of blocks (blocks) from a certain dynamic allocation (dynamically allocated).
(3) The free storage area is a memory block allocated by malloc, etc., which is very similar to the heap and is released by free.
(4) Global/static storage area, global variables and static variables are allocated to the same memory (in C language, global variables are divided into initialized and uninitialized, in C++ there is no such distinction).
(5) Constant storage area, which is a special storage area, which stores constants and cannot be modified.
The heap and the free storage area can be regarded as the same area, malloc is called in the underlying implementation code of new, and new can be regarded as an intelligent advanced version of malloc. The growth direction of the heap and the stack in memory is opposite. The growth direction of the stack is downward, that is, it grows in the direction of decreasing memory addresses; while the growth direction of the heap is upward, and it grows in the direction of increasing memory addresses.
The following examples are used to understand the specific concepts:
class Complex (...); ... { Complex c1(1,2); Complex* p = new Complex(3); }
The space occupied by c1 comes from stack. Complex(3) is a temporary object whose space is dynamically allocated by new from heap and pointed to by pointer p.
char *p1 = "123456"; char *p2 = (char *)malloc(10);The pointers p1 and p2 themselves are stored in the stack, the object pointed to by p1 is stored in the constant storage area, and the object pointed to by p2 is stored in the heap.
Lifetime of stack objects
class Complex {...}; ... { Complex c1(1,2); }
c1 is the stack object, and its life ends when the scope ends. The object in this scope is also called auto object, because it will be automatically cleaned up (the destructor will be automatically called).
Lifetime of static local objects
class Complex {...}; ... { static Complex c2(1,2); }
c2 is a static object, its life still exists after the scope ends, until the end of the entire program.
lifetime of global objects
class Complex {...}; ... Complex c3(1,2); intmain() { ... }
c3 is the global object (variable outside any curly braces) whose life ends after the entire program ends. It can also be regarded as a static object whose scope is the entire program.
Lifetime of heap objects
class Complex {...}; ... { Complex* p = new Complex; ... delete p; }
p refers to the heap object, whose life ends when it is deleted. If there is no delete p statement, there will be a memory leak, because when the scope ends, the heap object pointed to by p still exists, but the life of the pointer p ends, and p can no longer be seen outside the scope (there is no chance to delete p).
new is to allocate memory first, and then call the constructor ; while delete is to call the destructor first, and then release the memory.