允许在运行时不动态分配数组大小吗?
我已经使用 C++ 几年了,今天我看到了一些代码,但这怎么可能完全合法呢?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
在GCC下编译。
如何在没有new
或malloc
的情况下在运行时确定大小?
为了仔细检查,我在谷歌上搜索了一些,所有与我类似的代码都声称给出了存储大小错误。
甚至 Deitel 的《C++ 如何编程》第 11 页。 常见编程错误 4.5 下的 261 个状态:
只能使用常量来声明自动数组和静态数组的大小。
启发我。
I've been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
cin >> size;
int array[size];
for(size_t i = 0; i < size; i++)
{
array[i] = i;
cout << i << endl;
}
return 0;
}
Compiled under GCC.
How can the size be determined at run-time without new
or malloc
?
Just to double check, I've googled some and all similar codes to mine are claimed to give storage size error.
Even Deitel's C++ How To Program p. 261 states under Common Programming Error 4.5:
Only constants can be used to declare the size of automatic and static arrays.
Enlight me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这在 C99 中有效。
C99 标准支持堆栈上可变大小的数组。 也许您的编译器也选择支持这种构造。
请注意,这与
malloc
和new
不同。gcc
在堆栈上分配数组,就像使用int array[100]
一样,只需调整堆栈指针即可。 没有进行堆分配。 它非常像_alloca
。This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from
malloc
andnew
.gcc
allocates the array on the stack, just like it does withint array[100]
by just adjusting the stack pointer. No heap allocation is done. It's pretty much like_alloca
.这称为 VLA(可变长度数组)。 它是c99中的标准,但gcc允许它在c++代码中作为扩展。 如果您希望它拒绝代码,请尝试使用
-std=standard
、-ansi
和-pedantic
选项。This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with
-std=standard
,-ansi
and-pedantic
options.它仅在 C99 中有效。 下次您可以尝试在可靠的编译器中检查您的代码。
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
它是有效的 C99,它不是有效的 C++。 这是两种语言之间的众多差异之一。
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
该代码在 GNU GCC 编译器中运行。
This Code runs in GNU GCC Compiler.
如果您使用 Dev-Cpp 编译器,您可以动态地为数组指定大小我已经尝试过
并且没有错误,但在 Visual C++ 和 Visual Studio 编译器上这是不可能的。
我认为原因是dev-c++为未初始化的int分配了一个正数
当我们给它一个数字时,它就会被给定的数字替换。
但也许其他编译器将 null 赋予未初始化的变量。
You can give size to an array dynamically in if you are using Dev-Cpp compiler I have tried it
and got no error but on visual c++ and visual studio compilers it is not possible.
I think the reason is that dev-c++ assigns a positive number to the uninitialized int
and when we give it a number it is replaced by the given one.
but perhaps other compilers give null to uninitialized variables.
我最近遇到了一个需要堆栈分配数组的场景。 (它是 v8 的包装器,每个方法调用都需要一个参数数组)。
std::vector 会进行堆内存分配,其性能不可接受。
这是我的解决方案,使用模板来分配案例数组:(
当然,我可以使用 32 大小的数组,但这不是那么优雅。)
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
(And of course, I can just do with a 32-sized array, but which is not so elegant.)
C++14 标准支持可变长度数组 (VLA),该标准最近已被接受,正在等待发布。
Variable Length Arrays (VLAs) are supported in the C++14 standard, which has recently been accepted, and is awaiting publication.