是否有使用可变长度数组的开销? [英] Is there any overhead for using variable-length arrays?

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

问题描述

有没有使用可变长度数组的一些开销?可以数组的大小可通过在运行时的命令行参数传递?为什么介绍,相比于自动和动态分配数组?

Is there some overhead of using variable-length arrays? Can the size of array could 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 )。这立即意味着阵列的实际大小在某处被存储。这导致一些无关紧要的每个数组的内存开销。然而,由于沃拉斯只能被声明为自动对象,该内存开销是不是任何人会注意到。它就像声明整型额外的局部变量。

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 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.

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

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