opensubscriber
   Find in this group all groups
 
Unknown more information…

u : unix-porting@lists.apple.com 22 December 2005 • 11:30PM -0500

Re: global static variable initialization is not working properly
by David Fang

REPLY TO AUTHOR
 
REPLY TO GROUP




Hi,
Initialiation ordering: Function-local static objects (previously
described) are the easiest solution, but the lifetime of such objects is
undefined, i.e.  their destruction time is unspecified, sometimes *never*
destroyed.  (Related to the halting problem:  when will a program
terminate or when is the last time a particular object will be
referenced?)  Fortunately, for most programs destruction ordering at
exit-time is moot, the program's memory is released anyways, so who
cares(?).
Initialization missing(!):  With the Mach lazy linker, an entire
module (.o) may not even be linked into the final executable at link-time
if the linker thinks a module will not be used -- this leads to missing
modules (and their static objects) at run time.  In my experience, the
linker has thought wrong on many accounts (not a bug), so to coerce
linkage, I will insert a line of code somewhere that certainly uses a
symbol in the module in question, such as a non-inline Object::Object()
constructor.  This then triggers linkage of that module at link time.
(Results evident from change in resulting binary's size!)  Note that link
*ordering* is still undefined.
If you have an entire hierarchy of static object
initialization-ordering dependencies, you can use the function-local
static technique repeatedly with "requires-provides" relationships to make
sure dependencies are met.  Perhaps somewhat painful, sometimes necessary.

Hope this sheds some light.

> > I am facing one strange problem, in which i am not able to
> > get static object initialized completely or properly before
> > its use, and hence it is giving unexpected results.
> >
> > Is there any compile time option to force such initialization?
> > Or is there any other issue which creates such problems?
>
> Initialize the object inside a function.  Then it will always be
> initialized  before it's used.  The order in which global static
> objects are initialized isn't reliable, but objects in a function are
> always initialized before the function is called.
>
> Object& getObject() {
>    static Object object;
>    return object;
> }


David Fang
Computer Systems Laboratory
Electrical & Computer Engineering
Cornell University
http://www.csl.cornell.edu/~fang/
-- (2400 baud? Netscape 3.0?? lynx??? No problem!)

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (Unix-porting@list...)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/subscriber%40opensubscriber.com

This email sent to subscriber@open...

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.