two constructor copies in object file

Bernard Dautrevaux Dautrevaux@microprocess.com
Thu Jan 17 07:17:00 GMT 2002


> -----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<char*>((reinterpret_cast<unsigned int>(_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<char*>(
		(reinterpret_cast<unsigned int>(_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
-------------------------------------------- 



More information about the Gcc mailing list