如何在C ++中发生堆栈下溢?

编程入门 行业动态 更新时间:2024-10-11 13:29:38
本文介绍了如何在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 ++中发生堆栈下溢?

本文发布于:2023-10-24 20:53:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   发生   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!