gpt4 book ai didi

c - 为什么需要在自动存储类中声明可变长度数组?

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:59 25 4
gpt4 key购买 nike

我在一些书上看到,用于声明可变长度数组的变量必须具有自动存储类。

谁能解释一下为什么它需要具有自动存储类的变量?

变长数组的空间是分配在栈中还是堆中?

最佳答案

这包含在 Rationale for International Standard—Programming Languages—C 中其中说:

All variably modified types must be declared at either block scope or function prototype scope. File scope identifiers cannot be declared with a variably modified type. Furthermore, array objects declared with either the static or extern storage class specifiers cannot be declared with a variable length array type [...]

和:

Restricting variable length array declarators to identifiers with automatic storage duration is natural since “variableness” at file scope requires some notion of parameterized typing. There was sentiment for allowing structure members to be variably modified; however allowing structure members to have a variable length array type introduces a host of problems such as the treatment when passing these objects, or even pointers to these objects, as parameters. In addition, the semantics of the offsetof macro would need to be extended and runtime semantics added. Finally, there was disagreement whether the size of a variable length array member could be determined using one of the other members. The Committee decided to limit variable length array types to declarations outside structures and unions.

关于c - 为什么需要在自动存储类中声明可变长度数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30923935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com