Variable Location
#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.