- 论坛徽章:
- 0
|
都知道C中的数组要不给定大小,要不在堆中动态分配,而将变量作为数组长度是非法的,那么这么做到底会发生什么事情呢?我试了一下,源代码如下:
- //test.c
- void test()
- {
- int a = 10;
- int p[a];
- p[3] = 2;
- }
复制代码
$gcc -S test.c
生成的test.s的片断
- test:
- pushl %ebp
- movl %esp, %ebp
- subl $24, %esp
- movl %esp, %eax
- movl %eax, %edx
- movl $10, -4(%ebp)
- movl -4(%ebp), %eax
- sall $2, %eax
- addl $15, %eax
- addl $15, %eax
- shrl $4, %eax
- sall $4, %eax
- subl %eax, %esp
- movl %esp, -20(%ebp)
- movl -20(%ebp), %eax
- addl $15, %eax
- shrl $4, %eax
- sall $4, %eax
- movl %eax, -20(%ebp)
- movl -20(%ebp), %eax
- movl %eax, -8(%ebp)
- movl -8(%ebp), %eax
- movl $2, 12(%eax)
- movl %edx, %esp
- leave
- ret
复制代码
编译器获得了栈中变量的值后,进行了一系列固定的算术操作(不以变量的值为改变),这么做究竟有什么意义呢?请各位帮我分析一下,谢谢! |
|