How can a stack underflow happen in C++?

后端 未结 4 1521
半阙折子戏
半阙折子戏 2020-12-25 15:06

What is a simple example in C++ that causes a stack underflow in the case of invoking and returning from method calls?

I am familiar with the calling convention, i.e

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-25 15:56

    The only way I can see this actually happening would be if you declared a function to use the stdcall (or any other calling convention that specifies the callee clean the stack) and then invoke the function through a function pointer that was specified as a cdecl (or any other calling convention where the stack is cleaned by the caller). If you do that, the called function will pop the stack before returning and then the caller would also pop the stack leading to underflow and terrible things.

    In the specific case of member functions, the calling convention is usually referred to as thiscall and whether the caller or the callee cleans the stack depends on the compiler.

    See here for details of calling conventions.

提交回复
热议问题