- 栈: 存放程序的局部变量 。若未初始化,内容随机。
- 堆:堆用于存储那些生存期与函数调用无关的数据。堆是用于存放进程运行中被动态分配的内存段,它的大小并不固定,可动态扩张或缩减。
- 全局(静态区):
- bss:static int cntActiveUsers;未初始化,全0
- data:static int cntWorkerBees = 10
- 常量区:也称为文本区(text),用于存放各种字符串常量和数字常量,以及全局的常量数据。const int g_i = 100; const char* str = “Hello”; char* a = “Hello”;char* b = “Hello”; a和b中的“Hello”占用常量区中同一内存
- 代码区
int a = 0; //全局初始化区
char *p1; //全局未初始化区
int main()
{
int b; // 栈
char s[] = "abc"; //栈
char *p2; //栈
char *p3 = "123456"; //123456\0在常量区,而p3在栈上。
static int c =0; //全局(静态)初始化区
p1 = (char *)malloc(10);
p2 = (char *)malloc(20); //分配得来得10和20字节的区域就在堆区。
strcpy(p1, "123456"); //123456\0放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
return 0;
}
参考:https://blog.youkuaiyun.com/u011555996/article/details/78923291