In C99 this was legal:
在C99中,这是合法的:
void f(size_t sz) {
char arr[sz];
// ...
}
However, this - dynamically sized stack arrays - has been dropped in C++, and not seeing a return in C++11.
但是,这个动态大小的堆栈数组在c++中被删除,在c++中没有返回。
AFAIK C++ was made with C compatibility in mind, so I wondered There must be some very good argument of not including this useful feature, right?
AFAIK c++是在考虑了C兼容性的情况下进行的,所以我想知道一定有一些很好的论证不包括这个有用的特性,对吧?
All I could think of was this:
我所能想到的就是:
Pros
- Memory savings by allowing smarter array sizes that need to be on the stack (temporary buffers?).
- 通过允许需要在堆栈上的更智能的数组大小来节省内存(临时缓冲区?)
- Less "smart pointers" (or worse, manual bug-introducing
delete []
's) and slow heap allocations. - 更少的“智能指针”(或者更糟的是,手动引入删除[])和慢堆分配。
- Compatibility with C99.
- 与C99的兼容性。
Cons
- Allows people to easily allocate too large arrays on the stack giving hard-to-debug stack overflows.
- 允许人们很容易地在堆栈上分配太大的数组,从而产生难以调试的堆栈溢出。
- More complicated for compiler writers.
- 对于编译器编写者来说更加复杂。
So, why did they didn't they include it when they imported other C99 features?
To prevent this from being closed as "subjective" or "not constructive", I'm looking for quotes from commitee members or links to discussions talking about the matter - with bonus points for a quick SO roundup of course.
为了防止这种情况以“主观的”或“非建设性的”的方式结束,我正在寻找来自委员会成员的引用,或者讨论这个问题的链接——当然,还有一些额外的好处,可以作为一个快速的综述。
Rather than seeing this as a Ponies vs Hamsters discussion, see it as a historical question, mere interest in the advantages and disadvantages that were considered (if at all).
与其把这看成是小马对仓鼠的讨论,不如把它看成是一个历史问题,仅仅是对所考虑的利弊的兴趣(如果有的话)。
EDIT: As James McNellis pointed out in the comments below C++ existed before C99 standardized variable-length arrays. You might read my question then as: "Why didn't and won't they add it?".
编辑:正如James McNellis在下面的评论中指出的,在C99标准变长数组出现之前,c++就已经存在了。你可能会把我的问题解读为:“为什么他们没有也不愿意添加它?”
2 个解决方案
#1
18
I think, it's because C++ provides superior solutions: std::vector<T>
and std::array<T,N>
(C++11); though the latter is not dynamic as such but it's superior to raw arrays. You can always know the size, no matter which function you pass the vector or array.
我认为,这是因为c++提供了更好的解决方案:std::vector
Since C cannot provide these solutions, C99 came up with Variable Length Array (VLA). It has the same problem as regular arrays: it decays into a pointer on passing it to function, and you no longer know the size of the array.
由于C无法提供这些解决方案,C99提出了可变长度数组(VLA)。它有与常规数组相同的问题:它在传递给函数时衰减成一个指针,并且您不再知道数组的大小。
And as Florian Weimer asked here at comp.std.c++
that if C++0x allows VLA, then what would the following code mean?
正如Florian Weimer在comp.std中提出的。如果c++ 0x允许VLA,那么下面的代码是什么意思呢?
int vla[n]; //n is known at runtime!
std::vector<decltype(vla)> v; //what does this mean?
How is the compiler going to instantiate the vector template at compile-time when it's type argument depends on n
which is known at runtime?
编译器如何在编译时实例化向量模板,当它的类型参数依赖于在运行时知道的n时?
#2
2
This functionality largely duplicates that of std::vector
, except that it consumes a more limited resource (stack vs heap space). As such, there is not really any need for it in C++, semantics-wise.
这个功能在很大程度上复制了std::vector,除了它消耗了一个更有限的资源(堆栈和堆空间)。因此,在c++中,在语义上并不需要它。
One could argue that on-stack allocation can improve efficiency (particularly in the face of multiple threads); however, this can also be achieved in C++ using custom allocators to build a private memory pool, either on the stack or heap. This is again more flexible than placing memory on the stack, and indeed you could create a custom allocator that carves chunks out of an on-stack memory buffer easily enough. It's not exactly the same as dynamic array semantics, but the existence of custom allocators and STL containers covers most use cases you'd want stack allocation.
有人可能会说栈上分配可以提高效率(特别是在面对多个线程时);但是,这也可以在c++中实现,使用定制的分配器在堆栈或堆上构建私有内存池。这同样比将内存放在堆栈上更灵活,实际上您可以创建一个自定义分配器,它可以很容易地从堆栈上的内存缓冲区中分割出块。它与动态数组语义并不完全相同,但是自定义分配器和STL容器的存在涵盖了您希望堆栈分配的大多数用例。
#1
18
I think, it's because C++ provides superior solutions: std::vector<T>
and std::array<T,N>
(C++11); though the latter is not dynamic as such but it's superior to raw arrays. You can always know the size, no matter which function you pass the vector or array.
我认为,这是因为c++提供了更好的解决方案:std::vector
Since C cannot provide these solutions, C99 came up with Variable Length Array (VLA). It has the same problem as regular arrays: it decays into a pointer on passing it to function, and you no longer know the size of the array.
由于C无法提供这些解决方案,C99提出了可变长度数组(VLA)。它有与常规数组相同的问题:它在传递给函数时衰减成一个指针,并且您不再知道数组的大小。
And as Florian Weimer asked here at comp.std.c++
that if C++0x allows VLA, then what would the following code mean?
正如Florian Weimer在comp.std中提出的。如果c++ 0x允许VLA,那么下面的代码是什么意思呢?
int vla[n]; //n is known at runtime!
std::vector<decltype(vla)> v; //what does this mean?
How is the compiler going to instantiate the vector template at compile-time when it's type argument depends on n
which is known at runtime?
编译器如何在编译时实例化向量模板,当它的类型参数依赖于在运行时知道的n时?
#2
2
This functionality largely duplicates that of std::vector
, except that it consumes a more limited resource (stack vs heap space). As such, there is not really any need for it in C++, semantics-wise.
这个功能在很大程度上复制了std::vector,除了它消耗了一个更有限的资源(堆栈和堆空间)。因此,在c++中,在语义上并不需要它。
One could argue that on-stack allocation can improve efficiency (particularly in the face of multiple threads); however, this can also be achieved in C++ using custom allocators to build a private memory pool, either on the stack or heap. This is again more flexible than placing memory on the stack, and indeed you could create a custom allocator that carves chunks out of an on-stack memory buffer easily enough. It's not exactly the same as dynamic array semantics, but the existence of custom allocators and STL containers covers most use cases you'd want stack allocation.
有人可能会说栈上分配可以提高效率(特别是在面对多个线程时);但是,这也可以在c++中实现,使用定制的分配器在堆栈或堆上构建私有内存池。这同样比将内存放在堆栈上更灵活,实际上您可以创建一个自定义分配器,它可以很容易地从堆栈上的内存缓冲区中分割出块。它与动态数组语义并不完全相同,但是自定义分配器和STL容器的存在涵盖了您希望堆栈分配的大多数用例。