Game Engine Architecture

(Ben Green) #1

118 3. Fundamentals of Software Engineering for Games


void someFunction()
{
U32 anInteger;
// ...
}

Pushing and popping stack frames is usually implemented by adjusting
the value of a single register in the CPU, known as the stack pointer. Figure
3.10 illustrates what happens when the functions shown below are executed.

void c()
{
U32 localC1;
// ...
}
F32 b()
{
F32 localB1;
I32 localB2;
// ...

c(); // call function c()

// ...

return localB1;
}

void a()
{
U32 aLocalsA1[5];

// ...

F32 localA2 = b(); // call function b()

// ...
}

When a function containing automatic variables returns, its stack frame
is abandoned and all automatic variables in the function should be treated as
if they no longer exist. Technically, the memory occupied by those variables
is still there in the abandoned stack frame—but that memory will very likely
be overwritt en as soon as another function is called. A common error involves
returning the address of a local variable, like this:
Free download pdf