[Bug c++/13005] New: Pointer wrongly adjusted for derived class containing virtual function

stephenma at telus dot net gcc-bugzilla@gcc.gnu.org
Tue Nov 11 07:51:00 GMT 2003


If a base class B has no virtual functions, not even inherited ones,
but a class derived from B has at least one virtual function, then
sometimes a pointer to the derived class is wrongly adjusted.

Here is the slightly reformatted output from "g++ -v":

	Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.2/specs

	Configured with: ../src/configure -v
	--enable-languages=c,c++,java,f77,pascal,objc,ada,treelang
	--prefix=/usr --mandir=/usr/share/man
	--infodir=/usr/share/info
	--with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared
	--with-system-zlib --enable-nls --without-included-gettext
	--enable-__cxa_atexit --enable-clocale=gnu --enable-debug
	--enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc
	i486-linux

	Thread model: posix
	gcc version 3.3.2 (Debian)

The following little snippet demonstrates the problem:

	class Derived;
	
	struct Base {
	  Derived* before;
	  Derived* after;
	  void insertAfter(Derived* node);
	};
	
	struct Derived: public Base {
	  virtual void foo();
	};
	
	void Base::insertAfter(Derived* node) {
	  this->before->after = node;	// Notice the double indirection.
	}

When compiled with "g++-3.3 -S z.cpp" the compiler produces the
following assembler code for Base::insertAfter():

	pushl	%ebp
	movl	%esp, %ebp
	movl	8(%ebp), %eax
	movl	(%eax), %edx
	addl	$4, %edx         <-- BOGUS INSTRUCTION
	movl	12(%ebp), %eax
	movl	%eax, 4(%edx)
	popl	%ebp
	ret

I have marked what I believe is the offending instruction.

When I delete the declaration for Derived::foo(), the "addl"
instruction magically disappears.

In this specific example, the "addl" also disappears when I compile
with "-O2", regardless of whether Derived::foo() is declared.

-- 
           Summary: Pointer wrongly adjusted for derived class containing
                    virtual function
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: stephenma at telus dot net
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: i386-pc-linux-gnu
GCC target triplet: i386-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13005



More information about the Gcc-bugs mailing list