结构的大小 - 完全混淆 [英] Size of a struct - totally confused

查看:58
本文介绍了结构的大小 - 完全混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这非常完全无足轻重,标准C ++足够

可以在这里发布,不过它可能最初看起来像是如何制作一个

电脑游戏?问题,虽然我可能错了。


我正在加载一个位图,因为我正在使用Linux我必须定义

BITMAPFILEHEADER我自己(也许更好的做法是给我的版本

a新名称或其他什么,我不知道)。 Windows使用WORD和DWORD来定义它,我使用了SDL提供的一些标准尺寸类型(一个

图形/声音库的东西)来定义它:


struct BITMAPFILEHEADER

{

Uint16 bfType;

Uint32 bfSize;

Uint16 bfReserved1;

Uint16 bfReserved2;

Uint32 bfOffBits;

};


由于某种原因,我完全无法理解,但是,调试器显示我的

位图加载器函数认为sizeof(BITMAPFILEHEADER)== 16.


当然应该是14?


然后我尝试重新定义它几次:


BITMAPFILEHEADER

{

Uint32 bfType; //从16改为32

Uint32 bfSize;

Uint16 bfReserved1;

Uint16 bfReserved2;

Uint32 bfOffBits ;

};


这给出sizeof(BITMAPFILEHEADER)== 16,现在是正确的。


BITMAPFILEHEADER

{

Uint32 bfType;

Uint32 bfSize;

Uint32 bfReserved1;

Uint32 bfReserved2;

Uint32 bfOffBits;

};


这给出了sizeof(BITMAPFILEHEADER)== 20,这也是正确。


我很可能只是误解了某个地方,但我不知道

是什么。


谢谢,


James

I''m pretty sure this is non-totally-trivial enough and standard-C++ enough
to post here, however much it may initially look like a "how do I make a
computer game?" question, though I may be wrong.

I''m loading a bitmap, and as I''m using Linux I''ve had to define
BITMAPFILEHEADER myself (maybe better practice would be to give my version
a new name or whatever, I dunno). Windows uses WORDs and DWORDs to
define it, I''ve used some standard-size types provided by SDL (a
graphics/sound library thing) to define it thus:

struct BITMAPFILEHEADER
{
Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

For some reason I utterly can''t fathom, however, a debugger shows that my
bitmap loader function believes that sizeof(BITMAPFILEHEADER) == 16.

Surely that should be 14?

I then tried redefining it a couple of times:

BITMAPFILEHEADER
{
Uint32 bfType; //changed from 16 to 32
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

This gives sizeof(BITMAPFILEHEADER) == 16, which is now correct.

BITMAPFILEHEADER
{
Uint32 bfType;
Uint32 bfSize;
Uint32 bfReserved1;
Uint32 bfReserved2;
Uint32 bfOffBits;
};

This gives sizeof(BITMAPFILEHEADER) == 20, which is also correct.

Most likely I just misunderstand something somewhere, but I''ve no idea
what.

Thanks,

James

推荐答案

James Gregory写道:
James Gregory wrote:
I 我非常肯定这不是完全无足轻重的,标准的C ++足以在这里发布,不过它最初可能看起来像是我怎么做电脑游戏?问题,虽然我可能是错的。

我正在加载一个位图,因为我正在使用Linux,我必须自己定义
BITMAPFILEHEADER(也许更好的做法会是给我的
版本一个新名称或其他什么,我不知道)。 Windows使用WORD和DWORD来定义它,我使用了SDL提供的一些标准尺寸类型(
图形/声音库)来定义它:

struct BITMAPFILEHEADER
{Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

由于某种原因,我完全无法理解,但是,调试器显示我的
位图加载器函数认为sizeof(BITMAPFILEHEADER)== 16.

当然应该是14?
I''m pretty sure this is non-totally-trivial enough and standard-C++
enough to post here, however much it may initially look like a "how do I
make a computer game?" question, though I may be wrong.

I''m loading a bitmap, and as I''m using Linux I''ve had to define
BITMAPFILEHEADER myself (maybe better practice would be to give my
version a new name or whatever, I dunno). Windows uses WORDs and DWORDs
to
define it, I''ve used some standard-size types provided by SDL (a
graphics/sound library thing) to define it thus:

struct BITMAPFILEHEADER
{
Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

For some reason I utterly can''t fathom, however, a debugger shows that my
bitmap loader function believes that sizeof(BITMAPFILEHEADER) == 16.

Surely that should be 14?



[SNIP]


不一定:


bfType:2

填充以使bfSize在int边界上重新开始:2

bfSize:4

Res1:2
Res2:2

bfOffBits:4


那是16.您可能要查看gcc / g ++文档如何询问包装for

a struct。


-

WW aka Attila

:::

加粗在你所代表什么,并小心你的目标。


[SNIP]

Not necessarily:

bfType: 2
Padding to get bfSize to start again on int boundary: 2
bfSize: 4
Res1: 2
Res2: 2
bfOffBits: 4

That is 16. You may want to look at the gcc/g++ docs how to ask packing for
a struct.

--
WW aka Attila
:::
Be bold in what you stand for and careful what you fall for.


" James Gregory" < JA **** @ f2s.com>写道...
"James Gregory" <ja****@f2s.com> wrote...
我很确定这非常完全无足轻重,标准C ++足以在这里发布,不过它可能最初看起来像一个怎么样我做一个电脑游戏吗?问题,虽然我可能是错的。

我正在加载一个位图,因为我正在使用Linux,我必须自己定义
BITMAPFILEHEADER(也许更好的做法会是给我的版本
一个新的名字或其他什么,我不知道)。 Windows使用WORD和DWORD来定义它,我使用了SDL提供的一些标准尺寸类型(
图形/声音库)来定义它:
struct BITMAPFILEHEADER
{
Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

出于某种原因,我完全无法理解,但是,调试器显示我的
位图加载器函数认为sizeof(BITMAPFILEHEADER)== 16.

当然应该是14?

然后我尝试重新定义它几次:

BITMAPFILEHEADER
{Uint32 bfType; //从16改为32
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

这给出sizeof(BITMAPFILEHEADER)== 16,现在是正确的。

BITMAPFILEHEADER
{Uint32 bfType;
Uint32 bfSize;
Uint32 bfReserved1; < brint> bintReserved2;
Uint32 bfOffBits;
};

这给了sizeof(BITMAPFILEHEADER)== 20,这也是正确的。

很可能我只是误解了某个地方,但我不知道是什么。
I''m pretty sure this is non-totally-trivial enough and standard-C++ enough
to post here, however much it may initially look like a "how do I make a
computer game?" question, though I may be wrong.

I''m loading a bitmap, and as I''m using Linux I''ve had to define
BITMAPFILEHEADER myself (maybe better practice would be to give my version
a new name or whatever, I dunno). Windows uses WORDs and DWORDs to
define it, I''ve used some standard-size types provided by SDL (a
graphics/sound library thing) to define it thus:

struct BITMAPFILEHEADER
{
Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

For some reason I utterly can''t fathom, however, a debugger shows that my
bitmap loader function believes that sizeof(BITMAPFILEHEADER) == 16.

Surely that should be 14?

I then tried redefining it a couple of times:

BITMAPFILEHEADER
{
Uint32 bfType; //changed from 16 to 32
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

This gives sizeof(BITMAPFILEHEADER) == 16, which is now correct.

BITMAPFILEHEADER
{
Uint32 bfType;
Uint32 bfSize;
Uint32 bfReserved1;
Uint32 bfReserved2;
Uint32 bfOffBits;
};

This gives sizeof(BITMAPFILEHEADER) == 20, which is also correct.

Most likely I just misunderstand something somewhere, but I''ve no idea
what.




对象的大小可以包括所谓的填充。确保数组中的对象

从特定的字节边界开始。例如,

在某些平台上应该是4的倍数,以便更快地访问内存。

14不是4的倍数,因此它选择下一个4的较大倍数。


可能有[编译器特定的]方法来控制填充或

对齐因子,请查阅编译器文档以了解如何

那样做。


Victor



Sizes of object can include so called "padding" to ensure that the objects
in an array begin at a particular multiple of bytes boundary. For example,
on some platforms it should be multiple of 4 to make memory access faster.
14 is not multiple of 4, so it chooses the next larger multiple of 4.

There probably is a [compiler-specific] way to control the padding or the
alignment factor, consult your compiler documentation to find out how to
do that.

Victor


James Gregory写道:
James Gregory wrote:
我很确定这非常完全无足轻重,标准C ++足以在这里发布,不过它可能最初看起来像是如何制作
电脑游戏?问题,虽然我可能是错的。


放松 - 填充是关于主题的。

我正在加载位图,因为我正在使用Linux我必须定义
自己BITMAPFILEHEADER(也许更好的做法是给我的版本
一个新名称或其他什么,我不知道)。


必须有一个可以使用的图形库。从ImageMagick开始。

Windows使用WORD和DWORD来定义它,我使用了SDL提供的一些标准尺寸类型(
图形/声音库的东西)因此定义它:

struct BITMAPFILEHEADER
{Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

由于某种原因,我完全无法理解,但是,调试器显示我的
位图加载器函数认为sizeof(BITMAPFILEHEADER) == 16.

当然应该是14?
I''m pretty sure this is non-totally-trivial enough and standard-C++ enough
to post here, however much it may initially look like a "how do I make a
computer game?" question, though I may be wrong.
Relax - padding is on-topic.
I''m loading a bitmap, and as I''m using Linux I''ve had to define
BITMAPFILEHEADER myself (maybe better practice would be to give my version
a new name or whatever, I dunno).
There must be a graphics library you can use. Start with ImageMagick.
Windows uses WORDs and DWORDs to
define it, I''ve used some standard-size types provided by SDL (a
graphics/sound library thing) to define it thus:

struct BITMAPFILEHEADER
{
Uint16 bfType;
Uint32 bfSize;
Uint16 bfReserved1;
Uint16 bfReserved2;
Uint32 bfOffBits;
};

For some reason I utterly can''t fathom, however, a debugger shows that my
bitmap loader function believes that sizeof(BITMAPFILEHEADER) == 16.

Surely that should be 14?




大量的硬件最适合读取四字对齐的整数

边界。换句话说,bfSize的地址可以平均除以4

,余数为0。


编译器在bfType之间插入一个不可见的16位填充元素和

bfSize。允许编译器执行此操作,并且需要通知他们如何执行这些操作。它是指定的实现。


现在询问涵盖编译器的文档或论坛如何将其转为

off。他们可能会指出一个已经读取BMP文件的库,




-

Phlip
http://www.xpsd.org/cgi-bin/wiki? Tes ... UserInterfaces


这篇关于结构的大小 - 完全混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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