在C ++中变长数组? [英] Variable-length arrays in C++?

查看:153
本文介绍了在C ++中变长数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有用c非常多,在过去的几年里。当我读<一个href=\"http://stackoverflow.com/questions/1886504/coding-problem-using-a-2-d-array-of-structs-inside-another-struct-in-c\">this今天的问题,我遇到了一些C语法,这点我是不熟悉的。

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.

显然,在 C99 的下面的语法是有效的:

Apparently in C99 the following syntax is valid:

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

这似乎是一个pretty有用的功能。当时有过一个关于它添加到C ++标准的讨论,如果是这样,为什么它省略?一些潜在的原因:

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:


  • 毛为编译器厂商实施

  • 不兼容标准的其他部分

  • 功能可以与其他C ++结构进行仿真

  • ???

C ++标准规定,数组的大小必须是一个常量前pression(8.3.4.1)

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

是的,我当然知道,在玩具例如,一个可以使用的std ::矢量&lt;&INT GT;值(米); ,但这从堆,而不是堆栈分配内存。如果我想要一个多维数组,如:

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
}

矢量版本变为pretty笨拙

the vector version becomes pretty clumsy

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

切片,行和列也将有可能为s $ P $垫遍布内存。

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

纵观讨论在 comp.std.c ++ 很显然,这个问题是pretty争议与争论的双方一些非常重量级的名字。这当然并不明显,一个的std ::矢量始终是一个更好的解决方案。

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.

推荐答案

最近有一个关于这个讨论的Usenet拉开序幕:<一href=\"http://groups.google.com/group/comp.std.c++/browse_thread/thread/2bfe25800d4961e8/9545494bbb336dfa\">Why沃拉斯没有C ++ 0x中。

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

我同意那些似乎认为不必创建堆栈,它通常只有很少可用空间的一个潜在的大阵,是不是好人。这个论点是,如果你知道的大小提前,你可以使用一个静态数组。如果你不知道事先的大小,你会写不安全code。

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沃拉斯可以提供能够不浪费空间,或要求未使用的元素构造函数来创建小数组一个小的好处,但他们会引入相当大的改变类型系统(你需要能够根据运行时指定类型值 - 这还不在当前存在C ++,除了运算符类型说明符,但是它们特殊处理,使运行时的烦躁不越狱的范围在操作员)。

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

您可以使用的std ::矢量,但它是不太一样的,因为它使用的动态内存,并使其使用自己的堆栈分配不正是易(排列是一个问题,太)。它也没有解决同样的问题,因为一个载体是可调整大小的容器,而VLAS是固定大小。该 C ++动态数组提议旨在介绍基于库的溶液,作为替代基于VLA的语言。但是,它不会为C ++ 0x中的一部分,据我所知。

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天全站免登陆