delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin-developers/2002/09/30/02:47:30

Mailing-List: contact cygwin-developers-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-developers-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin-developers/>
List-Post: <mailto:cygwin-developers AT cygwin DOT com>
List-Help: <mailto:cygwin-developers-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-developers-owner AT cygwin DOT com
Delivered-To: mailing list cygwin-developers AT cygwin DOT com
Date: Mon, 30 Sep 2002 10:43:01 +0400
From: egor duda <deo AT logos-m DOT ru>
Reply-To: egor duda <cygwin-developers AT cygwin DOT com>
Organization: deo
X-Priority: 3 (Normal)
Message-ID: <52772748984.20020930104301@logos-m.ru>
To: Robert Collins <rbcollins AT cygwin DOT com>
CC: cygwin-developers AT cygwin DOT com
Subject: Re: Many pthread failures in the test suite, one setgroup failure
In-Reply-To: <1033254454.4375.48.camel@lifelesswks>
References: <20020925141653 DOT GA6134 AT redhat DOT com>
<1033139976 DOT 22908 DOT 333 DOT camel AT lifelesswks>
<163544913434 DOT 20020927192540 AT logos-m DOT ru> <1033140780 DOT 9593 DOT 0 DOT camel AT lifelesswks>
<44642850720 DOT 20020928223759 AT logos-m DOT ru> <1033254454 DOT 4375 DOT 48 DOT camel AT lifelesswks>
MIME-Version: 1.0

Hi!

Sunday, 29 September, 2002 Robert Collins rbcollins AT cygwin DOT com wrote:

>> Now, in verifyable_object_isvalid you're casing pointer to variable of
>> subclass to pointer to base class. Ain't it case of 'never do like
>> this'? I suppose to safely perform cast from subclass to base class
>> one should always use dynamic_cast().

RC> It's a case of this is always ways ok to do - upcasting is FINE.. (it
RC> happens every time you call a virtual function in fact).

Yes, it's fine as long as it's done via compiler. When virtual
function is called it's compiler duty to arrange things so that this
is pointing to the proper place in memory. The same for dynamic_cast,
compiler knows how to update pointer. But what's done in
verifyable_object_isvalid() is _not_ upcasting. It's essentially a
pointer arithmetic. And i bet it's not guaranteed to work -- it will
obviously fail in the case of multiple inheritance when class C is
derived from class A and class B, and clearly, you won't be able to do
the trick with pointer assignments to cast pointer to the instance of
class C to both class A and class B. You'll need to use compiler
knowledge about class C instance layout for either casting to class A
or class B. So, the safe way is to always use dynamic_cast (or virtual
functions) and let compiler to arrange everything.

RC>  It's never ok to case a base class to a subclass (known as downcasting)
RC> unless you use a static_cast or a dynamic_cast.
RC> http://www.cs.rpi.edu/~wiseb/xrds/ovp3-1.html

That document doesn't say that you can safely do pointer arithmetic to
cast from derived class to the base.

RC> What *may* be a regression in 3.2 is the apparent bug caused by the
RC> introduction of a VMT in a derived class. However, as we already have a
RC> destructor for verifyable_object, and in this instance it should be
RC> virtual, we get to sidestep the bullet - for now.

Which bug in gcc? I guess we should look into C++ standard first. But
i suspect it doesn't say anything about offsets for common members in
base and derived classes being always equal, so that programmer can do
pointer assignment to cast from base to derived and vice versa. As i
said, it wouldn't be true in at least one case -- multiple
inheritance. So, before qualifying this as a gcc bug, you should first
give an appropriate reference to the standard.

Egor.            mailto:deo AT logos-m DOT ru ICQ 5165414 FidoNet 2:5020/496.19

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019