This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/CC1310: 4 bytes padding added to struct (?)

Part Number: CC1310

Tool/software: TI-RTOS

Hi all,

I got an strange behaviour (as I understant byte alignment in structures and byte padding).

I got the structure you can see at image, and when I use uint64_t for address, the struct inserts 4 bytes, but cant find the sense of that alignment...cause variable is aligned to 4 byte addressing if compiler needs to add 4 bytes...

If I use 2 variables of 32bits byte padding doesn't exists, memory allocation is done as is hoped...

Any idea why is happening that?

Thanks so much in advance.

Rubén.

  • Hi RubénRM,

    According to GCC documentation:

    Note that the alignment of any given struct or union type is required by the ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct or union in question.

    So for PacketHeader_t, if you use uint64_t for address, the struct will be 8 byte aligned (thus you will have 4 bytes of padding after uiLen).

    If you use two uint32_t instead, PacketHeader_t will be 4 byte aligned (thus no padding needed after uiLen.)

    Regards,

    Toby

    PS: you may also refer to this post: https://stackoverflow.com/questions/45463701/why-is-the-compiler-adding-padding-to-a-struct-thats-already-4-byte-aligned

  • Hi Toby Pan,

    Thanks for your reply, the stackoverflow post is also helpfull. I didn't know that if you use 8byte elements, then 8 bytes alignment is performed instead 4 bytes.

    Best regards :)
    Rubén.