什么是 C 和 C++ 上下文中的活动记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1266233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:22:58  来源:igfitidea点击:

What is activation record in the context of C and C++?

c++c

提问by vehomzzz

What does it mean and how important to know about it for a C/C++ programmers?

对于 C/C++ 程序员来说,了解它意味着什么以及了解它有多重要?

Is it the same across the platforms, at least conceptually?

至少在概念上,跨平台是否相同?

I understand it as a block of allocated memory used to store local variable by a function...

我将其理解为用于通过函数存储局部变量的已分配内存块...

I want to know more

我想了解更多

回答by Alexandre Bell

An activation record is another name for Stack Frame. It's the data structure that composes a call stack. It is generally composed of:

激活记录是堆栈帧的另一个名称。它是组成调用堆栈的数据结构。它一般由以下组成:

  • Locals to the callee
  • Return address to the caller
  • Parameters of the callee
  • 被叫方的本地人
  • 将地址返回给调用者
  • 被调用者参数

The Call Stack is thus composed of any number of activation records that get added to the stack as new subroutines are added, and removed from the stack (usually) as they return.

因此,调用堆栈由任意数量的激活记录组成,这些激活记录在添加新子例程时添加到堆栈中,并在它们返回时(通常)从堆栈中删除。

The actual structure and order of elements is platform and even implementation defined.

元素的实际结构和顺序是平台甚至实现定义的。

For C/C++ programmers, general knowledgeof this structure is useful to understand certain implementation features like Calling Conventions and even why do buffer overflows allow 3rd party malicious code to be ran.

对于 C/C++ 程序员,此结构的一般知识对于理解某些实现功能(如调用约定)以及为什么缓冲区溢出允许运行 3rd 方恶意代码非常有用。

A more intimate knowledgewill further the concepts above and also allow a programmer to debug their application and read memory dumps even in the absence of a debugger or debugging symbols.

深入的知识将进一步加深上述概念,并且即使在没有调试器或调试符号的情况下,程序员也可以调试他们的应用程序和读取内存转储。

More generally though, a C/C++ programmer can go by a large portion of their hobbyist programming career without even giving the call stack a moments thought.

更一般地说,C/C++ 程序员可以在他们业余编程生涯的大部分时间里,甚至不考虑调用堆栈。

回答by CB Bailey

activation recordisn't a concept that is used much in talking about C or C++ langauges themselves. The format of activation recordsis very much platform specific.

活动记录不是一个在谈论 C 或 C++ 语言本身时经常使用的概念。激活记录的格式非常特定于平台。

Conceptually, how parameters are passed, the lifetimes of local variables, where functions return to and how the call stack is unwound in response to an expection throw are all important parts of C++ and (with the exception of the latter C). The details of how these are implemented will affect what an activation recordlooks like for a particular platform but knowledge of this is not usually necessary for writing code in C++ or C.

从概念上讲,参数如何传递、局部变量的生命周期、函数返回到何处以及调用堆栈如何展开以响应预期抛出都是 C++ 和(C++ 除外)的重要部分。这些如何实现的细节将影响特定平台的激活记录是什么样的,但对于用 C++ 或 C 编写代码通常不需要了解这方面的知识。

回答by u6949852

When we call function, we need someplace to store callers and callees' context, this place is called activation record(AKA stack frame).

当我们调用函数时,我们需要某个地方来存储调用者和被调用者的上下文,这个地方称为活动记录(又名堆栈帧)。

Yes, activation recordscompose call stack, however, that doesn't mean activation recordsmust be stack-based. It is implementation specific.

是的,激活记录组成调用栈,但是,这并不意味着激活记录必须是基于栈的。它是特定于实现的

You may wonder "Any examples?".

你可能想知道“有什么例子吗?”。

  • Of course, just take a look at IBM mainframes' stackless design, the stackis not available, its activation recordis heap-based.
  • Opposite, there is also the platform which doesn't provide heap(AKA heap-less), e.g., Arduino(but it also means newkeyword and new-expressioncannot be used).
  • Apart from hardware limitation, some functional languages cannot store local variableson stack, so their activation recordsare allocated on heap, if you wonder the reason, hereis a good reference.
  • 当然,看看IBM大型机的stackless设计stack是不可用的,它的活动记录heap-based
  • 相反,也有不提供的平台(AKA heap-less),例如Arduino(但这也意味着不能使用new关键字和new-expression)。
  • 除了硬件限制,一些函数式语言不能在上存储局部变量,所以它们的激活记录分配在堆上,如果你想知道原因,这里是一个很好的参考。

Just like @FrakHB said, not only heapand stack, other regionsof memory could also be activation record, that's what implementation specificmeans.

正如@FrakHB 所说,不仅是heapstack,其他内存区域也可以是活动记录,这就是具体实现的意思。

回答by Jas Wanth

The activation record contains the following three things

激活记录包含以下三件事

1.Function definition
2.Variable definition
3.Function application(function call)

1. 函数定义
2.变量定义
3.函数应用(函数调用)