Why no variable size array in stack?

后端 未结 7 1181
南方客
南方客 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 19:58

    Simple answer: because it is not defined in the C++ standard.

    Not so simple answer: Because no one propsed something behaving coherently for C++ in that case. From the standards POV there is no stack, it could be implemented totally differently. C99 has VLAs, but they seem to be so complex to implement that gcc only finished the implementation in 4.6. I don't think many people will want to propose something for C++ and see compiler manufacturers struggle with it for many years.

提交回复
热议问题