我们可以给静态数组的大小一个变量 [英] can we give size of static array a variable
问题描述
你好每一个我想问我,我已经阅读,我们可以声明动态数组只使用指针和使用malloc或newlike
hello every one i want to ask that i have read that we can declare dynamic array only by using pointer and using malloc or newlike
int * array = new int[strlen(argv[2])];
但我写了
int array[strlen(argv[2])];
它没有错误。
i已经读过静态数组只能通过给定常量数组大小来声明,但这里我给了一个可变大小的静态数组
i have read that static array can only be declared by giving constant array size but here i have given a variable size to static array
为什么是这样感谢
是安全使用或有机会,在任何后期阶段,它会使我的问题,我使用gcc linux
is it safe to use or is there chance that at any latter stages it will make problem i am using gcc linux
推荐答案
有什么称为可变长数组(VLA),它不是C ++的一部分, 是C99的一部分。许多编译器提供此功能作为扩展。
What you have is called a variable-length array (VLA), and it is not part of C++, although it is part of C99. Many compilers offer this feature as an extension.
即使是非常新的C ++ 11不包括VLA,因为整个概念不适合高级类型系统的C ++ 11(例如 decltype(array)
?),C ++为运行时大小的数组提供了开箱库解决方案,更强大(如 std :: vector
)。
Even the very new C++11 doesn't include VLAs, as the entire concept doesn't fit well into the advanced type system of C++11 (e.g. what is decltype(array)
?), and C++ offers out-of-the box library solutions for runtime-sized arrays that are much more powerful (like std::vector
).
在GCC中,使用 std = c ++ 98 / c ++ 03 / c ++ 0x
和 -pedantic
将给予警告。
In GCC, compiling with -std=c++98/c++03/c++0x
and -pedantic
will give you a warning.
这篇关于我们可以给静态数组的大小一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!