From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23328 invoked by alias); 30 Sep 2013 14:29:32 -0000 Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org Received: (qmail 23315 invoked by uid 89); 30 Sep 2013 14:29:31 -0000 Received: from mail-oa0-f49.google.com (HELO mail-oa0-f49.google.com) (209.85.219.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 30 Sep 2013 14:29:31 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=BAYES_00,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,NO_RELAYS autolearn=no version=3.3.2 X-HELO: mail-oa0-f49.google.com Received: by mail-oa0-f49.google.com with SMTP id i4so3791297oah.36 for ; Mon, 30 Sep 2013 07:29:29 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.182.71.82 with SMTP id s18mr20066797obu.9.1380551369415; Mon, 30 Sep 2013 07:29:29 -0700 (PDT) Received: by 10.60.45.207 with HTTP; Mon, 30 Sep 2013 07:29:29 -0700 (PDT) In-Reply-To: <20130930081626.GA15265@host2.jankratochvil.net> References: <20130928183852.GA12891@host2.jankratochvil.net> <20130930081626.GA15265@host2.jankratochvil.net> Date: Mon, 30 Sep 2013 14:29:00 -0000 Message-ID: Subject: Re: Could GDB get offset of a field in virtual base class through NULL pointer From: hex To: Jan Kratochvil Cc: gdb@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 X-SW-Source: 2013-09/txt/msg00034.txt.bz2 2013/9/30 Jan Kratochvil : > On Sun, 29 Sep 2013 03:59:54 +0200, hex wrote: >> > I do not see what it should do. In the following case &(((B *)&OBJECT)->a) >> > prints once 12 and once 16 for different OBJECT so what it should print for 0? >> > >> > class X:public virtual A,public B {}; >> > class C { >> > public: >> > int c; >> > }; >> > class Y:public virtual A,public C,public B {}; >> > #include >> > int main() { >> > X x; >> > Y y; >> > std::cout << (char *)&(((B *)&x)->a)-(char *)&x << std::endl; >> > std::cout << (char *)&(((B *)&y)->a)-(char *)&y << std::endl; >> > } >> > >> >> If we use &(((B *)0)->a), we are likely to get offset of 'a' in class >> B. If GDB could >> support this specific case, we do not need a real object to get the offset. > > This would apply if you had s/virtual A/A/. But with the inheritance of > A being virtual the memory location of A inside the whole object instance is > "random", it does not depend on B but it depends on X or Y. Specifically it > depends on virtual tables used for the specific instance, the virtual tables > specify the location of A. This is what I am trying to show you in the > example above. > > The same expression (char *)&(((B *)&OBJECT)->a) produces different result > depending on which OBJECT you pass there. Therefore which result should > produce passing 0 instead of &OBJECT there? It cannot be a single number. > > > Jan Kratochvil Thank you for the explanation. I hope &(((B *)0)->a) to be regarded as a special case that gets the same value as (B object; (char *)&((&object)->a) - (char *)&object). If G++ emits A's offset in B to the program's DAWRF file, GDB could support this case by saving the offset. But I checked, only found this offset in the binary code of B's constructor function.