Game Development Reference
In-Depth Information
Remember that smart pointers work with a reference count, and each of the smart
pointer objects only has one reference. If either of them goes out of scope, the mem-
ory for the object will be deallocated, and the other smart pointer will point to
garbage.
Using Memory Correctly
Did you ever hear the joke about the programmer trying to beat the devil in a coding
contest? Part of his solution involved overcoming a memory limitation by storing a
few bytes in a chain of sound waves between the microphone and the speaker. That
'
s
'
'
an interesting idea, and I
s someone out there who has already done it.
Memory comes in very different shapes, sizes, and speeds. If you know what you ' re
doing, you can write programs that make efficient use of these different memory
blocks. If you believe that it doesn
ll bet there
re in for a real
shock. This includes assuming that the standard memory manager for your operating
system is efficient; it usually isn
'
t matter how you use memory, you
'
'
t, and you
'
ll have to think about writing your own.
Understanding the Different Kinds of Memory
The system RAM is the main warehouse for storage, as long as the system has power.
Video RAM (or VRAM) is usually much smaller and is specifically used for storing
objects that will be used by the video card. Some platforms, such as Xbox and
Xbox360, have a unified memory architecture that makes no distinctions between
RAM and VRAM. Desktop PCs run operating systems like Windows 7 and have vir-
tual memory that mimics much larger memory space by swapping blocks of little-
used RAM to your hard disk. If you
re not careful, a simple memcpy() could cause
the hard drive to seek, which to a computer is like waiting for the sun to cool off.
'
System RAM
Your system RAM is a series of memory sticks that are installed on the motherboard.
Memory is actually stored in nine bits per byte, with the extra bit used to catch mem-
ory parity errors. Depending on the OS, you get to play with a certain addressable
range of memory. The operating system keeps some to itself. Of the parts you get
to play with, it is divided into three parts when your application loads:
n Global memory: This memory never changes size. It is allocated when your
program loads and stores global variables, text strings, and virtual function
tables.
n Stack: This memory grows as your code calls deeper into core code, and it
shrinks as the code returns. The stack is used for parameters in function calls
 
 
 
Search WWH ::




Custom Search