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

Xi Ruoyao xry111@mengyan1223.wang
Mon Feb 3 09:41:00 GMT 2020


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.

Considering:

method_base(&a0->obj);

if (a0 != NULL)
  return -EINVAL;

return a0->foo + 233;

Then an optimizing C compiler could do some analysis:

(1) If a0 is NULL, &a0->obj invokes undefined behavior.  There is no requirement
from the standard so we don't need to consider this situation.

(2) If a0 is not NULL, the if statement is useless and can be optimized away.

So the code may be optimized to:

method_base(&a0->obj);
return a0->foo + 233;   /* crash when a0 is NULL */
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list