sizeof(struct) 返回意外值

发布于 2024-10-31 14:34:40 字数 359 浏览 4 评论 0原文

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

我有一个结构:

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

当我执行 sizeof(region) 时,当我期待时,它会给我 40 33

有什么想法吗?

(mingw gcc,win x64 操作系统)

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 y;
    long long int width;
    long long int height;
    unsigned char scale;
};

When I do sizeof(region) it gives me 40 when I am expecting 33.

Any ideas?

(mingw gcc, win x64 os)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2024-11-07 14:34:40

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

如果您希望它只占用 33 个字节,请指定 packed 属性:

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

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));
眼角的笑意。 2024-11-07 14:34:40

long long int 值每个为 8 个字节。 scale 只有 1 个字节,但为了对齐而进行了填充,因此它实际上也占用了 8 个字节。 5*8 = 40。

long long int values are 8 bytes each. scale is only 1 byte but is padded for alignments, so it effectively takes up 8 bytes too. 5*8 = 40.

灰色世界里的红玫瑰 2024-11-07 14:34:40

正如其他人所说,结构体是为了对齐而进行填充的,这种填充不仅取决于成员的类型,还取决于它们所在的成员的顺序定义的。

例如,考虑如下定义的这两个结构体 AB。两个结构体在成员和类型方面是相同的;唯一的区别是定义成员的顺序不同:

struct A
{
    int i;
    int j;
    char c;
    char d;
};

struct B
{
    int i;
    char c;
    int j;
    char d;
};

sizeof(A) 是否等于 sizeof(B) 只是因为它们相同相同类型的成员数量?不。尝试打印每个的大小:

cout << "sizeof(A) = "<< sizeof(A) << endl;
cout << "sizeof(B) = "<< sizeof(B) << endl;

输出:

sizeof(A) = 12
sizeof(B) = 16

惊讶吗?自己查看输出:http://ideone.com/yCX4S

As others said, structs are padded for alignments, and such padding not only depends on the type of the members, but also on the order of the members in which they're defined.

For example, consider these two structs A and B as defined below. Both structs are identical in terms of members and types; the only difference is that the order in which members are defined isn't same:

struct A
{
    int i;
    int j;
    char c;
    char d;
};

struct B
{
    int i;
    char c;
    int j;
    char d;
};

Would the sizeof(A) be equal to sizeof(B) just because they've same number of members of same type? No. Try printing the size of each:

cout << "sizeof(A) = "<< sizeof(A) << endl;
cout << "sizeof(B) = "<< sizeof(B) << endl;

Output:

sizeof(A) = 12
sizeof(B) = 16

Surprised? See the output yourself : http://ideone.com/yCX4S

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文