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

J Decker d3ck0r@gmail.com
Mon Feb 3 09:34:00 GMT 2020


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....


>
>
> On 03.02.20 10:07, J Decker wrote:
>
>
>
> On Mon, Feb 3, 2020 at 12:23 AM aotto <aotto1968@t-online.de> wrote:
>
>> Hi,
>>
>> there is an easy test, if Stefan is right and I'm wrong.
>>
>> 1. cast an INVALID Pointer to "aObj"
>>
> how would friend prevent an invalid pointer pass?
>
>
>> 2. call "method_base(&aObj->obj,...)"
>>
>> -> if stefan is right than "method_base" is called with an INVALID aObj
>> as Pointer
>> -> if I am right than your software will CRASH :-)
>>
> given 1) sure, true in every case, doesn't make stephen wrong.
>
>>
>> that is the reason why I require the "frienddef".
>>
>>
>> mfg AO
>>
>>
>>
>> On 03.02.20 09:16, aotto wrote:
>> > Hi,
>> >
>> > Can someone confirm that *aObj->obj does NOT create any extray "code"
>> > and does NOT do any
>> > POINTER resolution? -> if so than "gcc" already uses the "friend"
>> > logic I require :-)
>>
>
>
> https://godbolt.org/  this is a good place to collaborate on shared
> code... it shows the generated assembly...
> https://godbolt.org/z/X5mRYg  is something resembling your case;
> especially the function call of g()... I think probably an optimized
> compile would generate the same for the assignments to tmp also...
>
> So you just want to shortcut that the any first struct in a containing
> pointer is valid as a parameter, and an implicit cast to the inner type
> should be done?  The internal browsing of what members are in a type is not
> very convenient, and would require quite a few lines to search...
>
> struct a {
>    struct b {
>        struct c {
>          int d;
>       }C;
>    }B;
> }A;
>
> void g( struct c* pC ) {}
> void h( struct b* pB) {}
>
> void f(void){
>     g( &A );  // instead of g( &A.B.C );
>     h( &A );  // instead of h( &A.B );
> }
>
>
>
>
> >
>> > → I don't think that "gcc" do this already
>>
>> Hi, the following scenario has a "definition hole" in the "C" language
>>
>> code example:
>>
>> -------------------------
>> struct base {
>>     ...
>> };
>>
>> struct A {
>>     struct base obj;
>>     ...
>> } aObj;
>>
>> struct B {
>>     struct base obj;
>>     ...
>> } bObj;
>>
>> void method_base (struct base * hdl, ...);
>>
>> method_base(&aObj, ...)
>> method_base(&bObj, ...)
>
> ------------------------
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: aotto <aotto1968@t-online.de>
>> Gesendet: Montag, 3. Februar 2020 09:02
>> An: Stefan Franke <stefan@franke.ms>
>> Betreff: Re: AW: Feature request for "friendship" of pointers in "C"
>>
>> This is not the same!
>>
>> 1. usually the aObj is a Pointer:
>>       -> &aObj.obj => &(aObj->obj)
>> 2. this DOUBLE indirect access creates definitely a CRASH if
>>       aObj is invalid or freed
>> 3. I use a internal a Pointer typecheck… where I can detect if
>>       "my" pointer is valid or freed… but a "&(aObj->obj)" you
>>       don't survive.
>>
>> mfg
>>
>> * Please reply at the bottom.
>>
>> Ad 1: there is no need for '(' ')' if it's a pointer
>>
>>      &aObj->obj
>>
>>
>> Ad 2: this is not a double indirect access, the resulting address is the
>> same as aObj as long obj is the first element of your struct, only the
>> type changes
>>
>>     (void *)aObj == (void*)&aObj->obj
>>
>> Ad 3: since the address is the same, your check will work.
>>
>> Your issues aren't related to gcc - you might consider improving your C
>> skills.
>>
>> Cheers
>>
>> Stefan
>>
>>
>



More information about the Gcc-help mailing list