为什么可变长度数组不是C ++标准的一部分? [英] Why aren't variable-length arrays part of the C++ standard?

查看:163
本文介绍了为什么可变长度数组不是C ++标准的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去几年中没有使用C。当我阅读这个问题今天我遇到了一些我不熟悉的C语法。



显然在 C99 以下语法有效:

  void foo int n){
int values [n]; //声明一个变长数组
}

这看起来是一个非常有用的功能。



有可能的原因:




  • 编译器供应商实现
  • 毛发
  • 与标准的其他部分不兼容

  • 功能可以用其他C ++结构模拟



C ++标准声明数组大小必须是常量表达式(8.3.4.1) p>

是的,我当然知道在玩具示例中可以使用 std :: vector< int> values(m); ,但是这会从堆而不是堆栈分配内存。如果我想要一个多维数组,如:

  void foo(int x,int y,int z){
int values [x] [y] [z]; //声明一个变长数组
}

/ code>版本变得很笨拙:

  void foo(int x,int y,int z){
vector<载体, vector< int> > >值(/ *这里真是痛苦的表达。
}

切片,行和列也可能在内存中扩展。 / p>

查看 comp.std.c ++ 的讨论,很清楚这个问题对于一些非常重量级的名称在两侧的争论。显然, std :: vector 始终是一个更好的解决方案。

解决方案

最近在usenet上讨论了这个问题:为什么不C ++ 0x中的VLA



我同意那些似乎同意必须在堆栈上创建一个潜在的大数组的人,这通常只有很少的空间,是不好的。参数是,如果你事先知道大小,你可以使用一个静态数组。如果你事先不知道大小,你会写不安全的代码。



C99 VLA可以提供一个小的优势,能够创建小数组,而不浪费空间或调用未使用的元素的构造函数,但是它们会对类型系统引入相当大的更改(您需要能够根据运行时值指定类型 - 除了 new 运算符类型说明符,这在当前C ++中不存在,但它们被特殊处理,因此运行时不会逃避 new 运算符的范围。



您可以使用 std :: vector ,但它不完全相同,因为它使用动态内存,并使它使用自己的stack-allocator不是很容易(对齐是一个问题,也)。它也不解决同样的问题,因为一个向量是一个可调整大小的容器,而VLAs是固定大小的。 C ++动态数组提案旨在介绍基于库的解决方案,作为基于语言的VLA的替代。但是,就我所知,它不会是C ++ 0x的一部分。


I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.

Apparently in C99 the following syntax is valid:

void foo(int n) {
    int values[n]; //Declare a variable length array
}

This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?

Some potential reasons:

  • Hairy for compiler vendors to implement
  • Incompatible with some other part of the standard
  • Functionality can be emulated with other C++ constructs

The C++ standard states that array size must be a constant expression (8.3.4.1).

Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:

void foo(int x, int y, int z) {
    int values[x][y][z]; // Declare a variable length array
}

the vector version becomes pretty clumsy:

void foo(int x, int y, int z) {
    vector< vector< vector<int> > > values( /* Really painful expression here. */);
}

The slices, rows and columns will also potentially be spread all over memory.

Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.

解决方案

There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.

I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.

C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).

You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.

这篇关于为什么可变长度数组不是C ++标准的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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