Table of contents
1 Memory partition in C language
1.2 Introduction to memory partition
C language has been learning for a while, and today I would like to summarize the five major areas of C language memory. It is a very necessary knowledge point for us to understand the C language in depth. By understanding the five major areas, it is very helpful for further learning the bottom layer of C language.
1 Memory partition in C language
1.1 Five memory partitions
The C language memory can be roughly divided into five areas, as shown in the figure and table below:
memory image area | content | permissions |
stack area | Ordinary variables in functions | readable and writable |
heap area | Dynamically allocated memory | readable and writable |
(global) static variable area | static modified variable | readable and writable |
constant area | Constants for initializing variables | read only |
code area | code instruction | read only |
nt k=1;
void main()
{
int i=1;
char *j;
static int m=1;
char *n="hello";
printf("栈区地址为:0X%x\n",&i);
j = (char*)malloc(2); //一般不确定需要多大空间的时候用
free(j);//及时释放
printf("堆区地址为:0X%x\n",j);
printf("全局变量地址为:0X%x\n",&k);
printf("静态变量地址为:0X%x\n",&m);
printf("文字常量区地址为:0X%x\n",n);
printf("程序区地址为:0X%x\n",&main);
}
char *i="hello";
char j[10]="hello";
printf("0X%x\n",i); //存放在文字常量区
printf("0X%x\n",j); //存放在栈区
j[1]='*';//可以直接赋值
//*(i+1)='*'; //等价于i[1]='*';
//不可以这样赋值, 因为i是指针,指向的是文字常量区,里面的内容是不能修改的
i=j; //这样可以
printf("%s\n",i);
printf("%x\n",i);
j=i;//这样不可以,因为j虽然也是地址,但是不是指针变量,不能直接赋值
1.2 Introduction to memory partition
1.2.1 Stack area (stack)
栈区
It is automatically allocated and released by the compiler, and is automatically managed by the operating system without manual management.
The content on the stack only exists within the scope of the function, and when the function finishes running, the content will be automatically destroyed.
#include<stdio.h>
char *getMem()
{
char buf[64]; //局部变量 栈区存放
strcpy(buf, "123456789");//向buf所代表的内存中写入内容
//printf("buf:%s\n", buf);
return buf;//返回所分配内存区域的第一个元素的地址
}
void main()
{
char *tmp = NULL;
tmp = getMem(10);
if (tmp == NULL)
{
return ;
}
printf("tmp:%s\n", tmp);//输出tmp:
system("pause");
return ;
}
Memory analysis:
- The stack area
由高到低
grows in the direction of the memory address, and its maximum size is determined at compile time. The speed is fast, but the freedom is poor, and the maximum space is not large. - The stack area is
先进后出
the principle, that is, those who enter first are blocked at the innermost part of the house, those who enter later are at the door, and those who enter at the door go out first when they are released. -
Stack storage content
- Temporarily created
局部变量
andconst定义的局部变量
stored in the stack area. - When the function is called and returned , its
入口参数
sum返回值
is stored in the stack area.
In layman's terms:
The stack area is used to store local variables, such as int a, int b, const int a, char p, char arr[ ] defined inside the function, and formal parameters of the function, etc. are stored in the stack area. The data in the stack area is managed by the compiler, and will be released automatically after the call, pushed to the stack, and popped from the stack. The principle of first in, last out, for example, when you execute a function call, the compiler will first push the address of the next code onto the stack, and then push some local variables, formal parameters, etc. in the function you called into the stack, and wait for your function call to complete. The stack will clear all the variables and formal parameters that were pushed onto the stack before the function you called, and then execute the program according to the address of the next code, and the subsequent programs will also be executed in this way. The stack area has a size, generally around 1M, so don't define too large an array.
1.2.2 Heap area (heap)
堆区
Memory is allocated and freed by the programmer. If the programmer does not release it, it may be reclaimed by the operating system at the end of the program.
#include <stdio.h>
char *getMem(int num)
{
char *p1 = NULL;
p1 = (char *)malloc(sizeof(char) * num);
if (p1 == NULL)
{
return NULL;
}
return p1;
}
void main()
{
char *tmp = NULL;
tmp = getMem(10);
if (tmp == NULL)
{
return ;
}
strcpy(tmp, "111222"); //向tmp做指向的内存空间中copy数据,注意不是向指针变量tmp中
printf("tmp:%s\n", tmp);//输出tmp:111222
system("pause");
return ;
}
Memory analysis:
- The heap area
由低到高
grows in the direction of memory addresses, and its size is determined by the upper limit of system memory/virtual memory. The speed is slow, but it has great freedom and large available space.
Dynamically apply and release memory in the heap area
Use functions such as malloc()
,free()
etc. to realize dynamic memory distribution.
void *malloc(size_t);
- The parameter
size_t
is the size in bytes of the allocation; - The return value is a
void*
type pointer, which points to the first address of the allocated space;
(void *
type pointers can be converted to other types of pointers)
Use free()
functions to release memory, otherwise it will cause memory leaks .
void free(void * /*ptr*/);
- The parameter
ptr
is the first address of the allocated memory. - no return value;
In layman's terms:
Manual application and release by the programmer
For example: int p=(int )malloc(sizeof(int)10), which means that a 40-byte heap space has been applied for, and then remember to release it with free after the application.
The code area is used to store code, which is converted into binary storage.
1.2.3 (Global) static area
全局变量
It is usually used for the storage area of variables whose storage size can be determined during compilation, but it is used for the sum that is visible during the entire program runtime 静态变量
.
The global zone consists of and , . .bss段
.data段
可读可写
#include <stdio.h>
char * getStr1()
{
char *p1 = "abcd";
return p1;
}
char *getStr2()
{
char *p2 = "abcd";
return p2;
}
void main()
{
char *p1 = NULL;
char *p2 = NULL;
p1 = getStr1();
p2 = getStr2();
//打印p1 p2 所指向内存空间的数据,不是p1 p2中的数据
printf("p1:%s , p2:%s \n", p1, p2);//输出p1:abcd , p2:abcd
//打印p1 p2 的值
printf("p1:%d , p2:%d \n", p1, p2);//输出p1:19184372 , p2:19184372
system("pause");
return;
}
Memory analysis:
.bss段
Uninitialized global variables and uninitialized static variables are stored in the .bss section.
Global variables initialized to 0 and static variables initialized to 0 are stored in the .bss section.
The .bss segment does not occupy executable space, and its contents are initialized by the operating system..data段
Initialized global variables are stored in the .data segment.
Initialized static variables are stored in the .data segment.
The .data segment occupies the space of the executable file, and its content is initialized by the program.
In layman's terms:
The global area is quite special, and it is also divided into a global variable area, a static variable area, and a constant area. The global variable area is used to store global variables, and the static variable area is used to store variables with static modification (including static local variables and static global variables). This area exists as long as it contains static. The constant area is used to store character constants, as well as global variables modified by const. Local variables modified by const do not exist here, so don't confuse them. Everything stored in the global area is managed by the operating system, and will be released by the operating system when the program ends. The data stored in the constant area cannot be changed, even if you use pointers. You may say that local variables modified by const can be changed with pointers, but local variables are not stored in the constant area. This is clear.
1.2.4 Constant area
字符串
,数字
and other constants are stored in the constant area. const
Modified global variables are stored in the constant area.
During the running of the program, the contents of the constant area cannot be modified.
1.2.5 Code area
程序执行代码
Stored in the code area, its value cannot be modified (if modified, an error will occur). 字符串常量
and define定义的常量
may also be stored in the code area.
The above five areas, the code area and the global area are created after the .exe file is generated. Double-click the .exe file to run the program to generate the stack area and heap area.
Directly above the picture below:
#include<stdio.h>
#include<string.h>
int a = 10;
static int b = 20;
void fun(int x)
{
char *p = "Hello";
printf("形参x的地址=%d\n\n", &x);
printf("Hello的地址=%d\n\n", "Hello");
printf("指针变量p的地址=%d\n\n", &p);
return;
}
int main(int argc,const char *argv[])
{
int c = 10;
int d = 20;
static int e = 30;
char *p = "Hello";
printf("\n全局变量a的地址=%d\n\n", &a);
printf("静态全局变量b的地址=%d\n\n", &b);
printf("静态局部变量e的地址=%d\n\n", &e);
printf("字符串\"Hello\"的地址=%d\n\n", "Hello");
printf("局部变量c的地址=%d\n\n", &c);
printf("局部变量d的地址=%d\n\n", &d);
printf("指针变量p的地址=%d\n\n", &p);
fun(5);
system("pause");
return 0;
}
Simply use a diagram to show it, and summarize:
the global area stores global variables, static variables, character constants, and const-modified global variables. The stack area stores the formal parameters of local variables and functions, as well as the addresses of some codes. The content of the stack area can be modified. The heap area is manually applied
and released by the programmer. It is applied for by the malloc function and released by the free function.