- 论坛徽章:
- 0
|
请教一个有关虚拟内存的问题
这个问题取决于编译器的实现,可以有两种做法:
1、见到局部变量后再分配。
2、计算一个函数所有局部变量占据空间的总和,一次分配。在函数运行过程中不再为局部变量做栈的调整。
第一种方法效率低,但容易实现。第二种方法效率高,但实现起来麻烦一些。gcc 在默认情况下,使用第二种策略。 至于vc,可以作如下观察:
- #include <stdio.h>;
- void main(int argc, char **argv)
- {
- int x;
- int count = argc;
- int y;
- while(count >; 0)
- {
- __asm
- {
- push eax;
- push eax;
- push eax;
- }
- count--;
- }
- {
- int i = 1; //在堆栈中分配i的空间
- printf ("%p\n", &x); <====
- printf ("%p\n", &count); <====
- printf ("%p\n", &y); <====
- printf("%p\n", &i); <====
- }
- count = argc;
- while(count >; 0)
- {
- __asm
- {
- pop eax;
- pop eax;
- pop eax;
- }
- count--;
- }
- }
复制代码
0012FF7C
0012FF78
0012FF74
0012FF70
显然是预先分配好的。 |
|