Memory model in C language (the difference between stack area, heap area, etc.)

As we all know, codes are stored in memory, so are the C language codes stored together or separately? Where are they stored?
Today we will find out.
Insert picture description here
The above figure is the specific storage location of the C language code, and I will introduce them one by one next.
Code area: store the binary code of the function body.
Constant area: store general constants, string constants, etc. This memory has only read permission but no write permission, so their values ​​cannot be changed during the running of the program. (This is especially important, we must keep in mind)
Static area (global area): store global variables, static variables, etc. This memory has read and write permissions, so their values ​​can be changed arbitrarily during the running of the program. (There is a small detail worthy of our attention. All global variables will be assigned the value 0 by default when they are not initialized. When we define global variables, the initialized variables and uninitialized variables are actually stored in different Location, the system will uniformly assign the variable stored in the uninitialized variable area to 0, and then store it together with the initialized variable.)
Heap area: generally allocated and released by the programmer, if the programmer does not release it, The operating system will reclaim the program when the program runs. Functions such as malloc(), calloc(), and free() operate on this memory.
Stack area: store function parameter values, local variable values, etc.

Next, I will give a piece of code, please judge where each part of the code is stored.

#include <stdio.h>
char *str1 = "hello world"; 
int n;
char* func(){
    
    
    char *str = "hello world";
    return str;
}
int main(){
    
    
    int a;
    static int b; 
    const int c;
    char *str2 = "01234";
    char  arr[20] = "56789";
}

answer:

#include <stdio.h>
char *str1 = "hello world"; //字符串在常量区,str1在全局数据区
int n;  //全局数据区
char* func(){
    
    
    char *str = "hello world";  //字符串在常量区,str在栈区
    return str;
}
int main(){
    
    
    int a;  //栈区
    static int b;//静态区 
    const int b;//常量
    char *str2 = "01234";  //字符串在常量区,str2在栈区
    char  arr[20] = "56789";  //字符串在常量区和栈区,arr在栈区
}

Here is worth our attention:

char *str2 = "01234";  //字符串在常量区,str2在栈区
char  arr[20] = "56789";  //字符串在常量区和栈区,arr在栈区

In fact, "01234" and "56789" are stored in the constant area, and the first type actually uses a pointer to point to the location where "01234" is stored in the constant area, so you cannot use such a statement: *str2='6' (because you are trying to change things in the constant area); and the second is to open up a new storage space in the stack area, and then copy the "56789" of the constant area on the stack In the area, so you can use the statement: arr[0]='0', but doing so will not change the "56789" in the constant area.

Guess you like

Origin blog.csdn.net/ABded/article/details/103655355