如何在C ++中发生堆栈下溢? [英] How can a stack underflow happen in C++?

查看:87
本文介绍了如何在C ++中发生堆栈下溢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,有什么简单的示例在调用和从方法调用返回时导致堆栈下溢?

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

我熟悉调用约定,即 thiscall stdcall cdecl 及其清理方式堆栈。编译器生成的代码不会自动处理堆栈下溢吗?

I am familiar with the calling convention, i.e thiscall, stdcall and the cdecl and way they would clean the stack. Wouldn't a stack underflow automatically be taken care of by the code generated by the compiler?

在哪些情况下会使我陷入堆栈下溢的麻烦?

What are the situations that can get me into trouble with stack underflow?

推荐答案

我看到这种情况的唯一方法是声明了使用的函数stdcall (或任何其他指定被调用方的调用约定清理堆栈),然后通过指定为 cdecl 的函数指针调用该函数(或调用方清理堆栈的任何其他调用约定)。如果这样做,被调用的函数将在返回之前弹出堆栈,然后调用者还将弹出堆栈,导致下溢和可怕的事情。

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.

成员函数,调用约定通常称为 thiscall ,并且调用方还是被调用方清除堆栈取决于编译器。

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.

这篇关于如何在C ++中发生堆栈下溢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆