Why are stack overflows still a problem?

前端 未结 11 1468
故里飘歌
故里飘歌 2020-12-25 10:22

This question is mystifying me for years and considering this site\'s name, this is the place to ask.

Why do we, programmers, still have this StackOverflow

11条回答
  •  梦毁少年i
    2020-12-25 10:55

    Any code that would cause a stack overflow on a typical static-length stack is wrong anyway.

    • You could make the stack a std::vector-like object, but you'd have extremely unpredictable performance when it decided to resize -- and anyway, it would most likely just keep doing it until all the heap was exhausted too, and that's more annoying.
    • You could make it like a std::list, where it grew at O(1). However, the pointer arithmetic used on a static stack is so totally critical in every way to program performance that it would be uselessly slow. Languages were invented to have one return value and arbitrary numbers of input parameters because that's what fit the static stack/pointer arithmetic paradigm.

    So a dynamically resizable stack would be A) a performance nightmare and B) of no value anyway, since your stack shouldn't have gotten that deep.

提交回复
热议问题