[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Allocation

On Wed, May 01, 2002 at 10:07:29PM -0500, Steve Baker wrote:
> Igor Cabral Corr?a wrote:

> > I'm not making a game now, but I thinking something related with game
> > creation. Maybe you could aid me, please...
> > If I declare all variables of my program global, giving to the OS the task of
> > managing virtual/phisical memory for these variables, I have significant loss
> > of speed than when I manage the blocks of memory with malloc?

> It doesn't matter whether your variables are global, local or dynamically
> allocated onto the heap - Linux will manage them for you in *exactly* the
> same way.

> Like I said before - "Just don't worry about it."

> Otherwise, you might get a *tiny* speed advantage by using globals - but
> the ugliness that this induces into your code design isn't worth it.

For all but the most trivial of programs they'll be a speed penalty for
declaring all variables global.  The instructions for globals tend to be
bigger and the storage space for local variables can be reused.  So with
all globals you program will have a larger memory footprint with all that
implies.  Also very short duration local variables can spend all their
lives in registers and have their storage optimised out completely.

You should define your variables with the appropriate scope so that
the compiler can manage them correctly.

Christopher John Purnell  | I thought I'd found a reason to live
http://www.lost.org.uk/   | Just like before when I was a child
--------------------------| Only to find that dreams made of sand
What gods do you pray to? | Would just fall apart and slip through my hands