The memory mechanism of C and C ++ is very similar, but there are still some differences. Let's take a look at the differences between them.
(1) c memory mechanism
1. STACK: local variables (including function arguments) in the function are allocated and released by the compiler. The function ends and the stack variable fails. 2. Heap:
Program Use malloc ()/calloc ()/realloc () to allocate space and free () to release the requested space. If the programmer forgets free (), it will cause memory leakage. When the program ends, it may be recycled by the operating system, and may remain occupied until it is shut down. 3. Global/static area: the storage area of global and static variables. Once the program is compiled, this area will exist. And
The global and static variables initialized in the C language and uninitialized variables are placed in two adjacent areas. (In c ++, the compiler does not differentiate global variables and static variables because they are automatically initialized and assigned values ). Because global variables occupy memory space and are not easy to maintain, we recommend that you use less. Release when the program ends. 4. C-style String constant storage area: stores string constants and releases them when the program ends.
Some books are also called symbol tables. 5. ProgramCodeZONE: The region where the binary code of the program is stored. (2) c ++ memory mechanism 1. STACK ): The compiler is responsible for allocating and releasing local variables (including function arguments) in the function. The function ends and the stack variable becomes invalid. 2.Heap ): What is different from C here is that the heap is released by the memory applied by new and deleted. 3.Free storage ): The space allocated by the programmer with malloc ()/calloc ()/realloc () is released by free. If the programmer forgets free (), it will cause memory leakage. When the program ends, the operating system may be recycled, and it may remain occupied until it is shut down. Corresponds to the heap MECHANISM OF C.
4. Global static area ):
Global variables and static variables are stored in the region. Once the program is compiled, the region exists.In C ++, the compiler automatically assigns values to global variables and static variables, so initialization and uninitialized variables are not distinguished.Because global variables occupy memory space and are not easy to maintain, we recommend that you use less. Release when the program ends.
5. Constant storage zone:
This is a special storage area dedicated to storing constants that cannot be modified (if you use abnormal means to change it, of course, it is also possible ).
Note:
To verify the above, the option-STD = c89 must be added during compilation. Why? If the-STD = c89 option is not added, the current GCC version is generally c99 standard, and many c ++ features are added to c99. This verification is affected.
For compiler implementation problems, Some compilers treat global or static variables initialized to 0 as uninitialized. Probably because it is not initialized, the compiler will automatically initialize it to zero!