Game Development Reference
In-Depth Information
When the compiler creates the code to push the stack frame for function1 onto the stack it also
ensures that the parameter variable is initialized with the value stored in variable from _tmain . This
is how parameters are passed by value. Finally, Figure 22-3 shows the last stack frame for function2
added to the stack.
function2.variable1 = function1.variable
function2.variable2 = function2.variable1
function1.variable = _tmain.variable
_tmain.variable = 0
Figure 22-3. The complete stack frame
The last stack frame is a little more complicated but you should be able to see how the literal
value 0 in _tmain has been passed all the way along the stack until it is eventually used to initialize
variable2 in function2 .
The remaining stack operations are relatively simple. When function2 returns the stack frame
generated for that call is popped from the stack. This leaves us back at the state presented in
Figure 22-2 , and when function1 returns we are back at Figure 22-1 . That's all you need to know to
understand the basic functionality of a stack in C++.
Unfortunately things aren't actually this simple. The stack in C++ is a very complicated thing to fully
understand and requires a bit of assembly programming knowledge. That topic is outside the scope
of a book aimed at beginners, but it's well worth pursuing once you have a grasp of the basics. The
article “Programmers Disassemble” in the September 2012 edition of Game Developer Magazine is
an excellent introductory article on the operation of the x86 stack and well worth a read, available
free from http://www.gdcvault.com/gdmag .
This chapter hasn't covered the ins and outs of how references and pointers are handled on the
stack or how return values are implemented. Once you begin to think about this, you might begin to
understand how complicated it can be. You might also be wondering why it's useful to understand
how the stack works. The answer lies in trying to work out why your game has crashed once it is in
a live environment. It's relatively easy to work out why a game crashes while you are developing, as
you can simply reproduce the crash in a debugger. On games that have launched, you might receive
a file known as a crash dump, which does not have any debugging information and simply has the
current state of the stack to go on. At that point you need to look out for the symbol files from the
build that let you work out the memory addresses of the functions that have been called, and you
can then manually work out which functions have been called from the addresses in the stack and
also try to figure out which function passed along an invalid memory address of value on the stack.
This is complicated and time-consuming work, but it does come up every so often in professional
game development. Services such as Crashlytics for iOS and Android or BugSentry for Windows PC
programs can upload crash dumps and provide a call stack for you on a web service to help alleviate
a lot of the pain from trying to manually work out what is going wrong with your game.
The next big topic in memory management in C++ is the heap.
 
Search WWH ::




Custom Search