为什么C ++不支持堆栈上的动态数组? [英] Why doesn't C++ support dynamic arrays on the stack?

查看:234
本文介绍了为什么C ++不支持堆栈上的动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99中,这是合法的:

  void f(size_t sz){
char arr [sz];
// ...
}

数组 - 已在C ++中删除,并且在C ++ 11中没有看到返回。



AFAIK C ++是基于C兼容性的,所以我想知道必须有一些非常好的论点,不包括这个有用的功能,对吗?



我可以想到的是这样:



优点




  • 通过允许需要在堆栈上的更智能的数组大小。

  • 减少智能指针(或更糟糕的是,手动引入 delete [] ),并减慢堆分配。 / li>
  • 与C99的兼容性。



缺点




  • 允许人们轻松地在堆栈上分配太大的数组,从而产生难以调试的堆栈溢出。

  • >


    那么,为什么他们在导入其他C99功能时不包括它?






    为了防止这被关闭为主观的或不建设性的,我正在寻找来自委员会成员的报价或讨论链接到谈论这件事 -



    讨论,将它看作一个历史问题,

    正如James McNellis在下面的评论中指出的,C ++存在于C99标准化可变长度数组之前。你可能会读我的问题,然后作为:他们为什么不会和不会添加它

    解决方案

    我认为,因为C ++提供了更好的解决方案: std :: vector< T> std :: array< T,N& ; (C ++ 11);虽然后者不是动态的,但它优于原始数组。



    由于C不能提供这些解决方案,C99提出了可变长度数组(Variable Length Array,VLA) 。它与常规数组有相同的问题:它在传递给函数时衰减为指针,你不再知道数组的大小。



    Florian Weimer 在<$>请这里 c $ c> comp.std.c ++ 如果C ++ 0x允许VLA,那么下面的代码是什么意思?

      int vla [n]; // n在运行时是已知的! 
    std :: vector< decltype(vla)> v; //这是什么意思?

    编译器如何在编译时实例化何时是类型参数取决于在运行时已知的 n


    In C99 this was legal:

    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.

    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?

    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.

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

    解决方案

    I think, its 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.

    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 pointer on passing it to function, and you no longer know the size of array.

    And as Florian Weimer asked here at comp.std.c++ that if C++0x allows VLA, then what would the following code mean?

    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?

    这篇关于为什么C ++不支持堆栈上的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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