[LC++]Scoping question

Jack Lloyd lloyd at acm.jhu.edu
Mon Jul 22 23:52:06 UTC 2002


On Mon, 22 Jul 2002, Vincent Penquerc'h wrote:

> There is a thing you can rely on, IIRC, that globals in a
> single compilation unit are initialized in the order they
> are declared. I think destruction will happens in the
> opposite order, so if you can declare y as a global, then
> you'd be fine.
> Hmm, as I write this, I become less and less sure about
> what I'm saying.... :)

I looked last night, and here is what is defined:

Two globals defined in the same file have a fixed construction/destruction
order, exactly as you suggest it. If they're not in the same file, all bets
are off, the compiler/linker can do it however it likes.

Static variables declared inside a function are first initialized when that
function is called (I should have realized this). As far as I can tell,
there is no way to tell when they'll be destroyed. Hopefully I just missed
it, but I'm starting to think it's not defined, or if it is defined, it's
defined in a thouroughly non-useful way (/me sad).

Creating all of them in the same file is not really useful in my case,
cause it's a whole lot of code. I basically worked around it by making
pointers to the objects in question, then having the initializer/shutdown
functions handle the allocation/deletion of them so I can be sure of the
ordering.

Thanks for taking the time to reply.

-Jack





More information about the tuxCPProgramming mailing list