public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* two constructor copies in object file
@ 2002-01-16 18:53 Rogelio M. Serrano Jr.
  2002-01-17  8:19 ` Alexandre Oliva
  0 siblings, 1 reply; 5+ messages in thread
From: Rogelio M. Serrano Jr. @ 2002-01-16 18:53 UTC (permalink / raw)
  To: gcc


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?
and why is the static member undefined?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: two constructor copies in object file
  2002-01-16 18:53 two constructor copies in object file Rogelio M. Serrano Jr.
@ 2002-01-17  8:19 ` Alexandre Oliva
  2002-01-17 12:24   ` Joe Buck
  0 siblings, 1 reply; 5+ messages in thread
From: Alexandre Oliva @ 2002-01-17  8:19 UTC (permalink / raw)
  To: Rogelio M. Serrano Jr.; +Cc: gcc

On Jan 16, 2002, "Rogelio M. Serrano Jr." <rogelio@evoserve.com> wrote:

> Why are the constructors created twice?

One to handle full-object construction, one to handle sub-object
construction, IIRC.

> and why is the static member undefined?

Because you didn't define it.  Every static data member must be
defined outside the class body, in addition to being declared inside
it.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: two constructor copies in object file
  2002-01-17  8:19 ` Alexandre Oliva
@ 2002-01-17 12:24   ` Joe Buck
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Buck @ 2002-01-17 12:24 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: Rogelio M. Serrano Jr., gcc

On Jan 16, 2002, "Rogelio M. Serrano Jr." <rogelio@evoserve.com> wrote:
> > Why are the constructors created twice?

Alexandre Oliva writes:
> One to handle full-object construction, one to handle sub-object
> construction, IIRC.

Yes, but the fact that the demangler maps two distinct strings into
one identical string is, IMHO, a bug.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: two constructor copies in object file
@ 2002-01-17 11:43 Bernard Dautrevaux
  0 siblings, 0 replies; 5+ messages in thread
From: Bernard Dautrevaux @ 2002-01-17 11:43 UTC (permalink / raw)
  To: 'Alexandre Oliva', Rogelio M. Serrano Jr.; +Cc: gcc

> -----Original Message-----
> From: Alexandre Oliva [mailto:oliva@lsd.ic.unicamp.br]
> Sent: Thursday, January 17, 2002 4:26 PM
> To: Rogelio M. Serrano Jr.
> Cc: gcc@gcc.gnu.org
> Subject: Re: two constructor copies in object file
> 
> 
> On Jan 16, 2002, "Rogelio M. Serrano Jr." 
> <rogelio@evoserve.com> wrote:
> 
> > Why are the constructors created twice?
> 
> One to handle full-object construction, one to handle sub-object
> construction, IIRC.

Does that mean that nm --demangle has "hidden" a difference in the two
names? It displayed exactly identical symbols (see the OP), but the mangled
ones *may* be different in some way that is *not* displayed by the
demangler.

If that's true, it would be nice if the demangler functions could display a
hint that one is a special constructor.

OTOH if you're true, that mean that we may have surprises when debugging: if
I set a breakpoint on the default constructor for some class, GDB should in
fact set one breakpoint for both constructors or at least *propose* to
choos. However allowing to set the breakpoint on either
"boot_obj::boot_obj()" or "boot_obj::boot_obj()" or "both" will probably
generate quite a lot of traffic on some ML :-)

	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
-------------------------------------------- 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: two constructor copies in object file
@ 2002-01-17  7:17 Bernard Dautrevaux
  0 siblings, 0 replies; 5+ messages in thread
From: Bernard Dautrevaux @ 2002-01-17  7:17 UTC (permalink / raw)
  To: 'Rogelio M. Serrano Jr.', gcc

> -----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
-------------------------------------------- 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-01-17 19:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-16 18:53 two constructor copies in object file Rogelio M. Serrano Jr.
2002-01-17  8:19 ` Alexandre Oliva
2002-01-17 12:24   ` Joe Buck
2002-01-17  7:17 Bernard Dautrevaux
2002-01-17 11:43 Bernard Dautrevaux

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).