This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: Could GDB get offset of a field in virtual base class through NULL pointer
- From: hex <heixia108 at gmail dot com>
- To: Jan Kratochvil <jan dot kratochvil at redhat dot com>
- Cc: gdb at sourceware dot org
- Date: Sun, 29 Sep 2013 09:59:54 +0800
- Subject: Re: Could GDB get offset of a field in virtual base class through NULL pointer
- Authentication-results: sourceware.org; auth=none
- References: <CAB_AMN7niBPRf4Mc6Tq=NXnbQacc7DKmM4btodcmKJ4TjOf8Bw at mail dot gmail dot com> <20130928183852 dot GA12891 at host2 dot jankratochvil dot net>
>> GDB could print &(((B *)0)->a), but it could not print &(((B *)0)->a).
> ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
> Those two expressions are the same and they really od not work:
>
> (gdb) p &(((B *)0)->a)
> Cannot access memory at address 0x0
>
Sorry, a mistake, I meant "GDB could print &(((B *)0)->b), but it could not
print &(((B *)0)->a)."
> 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 <iostream>
> 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.