home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: Static global initialization outside of main() legal?
- Message-ID: <86256@ut-emx.uucp>
- Date: 6 Jan 93 22:17:12 GMT
- References: <1992Dec31.191123.7222@netcom.com> <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi> <1993Jan4.153806.1062@ucc.su.OZ.AU>
- Reply-To: jamshid@emx.utexas.edu
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 64
-
- In article <1993Jan4.153806.1062@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- >In article <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi> marc.girod@ntc.nokia.com writes:
- >>The most important is
- >>guaranteed: that "the initialisation [...] in a translation unit is
- >>done before the first use of any function or object defined in that
- >>translation unit".
- >
- > This is not guarranteed at all, even if it says it is!
-
- Right. I never understood why the ARM says that. Maybe r3.4 should
- instead read:
-
- the initialisation [...] in a translation unit is done before the
- first use **(not related to the initialization of another static
- object)** of any function or object defined in that translation unit
-
- >>There remains the "pathologic" case of mutual
- >>dependencies, dealt with in the annotation part of the ARM for 3.4,
- >
- > This is not how it works. There is no general solution
- >to the problem, so no algorithms can be written to solve it.
-
- Right, there's no solution to:
- //file one.cpp
- extern int y;
- int x = y+1;
- //file two.cpp
- int y = x +1;
-
- but Marc may be referring to the situation where the initialization of
- one static uses another static object (eg, it uses a global
- 'error_log' stream). The ARM commentary demonstrates the
- 'nifty-counter' technique which would require a file-scope static
- object in the error_log.h file that would initialize the global
- error_log. One thing the ARM doesn't mention about this technique is
- that the constructor used for the 'error_log' definition must *not*
- modify the object since it may be called *after* one of the static
- initializers initializes 'error_log'. (err, is that clear?)
-
- > Typically, what happens is static variables are initialised
- >to a constant value first (0 if not otherwise specified)
-
- I have a question about this. The ARM seems to only guarantee that
- static objects are initialized to 0 before any dynamic initializations.
- (I assume this does not necessarily mean all-bits-zero in the case of
- pointers and floats/doubles). Does that mean there's no guarantee that
- 'x' below will be 5 during the construction of a static in another file?
- int x = 5; // 'x' is a global
-
- Btw, I believe even the following is considered a dynamic initialization:
- int x;
- int* p = &x;
- and therefore there's no guarantee that 'p' points to 'x' during the
- construction of other globals.
-
- >then modules get initialised in the order of linkage,
- >and variables in each module in the order of writing.
-
- I hope the Working Papers have clarified 3.4. Can someone with access
- please outline the changes?
-
- Thanks, Jamshid Afshar
- jamshid@emx.utexas.edu
-
-