C++ 为什么我不能创建一个大小为 n 的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5368531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I create an array of size n?
提问by Mihran Hovsepyan
Possible Duplicate:
Why can't I create an array with size determined by a global variable?
可能的重复:
为什么我不能创建一个大小由全局变量决定的数组?
This is definition of simple array with constant size 4, which is stored in stack memory:
这是常量大小为 4 的简单数组的定义,它存储在堆栈内存中:
int array[4];
Now If I want to declare array of dynamic size in stack it seems that I should write this code:
现在,如果我想在堆栈中声明动态大小的数组,似乎应该编写以下代码:
int n;
cin >> n;
int array[n];
But as we know this is not allowed in C++ and instead we can write this one, which will create the array in dynamic memory (i.e. heap):
但是我们知道这在 C++ 中是不允许的,我们可以编写这个,它将在动态内存(即堆)中创建数组:
int n;
cin >> n;
int *array = new int[n];
But this is more slower and (because of using new operator) and requires to call delete [] operator after we finish our work with array.
但这更慢(因为使用 new 运算符)并且需要在我们完成数组工作后调用 delete [] 运算符。
So my question is here:
所以我的问题在这里:
- Why is it that C++ don't allow you to create array of dynamic length in stack memory?
- 为什么 C++ 不允许您在堆栈内存中创建动态长度数组?
回答by Prasoon Saurav
int n;
cin >> n;
int array[n];
This will work if use g++. g++ support VLAs as an extension. However ISO C++ mandates size of an array to be a constant expression i.e the size must be known at compile time.
如果使用 g++,这将起作用。g++ 支持 VLA 作为扩展。然而,ISO C++ 要求数组的大小是一个常量表达式,即必须在编译时知道大小。
Why is it that C++ don't allow you to create array of dynamic length in stack memory?
为什么 C++ 不允许您在堆栈内存中创建动态长度数组?
Simple answer "Because the standard says so". Even the upcoming C++ Standard (C++0x) is not going to allow Variable Length Arrays.
简单的回答“因为标准是这样说的”。即使是即将到来的 C++ 标准 (C++0x) 也不会允许可变长度数组。
BTW we always have std::vector
in C++. So there's no reason to worry. :)
顺便说一句,我们总是std::vector
在 C++ 中使用。所以没有理由担心。:)
回答by Jonathan Leffler
C99 does allow variable length arrays (VLAs); C89 did not.
C99 确实允许可变长度数组 (VLA);C89没有。
void function(int n)
{
int array[n];
...
}
C++ (98, 03) does not allow VLAs in the same way that C99 does, but it has vectors and related types which are better in many respects.
C++ (98, 03) 不像 C99 那样允许 VLA,但它有向量和相关类型,它们在许多方面都更好。
回答by Nawaz
int n;
cin >> n;
int array[n];
In C++, the size of the array must be known at compile time. But in your code the size will be known at runtime. That is not allowed by the language!
在 C++ 中,必须在编译时知道数组的大小。但是在您的代码中,大小将在运行时已知。这是语言所不允许的!
回答by qrdl
There is no language called C/C++. There is C, which allows variable-length arrays (VLAs) since 1999, and there is C++, which doesn't allow.
没有称为 C/C++ 的语言。有 C,它允许自 1999 年以来的可变长度数组 (VLA),还有 C++,它不允许。
To answer your question "why" - C++ standard body for whatever reason didn't include VLAs into C++ standard.
回答你的问题“为什么”——C++ 标准体无论出于何种原因都没有将 VLA 包含到 C++ 标准中。