Game Development Reference
In-Depth Information
last parameter is the first to get pushed onto the stack in a function call. Local para-
meters get pushed onto the stack in their order of appearance:
void testStack(int x, int y)
{
int a = 1;
int b = 2;
printf(
&x= %-10x &y= %-10x\n
, &x, &y);
printf(
&a= %-10x &b= %-10x\n
, &a, &b);
}
This code produces the following output:
&x= 12fdf0 &y= 12fdf4
&a= 12fde0 &b= 12fdd4
Stack addresses grow downward to smaller memory addresses. Thus, it should be
clear that the order in which the parameters and local variables were pushed was: y,
x, a, and b, which turns out to be exactly the order in which you read them
a good
'
'
mnemonic. The next time you
ll be glad to
understand this, especially if you are setting your instruction pointer by hand.
C++ allows a high degree of control over the local scope. Every time you enclose
code in a set of braces, you open a local scope with its own local variables:
re debugging some assembler code, you
int main()
{
int a = 0;
{
// start a local scope here
int a = 1;
printf(
%d\n
, a);
}
printf(
%d\n
, a);
}
This code compiles and runs just fine. The two integer variables are completely sepa-
rate entities. I
d never actually
write code like this. Doing something like this is likely to get you shot. The real use-
fulness of this kind of code is for use with C++ objects that perform useful tasks
when they are destroyed
'
ve written this example to make a clear point, but I
'
you can control the exact moment a destructor is called
by closing a local scope.
 
Search WWH ::




Custom Search