sizeof(struct)返回意外的值 [英] sizeof(struct) returns unexpected value

查看:126
本文介绍了sizeof(struct)返回意外的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但我不知道在哪里寻找问题:

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

我有一个结构:

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

当我执行 sizeof(region)

任何想法?

(mingw gcc,win x64 os)

(mingw gcc, win x64 os)

推荐答案

填充结构以适应8字节边界。因此,它实际上是在内存中占用40个字节 - sizeof正在返回正确的值。

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.

如果你只想要33字节,然后指定打包的属性:

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));

这篇关于sizeof(struct)返回意外的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆