From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13622 invoked by alias); 17 Jan 2002 09:09:09 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 13590 invoked from network); 17 Jan 2002 09:09:06 -0000 Received: from unknown (HELO iis000.microdata.fr) (194.206.157.151) by sources.redhat.com with SMTP; 17 Jan 2002 09:09:06 -0000 Received: by IIS000 with Internet Mail Service (5.5.2653.19) id ; Thu, 17 Jan 2002 10:00:00 +0100 Message-ID: <17B78BDF120BD411B70100500422FC6309E431@IIS000> From: Bernard Dautrevaux To: "'Rogelio M. Serrano Jr.'" , gcc@gcc.gnu.org Subject: RE: two constructor copies in object file Date: Thu, 17 Jan 2002 07:17:00 -0000 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="ISO-8859-1" X-SW-Source: 2002-01/txt/msg01215.txt.bz2 > -----Original Message----- > From: Rogelio M. Serrano Jr. [mailto:rogelio@evoserve.com] > Sent: Thursday, January 17, 2002 1:29 AM > To: gcc@gcc.gnu.org > Subject: two constructor copies in object file > > > > I compiled the following code with: g++-3.1 -nostdinc -nostdinc++ -c > g++ is snapshot 20020114 with threads disabled. > > extern "C" char* _end; > > class boot_obj > { > protected: > static char* top_of_heap; > > boot_obj() throw(); > boot_obj(boot_obj& x) throw(); > void* boot_alloc(unsigned int) throw(); > }; > > > boot_obj::boot_obj() throw () > { > static bool started = false; > if (!started) > { > top_of_heap = > reinterpret_cast((reinterpret_cast(_end) > + 0x01) & ~0x01); > started = false; > } > } > > boot_obj::boot_obj(boot_obj& x) throw () > { > > } > > void* boot_obj::boot_alloc(unsigned int sz) throw () > { > void* ret = top_of_heap; > top_of_heap += ((sz + 0x01) & ~0x01); > return ret; > } > > nm --demangle yeilds the following: > > > 0000005c T boot_obj::boot_alloc(unsigned) > U boot_obj::top_of_heap > 00000054 T boot_obj::boot_obj(boot_obj&) > 00000026 T boot_obj::boot_obj() > 0000004c T boot_obj::boot_obj(boot_obj&) > 00000000 T boot_obj::boot_obj() > 00000000 d boot_obj::boot_obj()::started > U _end > > Why are the constructors created twice? For this I can't help... Note however that the copy constructor is not really needed; you don't have anything to copy so the default copy constructor will just be appropriate (and will be expanded inline to do effectively nothing, which is a lot more effective). > and why is the static member undefined? But for this it's simply C++ static member semantics. Jou just DECLARE it in the class declaration. You need somewhere (in the same or another file) to INITIALIZE it by: char* boot_obj::top_of_heap; or: char* boot_obj::top_of_heap = reinterpret_cast( (reinterpret_cast(_end) + 0x01) & ~0x01 ); The second incarnation will avoid th eneed for the "started" static and the need to test it each time you construct a boot_obj. In this case as the default constructor doesn't do anything either, you can let the compiler generate it inline when used (to do nothing). You should then get rid of all constructors. But of course, this does not explain why the compiler seems needed to generate twice your constructors :-) HTH Bernard -------------------------------------------- Bernard Dautrevaux Microprocess Ingenierie 97 bis, rue de Colombes 92400 COURBEVOIE FRANCE Tel: +33 (0) 1 47 68 80 80 Fax: +33 (0) 1 47 88 97 85 e-mail: dautrevaux@microprocess.com b.dautrevaux@usa.net --------------------------------------------