Game Development Reference
In-Depth Information
and local variables. The stack has a fixed size that can be changed with compiler
settings.
n Heap: This memory grows and shrinks with dynamic memory allocation. It is
used for persistent objects and dynamic data structures.
Old-timers used to call global memory the DATA segment, harkening back to the
days when there used to be near memory and far memory. It was called that because
programmers used different pointers to get to it. What a disgusting practice! Every-
thing is much cleaner these days because each pointer is a full 32 or 64 bits. (Don
'
t
worry, I ' m not going to bore you with the When I went to school I only had 640k of
memory to play with
story.)
Your compiler and linker will attempt to optimize the location of anything you put
into the global memory space based on the type of variable. This includes constant
text strings. Many compilers,
including Visual Studio, will attempt to store text
strings only once to save space:
const char *error1 =
Error
;
const char *error2 =
Error
;
int main()
{
printf (
, (int)error1);
// How quaint. A printf.
printf (
%x\n
%x\n
, (int)error2);
return 0;
}
This code yields interesting results. You
ll notice that under Visual C++, the two
pointers point to the same text string in the global address space. Even better, the
text string is one that was already global and stuck in the CRT libraries. It
'
'
sasifwe
wasted our time typing
This trick only works for constant text strings, since
the compiler knows they can never change. Everything else gets its own space. If you
want the compiler to consolidate equivalent text strings, they must be constant text
strings.
Don
Error.
t make the mistake of counting on some kind of rational order to the global
addresses. You can ' t count on anything the compiler or linker will do, especially if
you are considering crossing platforms.
On most operating systems, the stack starts at high addresses and grows toward lower
addresses. C and C++ parameters get pushed onto the stack from right to left
'
the
 
Search WWH ::




Custom Search