c++ - 函数从栈中返回的地址是什么?

标签 c++ gdb registry stack stack-frame

我有以下代码

int isBST(struct node* node) 
{ 
  return(isBSTUtil(node, INT_MIN, INT_MAX)); 
} 

int isBSTUtil(struct node* node, int min, int max) 
{
  if (node==NULL) 
     return 1;      

  if (node->data <= min || node->data > max) 
     return 0; 

  return
    isBSTUtil(node->left, min, node->data) &&  // Allow only distinct values
    isBSTUtil(node->right, node->data, max);  // Allow only distinct values
}

当我在 GDB 中调试代码时,我看到第二个参数设置为地址 ebp + 0xc (0xbffff188+0xc),第三个参数设置为 ebp + 0x10,第一个参数不清楚在哪里,在理论上,我们知道函数的返回地址位于 EBP + 4 ,第一个参数位于 EBP +8 并且......从我所知道的是什么?

最佳答案

从理论上讲,我们对参数或参数的位置一无所知 返回地址位于。在特定的架构上,我们 可以(通常)弄清楚特定编译器的作用 检查一些生成的汇编器。 第一的事情 examine 是可预见的功能。在 Intel 32 位处理器上, 帧指针将在 EBP 中,并将在 之后设置 一定数量的推送保存寄存器。 (尤其, 在为本地设置 EBP 之前必须有一个 push EBP frame.) 一个典型的 Intel preable 可能是:

function:
        PUSH EBP
        MOV  EBP, ESP
        SUB  ESP, n         ;   allocate space for local variables

除此之外:Intel 堆栈缩小,编译器 因为英特尔几乎普遍将论点从右推到 左边,所以在你的情况下,max 将具有最高地址, min 将在它的正下方,node 在其下方。所以你的 框架的图像将是:

[EBP - ...]     local variables
[EBP + 0]       the pushed old EBP
[EBP + 4]       the return address
[EBP + 8]       the first argument (node)
[EBP + 12]      the second argument (min)
[EBP + 16]      the third argument (max)

(假设参数都是 32 位值。) 当然,编译器可能会在之前压入额外的寄存器 插入 EBP,导致偏移相应地更高。 这只是一种可能的布局。

关于c++ - 函数从栈中返回的地址是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20000894/

相关文章:

c# - 如何从 C# 运行 PowerShell 脚本

c++ - 类成员 STL 容器 (const std::array) 的编译时创建,其中填充了元素

c++ - 未知来源的警告 : "can' t find linker symbol for virtual table for. .."

c++ - 指针差异是否是在基于范围的 for 循环中查找 vector 中元素索引的有效方法?

c - Eclipse CDT 中的 GDB 崩溃 : Error in `gdb' : free(): invalid next size (fast):

c -/lib/x86_64-linux-gnu/libthread_db.so.1 文件不存在

c++ - 实现二叉树时双重释放或损坏

linux - 在带有单个串行连接的远程设备上使用 gdbserver?

c# - 如何在 C# 中添加嵌套的注册表项/值?

C++/Qt : how to access windows registry remotely?