- 論壇徽章:
- 0
|
都知道C中的數(shù)組要不給定大小,要不在堆中動態(tài)分配,而將變量作為數(shù)組長度是非法的,那么這么做到底會發(fā)生什么事情呢?我試了一下,源代碼如下:
- //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
復制代碼
編譯器獲得了棧中變量的值后,進行了一系列固定的算術(shù)操作(不以變量的值為改變),這么做究竟有什么意義呢?請各位幫我分析一下,謝謝! |
|