The function main() immediately invokes parent(). The first thing that the processor sees in parent() is the declaration of intParent. At that point, intParent goes into scope — that is, intParent is defined and available for the remainder of the function parent(). The second statement in parent() is the call to child(). Once again, the function child() declares a local variable, this time intChild. The scope of the variable intChild is limited to the function child(). Technically, intParent is not defined within the scope of child() because child() doesn’t have access to intParent; however, the variable intParent continues to exist while child() is executing. When child() exits, the variable intChild goes out of scope. Not only is intChild no longer accessible, it no longer exists. (The memory occupied by intChild is returned to the general pool to be used for other things.) |
|
|
|
|
When it gets created second or third time, does it have the same memory location as the first time it was called? |
The following code segment compiles without error but doesn’t work (don’t you just hate that?) The problem with this function is that dLocalVariable is defined only within the scope of the function child(). Thus, by the time the memory address of dLocalVariable is returned from child(), it refers to a variable that no longer exists. The memory that dLocalVariable formerly occupied is probably being used for something else. |
|
|