0

I've got a memory management problem. Since i'm working in a constant sized buffer of bytes, I need my data as compact as possible.

I need to check the size of a struct that has another struct inside of it. Here's the code:

struct CommonData
{
    //common data
    uint64_t a; // 8B
    uint64_t b; // 8B
    uint64_t c; // 8B

    uint32_t d; // 4B

    uint8_t e; // 1B

    uint8_t f; // 1B
};

struct AllData
{
    //specific data
    uint8_t arr[15]; //15B

    //common data
    CommonData commonData; //30B
}

Normally I would think that sizeOf(AllData) would be 30 + 15 = 45 B but the compiler says it is 48. Yes, I kind of know about memory paddings and yes I know that is the "issue" here. Is there a way I could rearrange my data in order to get rid of those padding 3 bytes because memory is of high importance here.

NOTE: __attribute__((packed)) does not do the trick

EDIT:

pragma pack(push, 1)

.... ....

pragma pack(pop)

on both structs did the trick but I'm not sure how safe and reliable this option is

Thank you in advance!

0 Answers0