Why no variable size array in stack?

后端 未结 7 1204
南方客
南方客 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条回答
  •  萌比男神i
    2020-11-27 19:58

    Stacks are fairly small, and their sizes can vary dramatically per architecture. The problem is that it is fairly easy to 'over-allocate' and cause a seg fault or write over memory owned by somebody else. Meanwhile, solutions to the problem (e.g. vector) have existed for a long time.

    FWIW, I read Stroustrup say that he didn't want them, but I don't know which interview it was in.

提交回复
热议问题