public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Stack allocation skips vtable, optimalisation option?
@ 2005-07-28  9:21 Daan Oosterveld
  2005-07-28 11:49 ` Eljay Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Daan Oosterveld @ 2005-07-28  9:21 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1292 bytes --]


Hi all,

I have a problem finding the right switch to eliminate the compiler to 
jump over the vtable and calling the wrong virtual  function. When 
allocating a class on the stack and copying a subclass over it virtual 
functions don't behave as expected. As a pointer the code calls the 
virtual function of the subclass, but when called directly it always 
calls the function in the base class.

The compiler assumes the pointer to the vtable is to Foo, and then 
assumes to call Foo::foo() in stead of looking in the object and see the 
vtable is pointing at Bar and calling Bar::foo.

The output of this is:

$ ./a.out
Foo!
Bar!

It should be in my opinion:

$ ./a.out
Bar!
Bar!

The source is listed below. It is compiled without optimalisations (-O0)

Thanks

Daan Oosterveld


---- source:
#include <stdio.h>

class Foo
{
protected:
    char aap[12];
public:
    Foo() {}
    Foo(const Foo & foo) {
        char * d = (char*)this;
        char * s = (char*)&foo;
        
        for(int i = 0; i < sizeof(Foo); i ++) {
            d[i] = s[i];
        }
    }

    virtual void foo() { printf("Foo!\n"); }
};

class Bar : public Foo
{
public:
    virtual void foo() { printf("Bar!\n"); }
};

int main()
{
    Foo foo = Bar();
    Foo * bar = &foo;
    foo.foo();
    bar->foo();
}


[-- Attachment #2: d.oosterveld.vcf --]
[-- Type: text/x-vcard, Size: 153 bytes --]

begin:vcard
fn:Daan Oosterveld
n:Oosterveld;Daan
email;internet:d.oosterveld@wanadoo.nl
tel;home:053-4323699
tel;cell:06-24732100
version:2.1
end:vcard


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

* Re: Stack allocation skips vtable, optimalisation option?
  2005-07-28  9:21 Stack allocation skips vtable, optimalisation option? Daan Oosterveld
@ 2005-07-28 11:49 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2005-07-28 11:49 UTC (permalink / raw)
  To: Daan Oosterveld, gcc-help

Hi Daan,

Even with -O0, the compiler *KNOWS* that foo is a Foo (regardless of your
copy constructor shenanigans), and hence can call Foo::foo directly in main,
without using the vfntbl lookup.

HTH,
--Eljay


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

end of thread, other threads:[~2005-07-28 11:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-28  9:21 Stack allocation skips vtable, optimalisation option? Daan Oosterveld
2005-07-28 11:49 ` Eljay Love-Jensen

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