且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在运行时堆栈上的动态分配

更新时间:2023-09-07 09:30:52

在C ++中,数组有一个常量大小。然而,在C99中,存在称为可变长度数组或VLA的东西。这是你在这里做的。

In C++, arrays have a constant size. In C99, however, there exist something called variable-length arrays, or VLA's. This is what you're making here.

g ++ (C ++编译器)是 gcc (C编译器)和 g ++ 允许你使用C99特性,而VS没有这样的事情。基本上它是一个非标准的C ++扩展。

g++ (the C++ compiler) is a sibling of gcc (the C compiler), and g++ is allowing you to use that C99 feature, while VS has no such thing. Essentially it's a non-standard C++ extension.

如果你使 i (因为是标准C ++):

If you make i a compile-time constant, it would work (since that's standard C++):

const int i = 256; // obviously cannot change at runtime.

如果您需要C ++中的动态数组,请使用 std :: vector

If you need a dynamic array in C++, use std::vector.

注意,在C ++中,您需要为 main 指定返回类型。这总是 int

Note in C++ you need to specify a return type for main. This is and always shall be int.