sizeof(struct) returns unexpected value

后端 未结 3 1477
轻奢々
轻奢々 2020-11-30 13:39

This should be simple but I have no clue where to look for the issue:

I have a struct:

struct region
{
public:
    long long int x;
    long long int         


        
3条回答
  •  一整个雨季
    2020-11-30 14:42

    It's padding the struct to fit an 8-byte boundary. So it actually is taking 40 bytes in memory - sizeof is returning the correct value.

    If you want it to only take 33 bytes then specify the packed attribute:

    struct region
    {
    public:
        long long int x;
        long long int y;
        long long int width;
        long long int height;
        unsigned char scale;
    } __attribute__ ((packed));
    

提交回复
热议问题