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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!meaddata!brad
  3. From: brad@meaddata.com (Brad Craig)
  4. Subject: Question about global object deallocation.
  5. Sender: news@meaddata.com (Usenet Administrator)
  6. Organization: Mead Data Central, Dayton OH
  7. Date: Thu, 23 Jul 1992 17:38:21 GMT
  8. Message-ID: <1992Jul23.173821.22863@meaddata.com>
  9. Lines: 126
  10.  
  11. Here is an example program that I wrote to test some strangeness
  12. I was encountering with a String class and some global const
  13. String objects that were defined. I simplified the problem by
  14. creating the following header file and source files ...
  15.  
  16. --------------------------- Int.h --------------------------
  17. #ifndef INTEGER_H
  18. #define INTEGER_H
  19.  
  20. class Integer;
  21.  
  22. extern const Integer Zero;
  23. extern const Integer One;
  24. extern const Integer Two;
  25. extern       Integer Three;
  26. extern       Integer Four;
  27.  
  28. class Integer {
  29.   public:
  30.     Integer(const int i = 0);
  31.     ~Integer(void);
  32.   private:
  33.     int _i;
  34. };
  35.  
  36. #endif
  37. --------------------------- Int.h --------------------------
  38.  
  39. --------------------------- Int.C --------------------------
  40. #include <iostream.h>
  41.  
  42. #include "Int.h"
  43.  
  44. const Integer Zero;
  45. const Integer One(1);
  46. const Integer Two = 2;
  47.       Integer Three(3);
  48.       Integer Four = 4;
  49.  
  50. Integer::Integer(const int i) : _i(i)
  51. {
  52.     cout << "Integer::Integer(const int " << _i << ")" << endl;
  53. }
  54.  
  55. Integer::~Integer()
  56. {
  57.     cout << "Integer::~Integer() i = " << _i << endl;
  58. }
  59. --------------------------- Int.C --------------------------
  60.  
  61. --------------------------- main.C --------------------------
  62. #include "Int.h"
  63.  
  64. main()
  65. {;}
  66. --------------------------- main.C --------------------------
  67.  
  68. When these files are compiled, linked an executed
  69. the following output appears ...
  70.  
  71. Integer::Integer(const int 0)
  72. Integer::Integer(const int 1)
  73. Integer::Integer(const int 2)
  74. Integer::Integer(const int 3)
  75. Integer::Integer(const int 4)
  76. Integer::~Integer() i = 4
  77. Integer::~Integer() i = 3
  78. Integer::~Integer() i = 0
  79.  
  80. So my question is why doesn't _One_ and _Two_ get deallocated? 
  81. I looked at the translated 'C' code and here is the code that
  82. initializes and deallocates the globals ...
  83.  
  84. --------------------------- Int..c --------------------------
  85.  
  86. [ irrelevant stuff deleted ]
  87.  
  88. char __sti__Int_C___dt_ ()
  89. { __ct__13Iostream_initFv ( & iostream_init ) ;
  90.  
  91. __ct__7IntegerFCi ( (struct Integer *)(& Zero ), (int )0 ) ;
  92. __ct__7IntegerFCi ( (struct Integer *)(& One ), (int )1 ) ;
  93. __ct__7IntegerFCi ( (struct Integer *)(& Two ), (int )2 ) ;
  94. __ct__7IntegerFCi ( & Three , (int )3 ) ;
  95. __ct__7IntegerFCi ( & Four , (int )4 ) ;
  96.  
  97. }
  98.  
  99. char __std__Int_C___dt_ ()
  100. { __dt__7IntegerFv ( & Four , 2) ;
  101.  
  102. __dt__7IntegerFv ( & Three , 2) ;
  103.  
  104. __dt__7IntegerFv ( (struct Integer *)(& Zero ), 2) ;
  105.  
  106. __dt__13Iostream_initFv ( & iostream_init , 2) ;
  107.  
  108. }
  109. static struct __linkl { struct __linkl * next;
  110. char (*ctor)(); char (*dtor)(); } __link = 
  111. { (struct __linkl *)0, __sti__Int_C___dt_ ,__std__Int_C___dt_ };
  112.  
  113.  
  114. [ irrelevant stuff deleted ]
  115.  
  116. --------------------------- Int..c --------------------------
  117.  
  118. The __dt__7IntegerFv ( (struct Integer*)(& One ), 2), and
  119. __dt__7IntegerFv ( (struct Integer*)(& Two ), 2) dont get inserted. 
  120. Is there a reason that I am not seeing as to why cfront knows
  121. to allocate _One_ and _Two_ but not to deallocate them?
  122.  
  123. Additionally, If I remove the external declarations from _Int.h_
  124. then _One_ and _Two_ are deallocated.
  125.  
  126. This was tested with cfront 3.0.1 and cfront 2.1.0 and the same
  127. results occurr. The above _Int..c_ is from cfront 3.0.1.
  128.  
  129. Thanks in advance for any help.
  130.  
  131.  
  132. -- 
  133. Brad Craig        | "This generally results in   |             (513) 865-1536
  134. Mead Data Central |  heap insanity, and ultimate |Common Object Library Group
  135. P.O. Box 933      |  abnormal program death"     |          brad@meaddata.com
  136. Dayton, OH 45401  |    -- James O. Coplien       |    ...!uunet!meaddata!brad
  137.