Topic: static global objects


Author: leo@cs.kun.nl (Leo Willems)
Date: Sun, 21 Jun 1992 21:34:26 GMT
Raw View
I used to initialize some objects before main was entered by making them
global. Too avoid name clashes, these objects were declared static.

Now I read that *global static* object initialisation may be relaxed in the
future (lazy evaluation it is called), so my objects perhaps are not
initialised before main is called.

This forces me to make objects global and therefore, I have to assign them
unique names.
Now I could do that by using the preprocessor with the name2() and __LINE__FILE_
directives.
But since the global objects are already generated by a declare macro,
this won't work because preprocessors do (should) not work recursively.

What to do in the future? (besides implementing "eval" in cpp :-)

Thanks
--
Leo




Author: checker@acf3.nyu.edu (Christopher Hecker)
Date: 22 Jun 92 05:36:58 GMT
Raw View
>Now I read that *global static* object initialisation may be relaxed in the
>future (lazy evaluation it is called), so my objects perhaps are not
>initialised before main is called.

This may not mean what you think it means.  By global static the rule
means any global object statically allocated, not static scope.  So,
anytime you have this:

---<file1.cpp>---

extern foo Bar;  // declaration

---<file2.cpp>---

foo Bar; // definition

Bar is only guaranteed to be initialized before a function in file2.cpp
is called; you could reference it in file1.cpp before it existed.  This
alone is good enough reason to stay away from global objects.


Chris




Author: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
Date: Mon, 22 Jun 1992 07:48:36 GMT
Raw View
checker@acf3.nyu.edu (Christopher Hecker) writes:

>>Now I read that *global static* object initialisation may be relaxed in the
>>future (lazy evaluation it is called), so my objects perhaps are not
>>initialised before main is called.
>
>This may not mean what you think it means.  By global static the rule
>means any global object statically allocated, not static scope.  So,
>anytime you have this:
>
>---<file1.cpp>---
>
>extern foo Bar;  // declaration
>
>---<file2.cpp>---
>
>foo Bar; // definition
>
>Bar is only guaranteed to be initialized before a function in file2.cpp
>is called; you could reference it in file1.cpp before it existed.  This
>alone is good enough reason to stay away from global objects.
>
>
>Chris

I don't quite understand. Does this mean that if I define all my global
objects in a single file globals.cpp (with no functions in that file), then
they may never be initialized? Surely this would break a lot of existing
programs. Could someone please clarify this?

--
Fergus Henderson             fjh@munta.cs.mu.OZ.AU
This .signature VIRUS is a self-referential statement that is true - but
you will only be able to consistently believe it if you copy it to your own
.signature file!