home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11877 < prev    next >
Encoding:
Text File  |  1992-07-31  |  3.8 KB  |  141 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!sdl!adk
  3. From: adk@Warren.MENTORG.COM (Ajay Kamdar)
  4. Subject: initialization of static data
  5. Message-ID: <1992Jul31.193746.9714@Warren.MENTORG.COM>
  6. Summary: are my compilers broken as I believe they are?
  7. Organization: Mentor Graphics Corp. - IC Group
  8. Date: Fri, 31 Jul 1992 19:37:46 GMT
  9. Lines: 130
  10.  
  11.  
  12. I have run into a problem with initialization of static data in multiple
  13. translation units. This is despite my attempts to only do what is legal
  14. as specified by ARM.
  15.  
  16. On pg. 19, $3.4, ARM states:
  17.     The initialization of nonlocal static objects in a translation
  18.     unit is done before the first use of any function or object defined
  19.     in that translation unit. Such initializations may be done before
  20.     the first statement of main() or deferred to any point in time before
  21.     the first use of a function or object defined in that translation unit.
  22.  
  23. I believe that my compilers (Sun C++ 2.1, AT&T C++ 3.0)  don't seem to
  24. follow this rule. However, given the tricky nature of this whole issue,
  25. I would very much appreciate input from the net as to whether my
  26. interpretation of ARM on how this stuff should work is incorrect, or
  27. whether cfront is broken. If you agree that the compiler is indeed
  28. broken, suggestions on how to workaround the problem would be very
  29. welcome. However, while making suggestions, please keep in mind that the
  30. real life code on which this example is based is much more complex than
  31. what I have shown here.
  32.  
  33. I have distilled the problem to a very simple test example consisting
  34. of 3 files: t.c, a.c, and b.c. Both a.c and b.c contain static objects.
  35. The initialization of static objects in b.c starts first. However,
  36. from within the CTOR for the static object in b.c, I call a function
  37. defined in the file a.c. My interpretation of the ARM suggests to me
  38. that this should force the initilization of the static objects in a.c
  39. before the function is executed. However, as the example below demonstrates,
  40. things do not quite proceed as I expect them to.
  41.  
  42. Compiling and executing my test programs results in the following output:
  43.  
  44. dre:/home/adk/tmp 431% CC t.c a.c b.c
  45. CC  t.c:
  46. CC  a.c:
  47. CC  b.c:
  48. cc  -L/usr/lang/ATT_3.0/lib   t.c a.c b.c -lC
  49. t.c:
  50. a.c:
  51. b.c:
  52. dre:/home/adk/tmp 432% a.out
  53. a.d_val == 0
  54. setting a.d_val = 5
  55. a.d_val == 5
  56. B()
  57. a.d_val == 5
  58. setting a.d_val = 5
  59. a.d_val == 5
  60. B()
  61. A()
  62. a.d_val == 1
  63.  
  64. As the output shows, the static object in a.c is not initialized before a
  65. function defined in a.c is executed. This result was obtained with the
  66. following source code:
  67.  
  68. /*
  69. File: t.c
  70. */
  71.  
  72. extern void printStaticAVal();
  73. extern void fnInB();
  74.  
  75. main()
  76. {
  77.     fnInB();    // call function in b.c
  78.                 // should cause initialization of statics in B
  79.                 // if not already done before start of main()
  80.  
  81.     printStaticAVal(); // print the value of the static in a.c
  82. }
  83.  
  84. /*
  85. File: a.c
  86. */
  87.  
  88. #include <iostream.h>
  89.  
  90. class A {
  91. public:
  92.     int    d_val;
  93.     A(int i) : d_val(i) {cout << "A()" << endl;}
  94. };
  95.  
  96. static A    a(1);
  97.  
  98. void setStaticValInA()
  99. {
  100.     cout << "a.d_val == " << a.d_val << endl;
  101.     cout << "setting a.d_val = 5" << endl;
  102.     a.d_val = 5;
  103.     cout << "a.d_val == " << a.d_val << endl;
  104. }
  105.  
  106. void printStaticAVal() {cout << "a.d_val == " << a.d_val << endl;}
  107.  
  108. /*
  109. File: b.c
  110. */
  111.  
  112. #include <iostream.h>
  113.  
  114. extern void setStaticValInA();
  115.     // function defined in a.c.
  116.     // calling this function should cause the initialization of
  117.     // all non local static data in a.c before the first use of this
  118.     // function
  119.  
  120. class B {
  121. public:
  122.     B()
  123.     {
  124.         setStaticValInA();
  125.             // ensure that static data in A is initialized
  126.         cout << "B()" << endl;
  127.     }
  128. };
  129.  
  130. static B    b1;
  131. static B    b2;
  132.  
  133. void fnInB() {}
  134.     // function called from main() to initialize b.c's static data
  135.     // in case data not initialized before start of main()
  136. -- 
  137. I speak for none but myself.
  138.  
  139. Ajay Kamdar                               Email : ajay_kamdar@mentorg.com
  140. Mentor Graphics, IC Group (Warren, NJ)    Phone : (908) 580-0102
  141.