🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #431

Re: Dispatching bug in g++?

#431
From: Joe Buck
Date: Fri, 16 Oct 1992 22:24
53 lines
1775 bytes
In article <1992Oct16.204850.3311@lucpum.it.luc.edu> laufer@math.luc.edu
submits the program

#include <stream.h>

class P {
public:
  virtual void hello() { cout << "hello\n"; }
  void callhello() { hello(); }
};

class C : public P {
public:
  virtual void hello() { cout << "hullo\n"; }
};

void g(P x) { x.callhello(); }	// should call P::hello()

int main() {
  C z;
  z.hello();			// calls C::hello()
  g(z);				// results in an erroneous call to C::hello()!
}

and writes:

>When compiled with g++ 2.2.2 under SunOS 4.1.2, this program produces
>the erroneous output
>	hullo
>	hullo
>
>This is incorrect, since the x is a value parameter, whence
>dispatching should be static.
>
>Compilation with AT&T CC correctly results in
>	hullo
>	hello

This looks like a fundamental bug in the default copy constructors
generated by g++ (determined by looking at the assembly language output).
They copy everything, including the pointer to the virtual function table.
This is wrong; the first word of the destination object should always be
the virtual function table for that object, regardless of what is copied
into it.

That is, when z is passed by value, its virtual function table pointer
is copied into x, the formal parameter for g(P), so that x is not
really a P at all, but rather a C.



--
Joe Buck	jbuck@ohm.berkeley.edu
Technical Details
Message-ID: <1bnfemINN27h@agate.berkeley.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!forney.berkeley.edu!jbuck
References: <1992Oct16.204850.3311@lucpum.it.luc.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!forney.berkeley.edu!jbuck
From: jbuck@forney.berkeley.edu (Joe Buck)
Newsgroups: gnu.g++.help
Subject: Re: Dispatching bug in g++?
Date: 16 Oct 1992 22:24:22 GMT
Organization: U. C. Berkeley
Lines: 53
Message-ID: <1bnfemINN27h@agate.berkeley.edu>
References: <1992Oct16.204850.3311@lucpum.it.luc.edu>
NNTP-Posting-Host: forney.berkeley.edu