www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/16/20:45:18

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Register Variable Question?
Date: Mon, 16 Dec 1996 21:06:30 +0000
Organization: None
Lines: 43
Distribution: world
Message-ID: <7lld+CAWnbtyEwMN@talula.demon.co.uk>
References: <19961215 DOT 150724 DOT 3694 DOT 0 DOT MSnakes AT juno DOT com>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Matt J Reiferson writes:
>I want to have a couple of global register int variables in my program to
>get the most speed out of a couple of functions, when I compile I get an
>error message saying:
>       register name not specified for 'int sliver_column'
>
>The line it refers to is:
>       register int sliver_column

I very much doubt that global variables can be allocated to registers.
Think what this would mean: no code, anywhere in your program, could use
these registers! Because of the way C compiles each source files as an
individual unit, there is no way that this could be enforced.

You shouldn't need to explicitly declare them as register variables,
though. The gnu optimiser is quite smart enough to temporarily move
variables into registers as required (eg. when they are being used
inside a tight loop), and then to later swap them out in favour of
something else. It is far better to leave this sort of decision to the
compiler: it's more portable, and the compiler has a lot more control
over exactly where things go that you would with a simple 'register'
definition.

If you are really concerned about efficiency, though, you shouldn't make
the variables global. Think about it: every time you call an external
function, the compiler has no way of knowing if the variable has been
changed. For example, if foo is a global int, the code:

        for (i=0; i<foo; i++)
                do_something();

will have to examine the global var in memory each time around the loop,
becuase do_something() might have changed the value of foo. If you make
foo a local variable, though, the optimiser knows that it's can't be
changed by anything except the loop instruction, so it will load it into
a register for you.

Trust the optimiser - it does a very good job :-)

/*
 *  Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
 *  Ghoti: 'gh' as in 'enough', 'o' as in 'women', and 'ti' as in 'nation'.
 */

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019