为什么当int array [n]无效时,新的int [n]有效吗? [英] Why is new int[n] valid when int array[n] is not?

查看:173
本文介绍了为什么当int array [n]无效时,新的int [n]有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码:

foo(int n){
    int array[n];
}

我知道这是无效的语法,并且无效,因为c ++标准要求在编译时设置数组大小(尽管某些编译器支持以下语法).

I understand that this is invalid syntax and that it is invalid because the c++ standard requires array size to be set at compile time (although some compilers support the following syntax).

但是我也理解以下是有效的语法:

However I also understand the the following is valid syntax:

bar(int n){
    int *array = new int[n];
}

我不明白为什么允许这样做,这与创建一个在运行时确定大小的数组不一样吗?这样做是一种好习惯吗?如果需要,我应该使用向量吗?

I don't understand why this is allowed, isn't it the same as creating an array where the size is determined at runtime? Is it good practice to do this or should I be using a vector if I need to do this instead?

推荐答案

这是因为前者分配在堆栈上,而后者分配在堆上.

That's because the former is allocated on the stack and the latter on the heap.

在堆栈上分配某些内容时,了解对象的大小对于正确构建它至关重要. C99允许在运行时指定大小,并且由于无法在编译时计算其大小,因此在构建和拆卸上述堆栈时会带来一些麻烦.必须发出机器代码,以便在程序执行期间执行上述计算.这可能是C ++标准未包含此功能的主要原因.²

When you allocate something on the stack, knowing the size of the object is essential for correctly building it. C99 allows the size to be specified at run time, and this introduces some complications in building and dismantling the aforementioned stack, since you cannot calculate its size at compile time. Machine code must be emitted in order to perform said calculation during the execution of the program. This is probably the main reason why this feature wasn't included in the C++ standard.²

相反,顾名思义,堆没有固定的结构.只要不重叠并且您有足够的(虚拟)内存¹,就可以按任意顺序分配任何大小的块.在这种情况下,在编译时知道大小并不重要.

On the contrary, the heap has no fixed structure, as the name implies. Blocks of any size can be allocated with no particular order, as long as they do not overlap and you have enough (virtual) memory¹. In this case, knowing the size at compile time is not that relevant.

此外,请记住,堆栈的大小有限,主要是在耗尽所有可用内存之前检测无限递归.通常,该限制固定在1MB左右,而您很少达到该限制.除非您分配大对象,否则应将其放置在堆中.

Also, remember that the stack has a limited size, mostly to detect infinite recursions before they consume all the available memory. Usually the limit is fixed around 1MB, and you rarely reach that. Unless you allocate large objects, which should be placed in the heap.

您应该使用的可能是std::vector<int>.但这实际上取决于您要做什么.

As of what you should use, probably a std::vector<int>. But it really depends on what you are trying to do.

还要注意,C ++ 11有一个std::array类,其大小必须在编译时知道. C ++ 14应该引入了std::dynarray,但由于有关编译时未知大小堆栈分配的大量工作要做而被推迟了.

Also note that C++11 has a std::array class, whose size must be known at compile time. C++14 should have introduced std::dynarray, but it was postponed because there is still much work to do concerning compile-time unknown size stack allocation.

¹块通常是出于性能原因而顺序分配的,但这不是必需的.

¹ blocks are usually allocated sequentially for performance reasons, but that's not required.

²如所指出的那样,知道编译时的大小并不是硬性要求,但是它使事情变得更简单.

² as pointed out, knowing the size at compile time is not a hard requirement, but it makes things simpler.

这篇关于为什么当int array [n]无效时,新的int [n]有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆