1
messense 2014-07-25 20:40:41 +08:00
|
2
bombless 2014-07-25 22:33:51 +08:00
可变数组…等哪天你需要换vs编译就知错了…
|
3
dorentus 2014-07-25 22:42:07 +08:00
|
4
ffffwh 2014-07-25 23:15:02 +08:00
我在思考为什么原来(或VS中)不可以:
比方说这样 >>>> int a; int b[...]; int c; <<<< 变量的地址(即相对于栈指针的偏移)在编译时都应该是确定的。如果允许b长度不确定,那么就无法确定c的地址。 我想问: 请问是这么回事么? 新实现(C99)里是如何实现可变数组的呢? |
5
paulw54jrn 2014-07-25 23:27:42 +08:00 2
搬运工
http://stackoverflow.com/questions/21182307/how-does-gcc-implement-variable-length-arrays Here's the allocation code (x86 - the x64 code is similar) for the following example line taken from some GCC docs for VLA support: char str[strlen (s1) + strlen (s2) + 1]; where the calculation for strlen (s1) + strlen (s2) + 1 is in eax (GCC MinGW 4.8.1 - no optimizations): mov edx, eax sub edx, 1 mov DWORD PTR [ebp-12], edx mov edx, 16 sub edx, 1 add eax, edx mov ecx, 16 mov edx, 0 div ecx imul eax, eax, 16 call ___chkstk_ms sub esp, eax lea eax, [esp+8] add eax, 0 mov DWORD PTR [ebp-16], eax So it looks to be essentially alloca(). |