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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!news.funet.fi!ajk.tele.fi!funic!nokia.fi!newshost!girod
  3. From: girod@kilohp.kiloapo.ts.tele.nokia.fi (Marc Girod)
  4. Subject: Re: Static global initialization outside of main() legal?
  5. In-Reply-To: nagle@netcom.com's message of Thu, 31 Dec 1992 19:11:23 GMT
  6. Message-ID: <GIROD.93Jan4122311@kilohp.kiloapo.ts.tele.nokia.fi>
  7. Sender: usenet@noknic.nokia.fi (USENET at noknic)
  8. Nntp-Posting-Host: kilohp.ts.tele.nokia.fi
  9. Reply-To: marc.girod@ntc.nokia.com
  10. Organization: /user/girod/.organization
  11. References: <34727@sales.GBA.NYU.EDU> <1992Dec31.191123.7222@netcom.com>
  12. Date: Mon, 4 Jan 1993 10:23:11 GMT
  13. Lines: 49
  14.  
  15. wlee@sales.GBA.NYU.EDU (Wai-Shing Lee) writes:
  16. wlee> I want to initialize a static global at run-time but I don't want to
  17. wlee> have to have a programmer using my modules to have to call some init() funciton
  18. wlee> at the begining of thier main().
  19.  
  20. This is a problem I have met already. I find it most recommendable to
  21. find a way to insure that needed static data is transparently
  22. initialised before use, without any explicit call of the user. This
  23. should definitely be well (better!) supported by the language.
  24.  
  25. >>>>> On Thu, 31 Dec 1992 19:11:23 GMT, nagle@netcom.com (John Nagle) said:
  26. John>        You're working in a very messy area of C++.  The order in which
  27. John> things happen during static initialization isn't the same in different
  28. John> compilers.  The language rules arein section r3.4 of "The C++ Programming
  29. John> Language, Second Edition", and you should read them if you want to do this
  30. John> sort of thing.
  31.  
  32. Things are not so bad as John states! The most important is
  33. guaranteed: that "the initialisation [...] in a translation unit is
  34. done before the first use of any function or object defined in that
  35. translation unit". There remains the "pathologic" case of mutual
  36. dependencies, dealt with in the annotation part of the ARM for 3.4,
  37. ... and the treatment of "virtual constructors" through the
  38. declaration of meta-class objects, the precise class of which should
  39. be kept unknown to the rest of the world...
  40.  
  41. John>        In most implementations, static initializers are run before
  42. John> "main" is called. 
  43.  
  44. But not in all, as unfortunately allowed by the language (e.g. g++).
  45.  
  46. John>                    For initializers that don't call functions, this
  47. John> is enough to get the static variables initialized properly.  But
  48. John> initializers that call functions can create serious problems, especially
  49. John> if the functions have side effects, are in a different compilation unit
  50. John> (file), or depend on other static variables being initialized.  The order
  51. John> in which things get initialized varies from compiler to compiler and
  52. John> sometimes from compile to compile, so you can't depend on this at all.
  53.  
  54. Well, there is no surprise: you should not rely on such order, but if
  55. you may "force" it explicitly, e.g. with the technique of "nifty
  56. counters" described in the ARM (among others).
  57. --
  58. +-----------------------------------------------------------------------------+
  59. | Marc Girod - Nokia Telecommunications       Phone: +358-0-511 7703          |
  60. | TL4E - P.O. Box 12                            Fax: +358-0-511 7432          |
  61. | SF-02611 Espoo 61 - Finland              Internet: marc.girod@ntc.nokia.com |
  62. |    X.400: C=FI, A=Elisa, P=Nokia Telecom, UNIT=TRS, SUR=Girod, GIV=Marc     |
  63. +-----------------------------------------------------------------------------+
  64.