This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2个答案)
2年前关闭。
根据C ++标准,数组的大小必须大于零并具有一个编译时间常数。如果该数组是VLA,则它必须具有自动存储持续时间,即必须将数组设为本地存储。换一种说法:
所以,我的问题是-为什么我们不能在C ++中使用具有静态存储期限的VLA?
(2个答案)
2年前关闭。
根据C ++标准,数组的大小必须大于零并具有一个编译时间常数。如果该数组是VLA,则它必须具有自动存储持续时间,即必须将数组设为本地存储。换一种说法:
#include<iostream>
int size = 10;
constexpr int Size = 10;
int ar[size]; /* error as size is not a compile time constant and ar does not have auto storage class. */
int Ar[Size]; // fine, no error as Size is a compile time constant.
int main()
{
int arr[size]; // fine as arr has auto storage class.
return 0;
}
所以,我的问题是-为什么我们不能在C ++中使用具有静态存储期限的VLA?
最佳答案
所以,我的问题是-为什么我们不能在C ++中使用具有静态存储期限的VLA?
首先,VLA不是标准的c ++功能。
好吧,如果您有支持VLA的编译器,则它们不能用于静态存储分配,因为它只是运行时功能,并且所有静态存储分配都是在编译时完成的。