函数的静态和动态父函数

发布于 2024-08-27 01:30:35 字数 379 浏览 4 评论 0原文

我正在阅读《Thinking in C++》(第 2 卷):

每当调用函数时, 有关该功能的信息是 推入运行时堆栈 激活记录实例(ARI),也 称为堆栈帧。典型的堆栈 帧包含(1)的地址 调用函数(因此执行可以 返回它),(2)指向 ARI 的指针 函数的静态父函数( 词法上包含的范围 称为函数,因此变量是全局的 可以访问该函数),并且 (3) 指向调用函数的指针 它(它的动态父级)。所走的路 逻辑上的结果是重复的 跟随动态父链接的是 动态链或调用链

我无法理解作者作为函数的静态和动态父级的含义。我也无法区分第 1、2 或 3 项。它们似乎都是相同的。有人可以向我解释一下这段文字吗?

I'm reading Thinking in C++ (vol. 2):

Whenever a function is called,
information about that function is
pushed onto the runtime stack in an
activation record instance (ARI), also
called a stack frame. A typical stack
frame contains (1) the address of the
calling function (so execution can
return to it), (2) a pointer to the ARI of
the function’s static parent (the
scope that lexically contains the
called function, so variables global
to the function can be accessed), and
(3) a pointer to the function that called
it (its dynamic parent). The path that
logically results from repetitively
following the dynamic parent links is
the dynamic chain, or call chain

I'm unable to comprehend what the author means as function's static and dynamic parent. Also am not able to differentiate between item 1, 2 or 3. They all seem to be the same. Can someone please explain this passage to me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぶ宁プ宁ぶ 2024-09-03 01:30:35

我认为这个说法不是关于C++,而是关于堆栈框架的一般结构。
1) 是返回地址-主函数中调用之后的指令地址。当执行 return 时,它将从堆栈中弹出并执行到该点(对 C++ 有效)
2) 和 3) 对于允许嵌套函数的语言有效。 (函数内部声明的函数)此类函数可以访问父级的变量,因此它们具有到父级堆栈帧的链接(静态链接),而动态链接是为了使该函数能够递归地调用自身

I think this statement is not about C++ but general structure of stack frame.
1) is return address - address of instruction after call in main function. when return is performed it will be poped from stack and execution will go to that point (valid for c++)
2) and 3) are valid for languages that allow nested functions. (Function declared inside function) such functions may have access to parent's variables, so they have link (static link) to parent's stack frame and dynamic link is for this functions to be able call themselves recursively

可是我不能没有你 2024-09-03 01:30:35

这一切对我来说听起来很奇怪。静态帧指针通常用在具有词法作用域的语言中,例如函数式语言以及具有嵌套函数的 pascal 系列。全局变量在编译时或运行时绑定一次,并且不需要帧指针。 (1) 是有效的,但 (2) 在 C++ 中不存在,据我所知。

我怀疑 (3) 是指父框架指针。调用堆栈通常设置为链表,以便调试器和相关工具可以遍历它们,而无需深入了解程序。

This all sounds very odd to me. Static frame pointers are normally used in languages with lexical scope, such as functional languages, and the pascal family with their nested functions. Globals are bound once either at compile time or runtime, and shouldn't need frame pointers. (1) is valid, but (2) doesn't exist in C++, AFAIK.

I suspect that (3) was meant to refer to the parent frame pointer. Call stacks are usually setup as linked lists so that debuggers and related tools can walk them without requiring deep knowledge of the program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文