[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Globals vs. Locals
- From: David Given <dg@...>
- Date: Sat, 6 Aug 2005 11:52:04 +0100
On Saturday 06 August 2005 07:34, Rici Lake wrote:
> However, as the above example demonstrates, they work pretty well in
> the same way: the compiled C file contains a string which names the
> variable, and it is looked up in a hash table (at least in ELF format).
I should point out that this is *only* true if you use dlsym() to look up
symbols by name. When the final binary is linked together, all globals are
resolved to individual memory locations and all the names can be discarded.
Actually running code does not involve any run-time lookups --- the addresses
of all globals are hard-coded.
> I did cheat a bit in the above example, of course. First, I told the
> linker not to complain about the fact that 'a' is defined by both
> main.c and a.c.
Try compiling the files the other way around...
The following link is to a IOCCC winner; it's a raytracer, written in C using
no reserved words (this means that all globals are declared implicitly). I
think this contains as an argument against doing this.
"Curses! Foiled by the chilled dairy treats of righteousness!" --- Earthworm
Description: PGP signature