51

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

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

vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • 8
    AKA stack frame - see http://en.wikipedia.org/wiki/Activation_record#Structure –  Aug 12 '09 at 13:43
  • 1
    @Neil, your comment should have been the answer. – Kirill V. Lyadvinsky Aug 12 '09 at 13:51
  • I agree, but I think @LB beat him :) – vehomzzz Aug 12 '09 at 13:52
  • 1
    When you have too many of them, they generate a really cool website about programming questions. – Tyler McHenry Aug 12 '09 at 13:55
  • 2
    Actually, he didn't, but I don't like posting answers that consist only of a wikipedia link. –  Aug 12 '09 at 13:56
  • 3
    Frames for function call are not required to be implemented in stack. Both ISO C and ISO C++ mention nothing about "stack" except for "stack unwinding" in Clause 15 of ISO C++, which is basically "LIFO destruction of automatic objects", with nothing to do except for implementation details. Call stacks are actually allowed to be allocated from heap or other region of memory rather than ISA-specific stack. – FrankHB Mar 24 '16 at 04:04

4 Answers4

68

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 previous stack pointer (SP) value

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 knowledge of 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.

A more intimate knowledge will 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.

yes sure
  • 97
  • 2
  • 11
Alexandre Bell
  • 3,141
  • 3
  • 30
  • 43
  • @Alexandre Bell I understand from what you mention that the AR (stack frame) contains local vars, ret address and params. My question is: where is the code for the function being called stored? – Fernando Gabrieli Oct 01 '16 at 16:44
  • 4
    @FernandoGabrieli There are different segments in C like code segment, stack segment, heap segment. so the function code will be stored on code segment. refer [this](http://www.geeksforgeeks.org/memory-layout-of-c-program/) – Abhisek Jan 26 '17 at 09:14
7

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

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 record looks like for a particular platform but knowledge of this is not usually necessary for writing code in C++ or C.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
5

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

Yes, activation records compose call stack, however, that doesn't mean activation records must be stack-based. It is implementation specific.

You may wonder "Any examples?".

  • Of course, just take a look at IBM mainframes' stackless design, the stack is not available, its activation record is heap-based.
  • Opposite, there is also the platform which doesn't provide heap(AKA heap-less), e.g., Arduino(but it also means new keyword and new-expression cannot be used).
  • Apart from hardware limitation, some functional languages cannot store local variables on stack, so their activation records are allocated on heap, if you wonder the reason, here is a good reference.

Just like @FrakHB said, not only heap and stack, other regions of memory could also be activation record, that's what implementation specific means.

Chen Li
  • 4,824
  • 3
  • 28
  • 55
-3

The activation record contains the following three things

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

ice1000
  • 6,406
  • 4
  • 39
  • 85
Jas Wanth
  • 1
  • 1