Discuss: program in memory allocation of constant local variables global variables program code problems

  • 2020-04-02 00:50:41
  • OfStack

I. in c, it is divided into these storage areas
Stack - release is automatically allocated by the compiler
2. Heap - generally released by the programmer, if the programmer does not release, the end of the program may be recovered by the OS
3. Global area (static area), the storage of global variables and static variables is put together, the initialized global variables and static variables are in one area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. - program end release
There is also a special place for constants. - program end release

The variables defined in the function body are usually on the stack, and the functions that allocate memory such as malloc, calloc, and realloc are allocated on the heap. The static variable defined outside of all functions is the global quantity, which is stored in the global area (the static area) no matter where it is after adding the static modifier. The static variable defined outside of all functions is valid in this file, and cannot be used for extern to other files. In addition, strings such as "adgfdf" in the function are stored in the constant area. Such as:


//main.cpp 
int a = 0;      //Global initialization area
char *p1;      //Global uninitialized area
void main() 
{ 
    int b;            //The stack area
    char s[] = "abc"; //The stack area
    char *p2; //The stack area
    char *p3 = "123456"; //P3 in the stack area; & have spent & have spent "1234560" in the constant area, & PI;

    static int c =0;      //Global (static) initialization area
    p1 = (char *)malloc(10); 
    p2 = (char *)malloc(20); //The resulting 10 - and 20-byte regions are allocated in the heap area & NBSP;
    strcpy(p1, "123456");    //"1234560" is placed in the constant area, and the compiler may optimize it to the same place as "123456" pointed to by p3.
} 

In C++, memory is divided into five areas: heap, stack, free storage, global/static storage, and constant storage
1. A stack is a storage area for variables that are allocated by the compiler when needed and are automatically clear when not needed. The variables inside are usually local variables, function parameters, etc.
2. The heap is the block of memory allocated by new, which is not handled by the release compiler and controlled by our application. A new usually corresponds to a delete. If the programmer does not release it, the operating system will automatically reclaim it when the program is finished.
3. Free storage area, which is the memory block allocated by malloc, is very similar to the heap, but it ends its life with free.
4. Global/static storage area. Global variables and static variables are allocated to the same block of memory.
5. Constant storage, this is a special storage area, they are stored in a constant, not allowed to change (of course, you have to use improper means to change)

Theoretical knowledge of stacks and stacks
Application way
The stack:
It is automatically assigned by the system.     For example, declare a local variable in a function     int     B;     The system automatically makes room in the stack for b
Heap: Requires the programmer to apply and specify the size of the malloc function in c
Such as p1   =     (char     *) malloc (10);
The new operator is used in C++
Such as p2   =     (char     *) malloc (10);
But notice that p1 and p2 themselves are in the stack.


Related articles: