为什么 C++允许未动态分配的可变长度数组吗?

发布于 2024-08-29 01:31:19 字数 291 浏览 6 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莳間冲淡了誓言ζ 2024-09-05 01:31:20

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.

奶茶白久 2024-09-05 01:31:20

因为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.

不念旧人 2024-09-05 01:31:20

许多编译器都接受并扩展了标准。有两个基本原因:

  1. 邪恶的编译器编写者可能认为,让他们更难摆脱编译器有助于延长寿命。
  2. 仁慈的编译器编写者可能认为,在他们可以的情况下为您提供更多选择,而自己几乎不需要付出任何代价,这是一件好事。

Lot's of compilers embrace and extend standards. There are two basic reasons:

  1. Nefarious compiler-writers probably think that making it harder to move away from their compiler helps increase longevity.
  2. Benevolent compiler-writers probably think that giving you more options when they can at little to no cost to themselves is a good thing.
噩梦成真你也成魔 2024-09-05 01:31:20

所有提到的与它们在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文