[comp.lang.c++] Is this a bug in Apollo C++ Compiler System?

ashley@usage.csd.oz (Ashley Aitken) (06/25/91)

C++ Compiler System Bug Report?
===============================

Overview:

Not all constructors and destructors for global (and static) 
objects (in separated source files) are called before the 
start of the main program. Those called depend on the
order of linking of the object files.

Cause?

I believe that the error is caused by failure of the start-
up initialization code in the _entry routine, or more likely
failure of the Constructor Linker.     

Technical:
On observation of the C code generated it seems that the 
correct STI and STD functions and LINK structures are 
being created for each file but not being linked 
together or called correctly. 

c++patch / mxx ?

Compiler:
Error seems to be common to both Version 1.2.1 and Version
2.0 of the C++ Compiler (Glockenspiel) on HP-Apollo Domain
machines. 

ld : C Compilation System, Issue 4.0 (11.0) 1/3/86 : Version 1.8.2.1: 11/19/85


Example
	Source Files (appended to document):
		file1.cxx
		file2.cxx

	Compilation:
		CC -c file1.cxx
		CC -c file2.cxx

	CC -o testa file1.o file2.o  
 
        Output From testa:

		Construtor C
		Construtor D
		Start Main
		A::m1
		Construtor B
		D::m1
		End   Main
		Destructor B
		Destructor D
		Destructor C


Constructor (and Destructor) for a of class A is not called 		
at all!
            
	CC -o testa file2.o file1.o  
           
	Output from testb:

		Construtor A
		Start Main
		A::m1
		Construtor B
		D::m1
		End   Main
		Destructor B
		Destructor A     

Constructor (and Destructor) for c of class C and d of class
D are not called at all!  

file1.cxx:

#include	<stream.h>
                       

class A {
public:
	 A();      
	 void m1();
	~A();
};  

A::A()  { cout << "Construtor A\n"; } 
void A::m1() { cout << "A::m1\n"; }
A::~A() { cout << "Destructor A\n"; }
 
A a;


class B {
public:
	 B();   
	~B();
};  

B::B()  { cout << "Construtor B\n"; }
B::~B() { cout << "Destructor B\n"; }
   
 
class D {
public:
	 D();
	 void m1();   
	~D();
};  
   
extern D d;

main () {

	cout << "Start Main\n"; 
	a.m1();
	B b; 
	d.m1();
	cout << "End   Main\n";

}

*EOF*

file2.cxx:

#include	<stream.h>

class C {
public:
	 C();   
	~C();
};  

C::C()  { cout << "Construtor C\n"; }
C::~C() { cout << "Destructor C\n"; }
 
static C c;


class D {
public:
	 D(); 
	 void m1();  
	~D();
};  

D::D()  { cout << "Construtor D\n"; }
void D::m1() { cout << "D::m1\n"; }
D::~D() { cout << "Destructor D\n"; }
   
D d;

*EOF*
      
Originator:                 

Ashley Aitken    

School of Electrical Engineering and Computer Science
University of New South Wales 

ashley@spectrum.cs.unsw.oz.au
Ph (02) 663-8117 (Answering)
24th June, 1991.

dsouza@gwen.cad.mcc.com (Desmond Dsouza) (06/25/91)

In article <1801@usage.csd.unsw.oz.au> ashley@usage.csd.oz (Ashley Aitken) writes:

   Not all constructors and destructors for global (and static) 
   objects (in separated source files) are called before the 
   start of the main program. Those called depend on the
   order of linking of the object files.

Global objects do NOT have to be constructed before main() is 
entered. The language simply requires that such globals be 
constructed
	 "... before the first use of any function or object defined
	in THAT translation unit". 

An implementation is free to defer initialization until that time (see
ARM, Sec. 3.4).

Can someone clarify the ARM definition for me, please. Is it the case
that I can freely refer to global objects defined in any other files
and be assured that they are initialized before they are first
accessed? 

--
Desmond.
--

-------------------------------------------------------------------------------
 Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324
 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza