------------------------------------------------------------------------------
MC logo
Variable Location
[^] Functions
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Call-by-Reference] [Call-by-Value-Result] [Call-by-Name] [Dyn Link Example] [Dyn Link Example] [C++ Translation of Java Dyn Link] [Variable Location] [Parameter Passing Method Exercise]
varloc.cpp
#include <iostream>

using namespace std;

/*
 * When using a stack, the locations of variables will differ depending on 
 * the calling sequence.
 */
void bilgewater(int n)
{
        int q = n - 1;
        cout << "q is located at " << (void *)&q << endl;
        if(q > 0) bilgewater(q);
        cout << "q at " << (void *)&q << " ceases to be." << endl;
}

void dimwaddle(double pct)
{
        int q = 19837;
        cout << pct << " of " << q << " is " << q*pct/100.0 << endl;
        bilgewater(1);
}

main()
{
        bilgewater(1);
        dimwaddle(35.9);
        bilgewater(2);
}

Specific output is system-dependent, but I have:
q is located at 0xbfbefdac
q at 0xbfbefdac ceases to be.
35.9 of 19837 is 7121.48
q is located at 0xbfbefd6c
q at 0xbfbefd6c ceases to be.
q is located at 0xbfbefdac
q is located at 0xbfbefd7c
q at 0xbfbefd7c ceases to be.
q at 0xbfbefdac ceases to be.
Notice that the location of q varies depending on the calling sequence, since the stack frame falls in different places.