
元旦快乐
happy new year



新年最大的心愿就是一家人平平安安,幸福开心。
大人物小城梦公众号,祝福大家,元旦快乐。长大后,因为各种原因,陪伴家人很少,在以后的每年,一定多拿出一些时间陪陪家人。



内存结构
memory structure
内存中,有代码段,数据段,栈,堆。数据段分为已初始化和未初始化。
内存中属性很多,但作为程序员,只重点关注读(read),写(write),执行(execyte),共享(share),这四个属性。英语取开头字母,缩写为rwes。
代码段放程序的代码,属性是可读可执行。
数据段放全局数据,静态数据和常量。未初始化部分,属性是可读写。已初始化部分,分两部分,一部分属性是可读写,一部分属性是只读。
栈放参数,局部变量,保存的环境和返回地址,属性是可读写。
堆的属性是可读写。
这里的属性是初始状态,在程序运行过程中,操作系统有一定的机制可以调整属性。


全局变量和常量的内存分布
Memory distribution for global variables and constants
游离在其他函数以外,定义的变量,叫做全局变量。全局变量可以被所有函数同时访问。
编写程序:
#include #include int g_nTest1 = 0x53801204;int g_nTest2 = 0x41201314;void foo(){ printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1);}int main(){ int nTest = 999; printf("%p:", &nTest); printf("%d\r\n", nTest); printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1); foo(); system("pause"); return 0;}
按 F10 进行单步调试,可以看到 nTest 的地址是 0019ff2c

g_nTest1 的地址是 00429098 ,g_nTest2 的地址是 0042909c。两个是相邻的。
由此可见,编译器分配变量的原则:按同内存属性类型分配

在原有代码中,定义一个字符串 Hello
#include #include int g_nTest1 = 0x53801204;char g_szHello[] = "Hello";int g_nTest2 = 0x41201314;void foo(){ printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1);}int main(){ int nTest = 999; printf("%p:", &nTest); printf("%d\r\n", nTest); printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1); foo(); system("pause"); return 0;}
按 F10 进行单步调试,从内存结构可以看出,虽然数据类型不一致,有 int 类型,有字符串。但是它们都是已初始化的全局数据,所以分配的空间是连续的。

将代码中的 g_nTest2 的初值去掉
#include #include int g_nTest1 = 0x53801204;char g_szHello[] = "Hello";int g_nTest2;void foo(){ printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1);}int main(){ int nTest = 999; printf("%p:", &nTest); printf("%d\r\n", nTest); printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1); foo(); system("pause"); return 0;}
因为 g_nTest1 和 g_szHello 是已初始化全局数据,而 g_nTest2 是未初始化全局数据,所以 g_nTest1 和 g_szHello 连续放在已初始化全局数据区,g_nTest2 独自放在未初始化全局数据区。
按 F10 进行单步调试,g_nTest1 的地址是 00426a30 。g_szHello 的地址是 00426a34,和 g_nTest1 是连续排列的。而 g_nTest2 的地址却是 00429e60。


添加常量 g_nTest = 0x13145201 在代码中
#include #include int g_nTest1 = 0x53801204;char g_szHello[] = "Hello";int g_nTest2;const int g_nTest = 0x13145201void foo(){ printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1);}int main(){ int nTest = 999; printf("%p:", &nTest); printf("%d\r\n", nTest); printf("%p:", &g_nTest1); printf("%d\r\n", g_nTest1); foo(); system("pause"); return 0;}
按 F10 进行单步调试,可见,初始化全局数据在一个内存空间,未初始化全局数据在一个内存空间,常量在一个内存中间。

