We trace recursion using an internal stack.How can we relate this to an activation record **?i.e I want to know the relation** between an internal stack and an activation record.

Recommended Answers

All 6 Replies

The relation is that the record is stored on the stack.

PS: This happens for all function calls (that aren't inlined by the compiler), not just recursive ones.

also had another doubt....what is meant by static scope **and **dynamic scope?

Suppose i have 2 functions (main) and (fact).The activation record for main knows the address of fact so that it can call it.How? Do main and fact functions link before compiling?

what is meant by static scope and dynamic scope?

The scope affects whether a given variable can be accessed from a given location in the code and, if there are multiple variables with the same name, which one is meant by a given use of the name.

Using static scope you can use variables that are defined globally or inside a code block that you're nested in. This means that, when using nested and/or anonymous functions (in languages that support them), such a function can access variables of the enclosing function even after that function is no longer running. If multiple variables with the same name could be accessed by that logic, the inner-most one is chosen.

Using dynamic scope you can use variables that are defined globally or which exist in any activation record currently on the stack. This means that you can access local variables of another function if (and only if) that function called you. If multiple variables with the same name could be accessed by that logic, the one that has been most recently pushed onto the stack is chosen.

Suppose i have 2 functions (main) and (fact).The activation record for main knows the address of fact so that it can call it.How? Do main and fact functions link before compiling?

(Assuming the functions are defined in different compilation units - otherwise linking is irrelevant) No, linking happens after compilation. The address of fact is simply not known until linking. Before then, the object file simply contains a symbol as a placeholder instead of the address. The linker then replaces that placeholder with the actual address.

PS: If I may ask, where do these questions come from? It sounds like you're working through a compilers book (or following a compilers course), but such a book/course should already contain the information you're looking for.

Yes you happen to be right.I have been reading a book on compiler construction but still the picture was blur in my mind.

So,in dynamic scoping if the varibles are declared in main and then assigned values there and then main calls fact() then the fact() would be able to access the variables(local) of main() because the activation record of main()
is still on the stack?

Yes, exactly.

It should be noted that this is generally seen to be very confusing behavior and most modern languages don't support dynamic scope and those that do don't tend to use it as the default.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.