本文介绍了使用可变长度数组是否有任何开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用变长数组是否有一些开销?可以在运行时通过命令行参数传递数组的大小吗?与自动和动态分配数组相比,为什么要引入它?

Is there some overhead of using variable-length arrays? Could the size of array be passed via command line argument at run time? Why is it introduced, compared to automatic and dynamically allocating an array?

推荐答案

VLA 确实有一些开销(与普通"命名的编译时大小的数组相比).

VLA does have some overhead (compared to "ordinary" named compile-time-sized array).

首先,它具有运行时长度,但该语言为您提供了在运行时获取数组实际大小的方法(使用 sizeof).这立即意味着数组的实际大小必须存储在某处.这会导致一些微不足道的每阵列内存开销.但是,由于 VLA 只能声明为自动对象,因此任何人都不会注意到这种内存开销.这就像声明一个额外的整型局部变量.

Firstly, it has run-time length and yet the language provides you with means to obtain the actual size of the array at run-time (using sizeof). This immediately means that the actual size of the array has to be stored somewhere. This results in some insignificant per-array memory overhead. However, since VLAs can only be declared as automatic objects, this memory overhead is not something anyone would ever notice. It is just like declaring an extra local variable of integral type.

其次,VLA 通常是在栈上分配的,但由于其大小可变,一般情况下,它在内存中的确切位置在编译时是未知的.为此,底层实现通常必须将其实现为指向内存块的指针.这引入了一些额外的内存开销(对于指针),由于上述原因,这再次完全无关紧要.这也引入了轻微的性能开销,因为我们必须读取指针值才能找到实际的数组.这与访问 malloc-ed 数组时获得的开销相同(而不是使用命名的编译时大小的数组).

Secondly, VLA is normally allocated on stack, but because of its variable size, in general case its exact location in memory is not known at compile time. For this reason the underlying implementation usually has to implement it as a pointer to a memory block. This introduces some additional memory overhead (for the pointer), which is again completely insignificant for the reasons described above. This also introduces slight performance overhead, since we have to read the pointer value in order to find the actual array. This is the same overhead you get when accessing malloc-ed arrays (and don't get with the named compile-time-sized arrays).

由于 VLA 的大小是一个运行时整数值,因此它当然可以作为命令行参数传递.VLA 并不关心它的大小来自哪里.

Since the size of the VLA is a run-time integer value, it can, of course, be passed as a command-line argument. VLA doesn't care where its size comes from.

VLA 是作为运行时大小的数组引入的,具有低分配/解除分配成本.它们适用于普通"命名的编译时大小的数组(分配释放成本几乎为零,但大小固定)和 malloc ed 数组(具有运行时大小,但相对较高)分配-解除分配成本).

VLA were introduced as run-time-sized arrays with low allocation/deallocation cost. They fit between "ordinary" named compile-time-sized arrays (which have virtually zero allocation-deallocation cost, but fixed size) and malloc-ed arrays (which have run-time size, but relatively high allocation-deallocation cost).

VLA 遵守 [几乎] 与自动(即本地)对象相同的作用域相关生命周期规则,这意味着在一般情况下,它们不能替换 malloc-ed 数组.它们的适用性仅限于您需要具有典型自动生命周期的快速运行时大小的数组的情况.

VLA obey [almost] the same scope-dependent lifetime rules as automatic (i.e local) objects, which means that in general case they can't replace malloc-ed arrays. They applicability is limited to situations when you need a quick run-time sized array with a typical automatic lifetime.

这篇关于使用可变长度数组是否有任何开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:31
查看更多