为什么 C++允许未动态分配的可变长度数组吗?
我对 C++ 比较陌生,从一开始我就被灌输这样的观念:你不能做类似的事情
int x;
cin >> x;
int array[x];
,相反,你必须使用动态内存。然而,我最近发现上面的将编译(尽管我收到一个迂腐的警告,说它被 ISO C++ 禁止)。我知道如果标准不允许这样做显然是一个坏主意,但我以前甚至不知道这是可能的。
我的问题是,如果标准不允许,为什么 g++ 允许不动态分配的可变长度数组?另外,如果编译器可以做到这一点,为什么它不在标准中?
I'm relatively new to C++, and from the beginning it's been drilled into me that you can't do something like
int x;
cin >> x;
int array[x];
Instead, you must use dynamic memory. However, I recently discovered that the above will compile (though I get a -pedantic warning saying it's forbidden by ISO C++). I know that it's obviously a bad idea to do it if it's not allowed by the standard, but I previously didn't even know this was possible.
My question is, why does g++ allow variable length arrays that aren't dynamically allocated if it's not allowed by the standard? Also, if it's possible for the compiler to do it, why isn't it in the standard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C99 中的 C 语言添加了对可变长度数组 (VLA) 的支持。
很可能,由于 gcc 中存在对它们的支持(以支持 C99),因此向 g++ 添加对它们的支持相对简单。
也就是说,它是一个特定于实现的语言扩展,如果您希望代码可移植,那么使用特定于实现的扩展并不是一个好主意。
Support for variable length arrays (VLAs) was added to the C language in C99.
It's likely that since support for them exists in gcc (to support C99), it was relatively straightforward to add support for them to g++.
That said, it's an implementation-specific language extension, and it's not a good idea to use implementation-specific extensions if you want your code to be portable.
因为C99支持它。我实在无法解释为什么它不在 C++ 标准中。但是,它并不像您想象的那么有用,因为它很容易导致(如果您不小心)堆栈溢出(因为它通常基于 alloca,本身是非标准的)。另一个错误是返回指向动态数组的指针,这将立即超出范围。
Because it's supported in C99. I can't really speak to why it's not in the C++ standard. However, it's not as useful as you might think because it easily leads (if you're not careful) to stack overflow (since it's typically based on alloca, itself non-standard). Another mistake is to return a pointer to a dynamic array, which will immediately go out of scope.
许多编译器都接受并扩展了标准。有两个基本原因:
Lot's of compilers embrace and extend standards. There are two basic reasons:
所有提到的与它们在 C 中有关的原因都是正确的,尽管要求有限制。您的示例可能会展示比 C 中所需的更灵活的支持(如果您使用 scanf 而不是 cin 实现它,将其放入 .c 文件中,并使用 gcc)。
这几乎只是对 alloca (allocate auto) 的隐式调用,它只是减少堆栈指针(增加堆栈大小)并将新堆栈指针复制到另一个寄存器,该寄存器用作指向已分配内存的指针。
不同之处在于,使用 alloca 创建的对象不会调用构造函数和析构函数。
All of the reasons mentiond having to do with them being in C are correct, though there are limitations for the requirement. You're example may demonstrate more flexible support than what is required in C (if you implemented that using scanf rather than cin, put it in a .c file, and used gcc).
This is almost just a implicit call to alloca (allocate auto) which just decreases the stack pointer (increasing the stack size) and copies the new stack pointer to another register which is used as a pointer to the allocated memory.
The difference is that constructors and destructors aren't going to be called on objects created with a alloca.