如何实现GCC变长数组? [英] How does GCC implement variable-length arrays?
问题描述
如何实现GCC可变长度的数组(VLA)?是这样的阵列基本指针,以如返回的alloca动态分配的存储空间?
How does GCC implement Variable-length arrays (VLAs)? Are such arrays essentially pointers to the dynamically allocated storage such as returned by alloca?
的其它替代我能想到的是,这种阵列被分配作为在一个功能最后一个变量,以便在编译时已知的变量的偏移量。然而,第二VLA的偏移量,然后再次不被在编译时已知的。
The other alternative I could think of, is that such an array is allocated as last variable in a function, so that the offset of the variables are known during compile-time. However, the offset of a second VLA would then again not be known during compile-time.
推荐答案
下面是分配code(86 - 在64 code是相似的)从的为VLA支持一些海合会文档:
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];
其中计算的strlen(S1)+ strlen的(S2)+ 1
是 EAX
(GCC MinGW的4.8.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
所以它看起来是基本的alloca()
。
这篇关于如何实现GCC变长数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!