AW: AW: Feature request for "friendship" of pointers in "C"

Xi Ruoyao xry111@mengyan1223.wang
Mon Feb 3 10:08:00 GMT 2020


On 2020-02-03 01:49 -0800, J Decker wrote:
> On Mon, Feb 3, 2020 at 1:41 AM Xi Ruoyao <xry111@mengyan1223.wang> wrote:
> 
> > On 2020-02-03 01:34 -0800, J Decker wrote:
> > > On Mon, Feb 3, 2020 at 1:29 AM aotto <aotto1968@t-online.de> wrote:
> > > 
> > > > Hi,
> > > > 
> > > > this is a test: https://godbolt.org/z/8PpX8W
> > > > 
> > > 
> > > why is method_base(&aO->obj, 1); so much worse than method_base(aO, 1);
> > >  it's the same resulting code...
> > > 
> > > 
> > >  method_base(aO, 1);
> > > mov rax, QWORD PTR [rbp-8]
> > > mov esi, 1
> > > mov rdi, rax
> > > call method_base
> > > 
> > > method_base(&aO->obj, 1);
> > > mov rax, QWORD PTR [rbp-8]
> > > mov esi, 1
> > > mov rdi, rax
> > > call method_base
> > > 
> > > and there's no forced cast....
> > 
> > If a0 is NULL, the standard specifies "(base *) a0" will also be NULL
> > (6.3.2.3
> > p4).  But, &a0->obj is undefined behavior.
> > 
> 
> While true; that's not necessarily any sort of issue... other than perhaps
> finding words to describe it.
> It doesn't require dereferencing the pointer to get the value that is
> 'obj'.  It's just (address that is a pointer of some type) + ( offsetof(
> type, member ) ) which does yield a non-NULL result

After reading the standard more carefully I found you are right...  It's not
undefined, if a0 is NULL but the value of a0->obj is not used.  Only actually
using the value is undefined.

Sorry for being so paranoid :(.
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list