home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18777 < prev    next >
Encoding:
Text File  |  1993-01-06  |  3.0 KB  |  76 lines

  1. Path: sparky!uunet!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Static global initialization outside of main() legal?
  5. Message-ID: <86256@ut-emx.uucp>
  6. Date: 6 Jan 93 22:17:12 GMT
  7. References: <1992Dec31.191123.7222@netcom.com> <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi> <1993Jan4.153806.1062@ucc.su.OZ.AU>
  8. Reply-To: jamshid@emx.utexas.edu
  9. Organization: The University of Texas at Austin; Austin, Texas
  10. Lines: 64
  11.  
  12. In article <1993Jan4.153806.1062@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13. >In article <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi> marc.girod@ntc.nokia.com writes:
  14. >>The most important is
  15. >>guaranteed: that "the initialisation [...] in a translation unit is
  16. >>done before the first use of any function or object defined in that
  17. >>translation unit". 
  18. >
  19. >    This is not guarranteed at all, even if it says it is!
  20.  
  21. Right.  I never understood why the ARM says that.  Maybe r3.4 should
  22. instead read:
  23.  
  24.     the initialisation [...] in a translation unit is done before the
  25.     first use **(not related to the initialization of another static
  26.     object)** of any function or object defined in that translation unit
  27.  
  28. >>There remains the "pathologic" case of mutual
  29. >>dependencies, dealt with in the annotation part of the ARM for 3.4,
  30. >
  31. >    This is not how it works. There is no general solution
  32. >to the problem, so no algorithms can be written to solve it.
  33.  
  34. Right, there's no solution to:
  35.     //file one.cpp
  36.     extern int y;
  37.     int x = y+1;
  38.     //file two.cpp
  39.     int y = x +1;
  40.  
  41. but Marc may be referring to the situation where the initialization of
  42. one static uses another static object (eg, it uses a global
  43. 'error_log' stream).  The ARM commentary demonstrates the
  44. 'nifty-counter' technique which would require a file-scope static
  45. object in the error_log.h file that would initialize the global
  46. error_log.  One thing the ARM doesn't mention about this technique is
  47. that the constructor used for the 'error_log' definition must *not*
  48. modify the object since it may be called *after* one of the static
  49. initializers initializes 'error_log'. (err, is that clear?)
  50.  
  51. >    Typically, what happens is static variables are initialised
  52. >to a constant value first (0 if not otherwise specified)
  53.  
  54. I have a question about this.  The ARM seems to only guarantee that
  55. static objects are initialized to 0 before any dynamic initializations.
  56. (I assume this does not necessarily mean all-bits-zero in the case of
  57. pointers and floats/doubles).  Does that mean there's no guarantee that
  58. 'x' below will be 5 during the construction of a static in another file?
  59.     int x = 5;  // 'x' is a global
  60.  
  61. Btw, I believe even the following is considered a dynamic initialization:
  62.     int x;
  63.     int* p = &x;
  64. and therefore there's no guarantee that 'p' points to 'x' during the
  65. construction of other globals.
  66.  
  67. >then modules get initialised in the order of linkage, 
  68. >and variables in each module in the order of writing.
  69.  
  70. I hope the Working Papers have clarified 3.4.  Can someone with access
  71. please outline the changes?
  72.  
  73. Thanks, Jamshid Afshar
  74. jamshid@emx.utexas.edu
  75.  
  76.