是否要求从堆栈分配一个 C 可变长度数组?
Is it required that a C Variable Length Array is allocated from the stack?
从我们的嵌入式系统代码中删除对 malloc 和 calloc 的所有调用后,我惊讶地发现 malloc 仍在链接中。调用图将我指向一个没有显式 *alloc 调用的函数,并且不调用任何可能分配的库函数,例如 strdup
.
我不得不查看生成的程序集,才意识到这是由于包含 VLA 的内联函数造成的。
我认为 VLA 必须是堆栈分配的。这个编译器坏了吗?
不,它们不必进行堆栈分配。如果你想让它在堆栈上,我会使用 alloca
。
来源 1:
Secondly, VLA is normally allocated on stack, but because of its variable size, in general case its exact location in memory is not known at compile time. For this reason the underlying implementation usually has to implement it as a pointer to a memory block. This introduces some additional memory overhead (for the pointer), which is again completely insignificant for the reasons described above. This also introduces slight performance overhead, since we have to read the pointer value in order to find the actual array. This is the same overhead you get when accessing malloc-ed arrays (and don't get with the named compile-time-sized arrays).
来源 2:https://en.wikipedia.org/wiki/Variable-length_array
One problem that may be hidden by a language's support for VLAs is that of the underlying memory allocation: in environments where there is a clear distinction between a heap and a stack, it may not be clear which, if any, of those will store the VLA.
不要求从堆栈分配 VLA(语言标准甚至没有提到堆栈或堆)。唯一要求如下:
6.2.4 Storage durations of objects
...
7 For such an object that does have a variable length array type, its lifetime extends from
the declaration of the object until execution of the program leaves the scope of the
declaration.35) If the scope is entered recursively, a new instance of the object is created
each time. The initial value of the object is indeterminate.
35) Leaving the innermost block containing the declaration, or jumping to a point in that block or an
embedded block prior to the declaration, leaves the scope of the declaration.
鉴于此,从堆栈分配是有意义的,但对于非常大的对象,这可能是不可能的,并且这样的对象可能从堆或一些其他内存段代替。簿记取决于实施。
从我们的嵌入式系统代码中删除对 malloc 和 calloc 的所有调用后,我惊讶地发现 malloc 仍在链接中。调用图将我指向一个没有显式 *alloc 调用的函数,并且不调用任何可能分配的库函数,例如 strdup
.
我不得不查看生成的程序集,才意识到这是由于包含 VLA 的内联函数造成的。
我认为 VLA 必须是堆栈分配的。这个编译器坏了吗?
不,它们不必进行堆栈分配。如果你想让它在堆栈上,我会使用 alloca
。
来源 1:
Secondly, VLA is normally allocated on stack, but because of its variable size, in general case its exact location in memory is not known at compile time. For this reason the underlying implementation usually has to implement it as a pointer to a memory block. This introduces some additional memory overhead (for the pointer), which is again completely insignificant for the reasons described above. This also introduces slight performance overhead, since we have to read the pointer value in order to find the actual array. This is the same overhead you get when accessing malloc-ed arrays (and don't get with the named compile-time-sized arrays).
来源 2:https://en.wikipedia.org/wiki/Variable-length_array
One problem that may be hidden by a language's support for VLAs is that of the underlying memory allocation: in environments where there is a clear distinction between a heap and a stack, it may not be clear which, if any, of those will store the VLA.
不要求从堆栈分配 VLA(语言标准甚至没有提到堆栈或堆)。唯一要求如下:
6.2.4 Storage durations of objects
...
7 For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration.35) If the scope is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate.
35) Leaving the innermost block containing the declaration, or jumping to a point in that block or an embedded block prior to the declaration, leaves the scope of the declaration.
鉴于此,从堆栈分配是有意义的,但对于非常大的对象,这可能是不可能的,并且这样的对象可能从堆或一些其他内存段代替。簿记取决于实施。