c******g 发帖数: 63 | 1 两种情况,一个是global static,一个是local static。
有一点应该没啥疑问,就是这两种static variable的memory allocation是before the
program starts的(我的理解是进入main()函数之前)。但是经常有参考资料中看到
说是“at the compile time”--这就不太懂了,我把一个.cpp文件compile一下就把
memory给allocate了?这明显不可能嘛。
第二个问题是:它们到底是啥时候initialize的?我觉得global的static肯定是在到了
declare这个变量的那一句时initialize的,是在进入main()函数前。
那local的呢(在一个function里的)?究竟是在整个程序start之前initialize,还是
第一次访问那个函数,进入函数后到了declare这个变量的那一句时才initialize?
请诸位大虾给予详细批评指正!多谢! |
t****t 发帖数: 6806 | 2
the
it's most likely allocated on .bss (uninitialized)/.data (initialized)
section, which is indeed "allocated" before runtime, since it's not dynamic
allocation anyway. Link editor will "know" the address of each, but before
execution starts, dynamic loader will "allocate" all the memory required for
.bss, most likely in one go.
for non-POD (non-trivial ctor), the ctor is executed before main(). The
order in one compilation unit is always up-to-down, but order across
multiple compilation units is indeterminate.
At first execution of function.
【在 c******g 的大作中提到】 : 两种情况,一个是global static,一个是local static。 : 有一点应该没啥疑问,就是这两种static variable的memory allocation是before the : program starts的(我的理解是进入main()函数之前)。但是经常有参考资料中看到 : 说是“at the compile time”--这就不太懂了,我把一个.cpp文件compile一下就把 : memory给allocate了?这明显不可能嘛。 : 第二个问题是:它们到底是啥时候initialize的?我觉得global的static肯定是在到了 : declare这个变量的那一句时initialize的,是在进入main()函数前。 : 那local的呢(在一个function里的)?究竟是在整个程序start之前initialize,还是 : 第一次访问那个函数,进入函数后到了declare这个变量的那一句时才initialize? : 请诸位大虾给予详细批评指正!多谢!
|
D*******a 发帖数: 3688 | 3 编译生成binary的时候,把static variable的内容直接写进文件。操作系统运行的时
候,先把文件读入内存,这就等于分配了空间。
the
【在 c******g 的大作中提到】 : 两种情况,一个是global static,一个是local static。 : 有一点应该没啥疑问,就是这两种static variable的memory allocation是before the : program starts的(我的理解是进入main()函数之前)。但是经常有参考资料中看到 : 说是“at the compile time”--这就不太懂了,我把一个.cpp文件compile一下就把 : memory给allocate了?这明显不可能嘛。 : 第二个问题是:它们到底是啥时候initialize的?我觉得global的static肯定是在到了 : declare这个变量的那一句时initialize的,是在进入main()函数前。 : 那local的呢(在一个function里的)?究竟是在整个程序start之前initialize,还是 : 第一次访问那个函数,进入函数后到了declare这个变量的那一句时才initialize? : 请诸位大虾给予详细批评指正!多谢!
|
F********g 发帖数: 475 | 4 If it's not explicitly initialized(static int temp;), stored in .bss.
If it is(static int temp=0x10;) ,stored in .data |
p*********t 发帖数: 2690 | 5 翻教科书.
the
【在 c******g 的大作中提到】 : 两种情况,一个是global static,一个是local static。 : 有一点应该没啥疑问,就是这两种static variable的memory allocation是before the : program starts的(我的理解是进入main()函数之前)。但是经常有参考资料中看到 : 说是“at the compile time”--这就不太懂了,我把一个.cpp文件compile一下就把 : memory给allocate了?这明显不可能嘛。 : 第二个问题是:它们到底是啥时候initialize的?我觉得global的static肯定是在到了 : declare这个变量的那一句时initialize的,是在进入main()函数前。 : 那local的呢(在一个function里的)?究竟是在整个程序start之前initialize,还是 : 第一次访问那个函数,进入函数后到了declare这个变量的那一句时才initialize? : 请诸位大虾给予详细批评指正!多谢!
|