Thread: offset a cast?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Question offset a cast?

    Hi

    I have a uint8_t array that I am casting to a struct:

    Code:
    void processe(uint8_t *buffer, int length) {
        AppConfigData_t* configData;
        configData = (AppConfigData_t *) buffer;
    } // processe
    However the first two bytes in the buffer are not required / not wanted – is there a way I can offset the cast to start working from the 3rd byte in on the buffer?

    Many thanks

    David

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure:
    Code:
    configData = (AppConfigData_t *) buffer + 2;
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Hi - thanks for the reply - sadly if i do that the data within the strut get mangled, do I need to specify its the pointer I want to increment or something?

    David

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > do I need to specify its the pointer I want to increment or something?
    That's what it does...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DavidDobson View Post
    Hi - thanks for the reply - sadly if i do that the data within the strut get mangled, do I need to specify its the pointer I want to increment or something?

    David
    Perhaps you want (buffer + 2) so that the 2 is added before the cast - I'm not sure of the order precedence for casts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > I'm not sure of the order precedence for casts.
    Same as +... but they have right-left associativity.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    cast has a preceedence, so I think it should be
    configData = (AppConfigData_t *) (buffer + 2);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by zacs7 View Post
    > I'm not sure of the order precedence for casts.
    Same as +... but they have right-left associativity.
    same as unary +-
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ideally it should be...
    Code:
    configData = (AppConfigData_t *) (buffer + sizeof(the only member he wants to skip (all of or part of the first)));

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    ah! that did it - thanks everyone

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    But don't rely on (aka, it's wrong) your AppConfigData_t pointer being meaningfully aligned for your architecture.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    But don't rely on (aka, it's wrong) your AppConfigData_t pointer being meaningfully aligned for your architecture.
    I do agree that this type of "changing the pointer" may well cause unaligned data structures. But it does depend (a lot) on the content of AppConfigData_t's actual alignment requirements. If it consists solely of char arrays, any alignment should work OK. If it has integers in it, it may need to be aligned to an even 4 bytes, so if buffer in itself is aligned to 4 bytes, then the new pointer will not be aligned correctly - which can lead to a crash due to access of unaligned data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 03:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM