Why no variable size array in stack?

后端 未结 7 1184
南方客
南方客 2020-11-27 19:28

I don\'t really understand why I can\'t have a variable size array on the stack, so something like

foo(int n) {
   int a[n];
}

As I underst

7条回答
  •  清酒与你
    2020-11-27 20:13

    Note that the proposal was rejected and the following is no longer true. It may be revived for a future version of C++ though.

    VLA as described in N3639 has been accepted in Bristol meeting and will become part of C++14, as well as a library counter-part "dynarray". So using compiler with C++14 support we can start writing something like:

    void func(int n)
    {
        int arr[n];
    }
    

    Or use dynarray:

    #include 
    
    void func(int n)
    {
        std::dynarray arr(n);
    }
    

提交回复
热议问题