(part 34) Han from China answers your C questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Borked Pseudo Mailed

    (part 34) Han from China answers your C questions

    What is this?

    Eric said:
    A more detailed block of code is:
    >
    bool result;
    bool GetByte( char *theAddr, int theCount, char *theDest);
    >
    char Dest[ SOME_SIZE ];
    >
    result =
    GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
    1, ( char * ) ( &Dest ) );
    >
    My question is... does the struct aStruct live at address 0, or does
    address 0 contain a pointer to wherever aStruct actually lives? Seems
    to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
    indicate the latter...
    ...
    it's an embedded system, uses an ARM7-based controller.
    That ( char * ) ( &Dest ) is like two cups of redundancy. I'd hate to
    think that this embedded system is a pacemaker or something.

    As for "does address 0 contain a pointer to wherever aStruct actually
    lives?", no and yes. No, the code appears to be referring to a structure
    that begins at address 0, or at least is temporarily treating the data
    there as a structure. Yes, it's possible address 0 is in some sense a
    pointer (though not in the C sense), since the ARM MMU supports virtual
    memory, and the actual address that winds up on the address bus needn't
    refer to a real address of 0.

    I believe your confusion is a result of binding the "&" before the "->".
    &( ( struct aStruct * ) 0 )->Addr is broken down as follows:

    1. 0
    2. (struct aStruct *) 0
    --3. ( ( struct aStruct * ) 0 )->Addr
    4. &( ( struct aStruct * ) 0 )->Addr

    I believe you're making the mistake of breaking it down as follows:

    1. 0
    2. (struct aStruct *) 0
    --3. &( ( struct aStruct * ) 0 )
    4. &( ( struct aStruct * ) 0 )->Addr

    Yours,
    Han from China

  • Nick Keighley

    #2
    Re: (part 34) Han from China answers your C questions

    On 20 Nov, 22:36, Borked Pseudo Mailed <nob...@pseudo. borked.net>
    wrote:
    What is this?
    Eric said:
    A more detailed block of code is:
    >
    bool result;
    bool GetByte( char *theAddr, int theCount, char *theDest);
    >
    char Dest[ SOME_SIZE ];
    >
    result =
      GetByte( ( char * ) &( ( struct aStruct * ) 0 )->Addr,
                                      1,  ( char * ) ( &Dest ) );
    >
    My question is... does the struct aStruct live at address 0, or does
    address 0 contain a pointer to wherever aStruct actually lives?  Seems
    to me like the "&" ahead of ( ( struct aStruct * ) 0 )->Addr would
    indicate the latter...
    ..
    it's an embedded system, uses an ARM7-based controller.
    >
    That ( char * ) ( &Dest ) is like two cups of redundancy. I'd hate to
    think that this embedded system is a pacemaker or something.
    that would all be sorted out at compile time on any reasonable
    compiler. Both address-of and casting between pointers are free at run-
    time.

    As for "does address 0 contain a pointer to wherever aStruct actually
    lives?", no and yes. No, the code appears to be referring to a structure
    that begins at address 0, or at least is temporarily treating the data
    there as a structure. Yes, [its] possible address 0 is in some sense a
    pointer (though not in the C sense),
    nonsense

    since the ARM MMU supports virtual
    memory, and the actual address that winds up on the address bus needn't
    refer to a real address of 0.
    by "real" I assume you mean "physical". In what sense is the address/
    pointer
    referred to in the C program not-real? You seem to be adding
    unnecessary
    confusion (surprise!)
    I believe your confusion is a result of binding the "&" before the "->".
    &( ( struct aStruct * ) 0 )->Addr is broken down as follows:
    >
        1. 0    
        2. (struct aStruct *) 0  
    --3. ( ( struct aStruct * ) 0 )->Addr
        4. &( ( struct aStruct * ) 0 )->Addr
    this is correct
    I believe you're making the mistake of breaking it down as follows:
    >
        1. 0
        2. (struct aStruct *) 0
    --3. &( ( struct aStruct * ) 0 )  
        4. &( ( struct aStruct * ) 0 )->Addr

    --
    Nick Keighley

    Comment

    Working...