2/4字节边界问题 [英] 2/4 bytes boundary problem

查看:66
本文介绍了2/4字节边界问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




正如我们所知,编译器将填充结构使其在4 b / b
字节边界上对齐。

我有这样的结构。

typedef struct {

uint8 a;

uint8 b;

uint8 c;

uint16 d;

...

} s;

s s1;

... func(...,& s1.d,...){...}

但是在调用func()时我总是崩溃。我互换了c的

位置。和d,任何事情都没问题。

因为编译器会正确填充它。为什么我无法获取地址

" d"?

虽然c的位置是和d被交换,d仍然不在

4字节边界。但它运作良好,为什么?


托马斯

Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?

Thomas

推荐答案

2006-05- 15,Thomas.Chang< Mi *********** @ gmail.com>写道:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:


我们知道,编译器将填充结构使其在4字节边界上对齐。
不,这根本不保证。你在哪里听到的?

我得到了这样的结构。
typedef struct {
uint8 a;
uint8 b;
uint8 c;
为什么不使用字符?

uint16 d;
如果你坚持16位,试试小int

...
} s;
s s1;
... func(。 ..,& s1.d,...){...}
但是在调用func()时我总是崩溃。我互换了c的
位置。和d,任何事情都没问题。
因为编译器会正确填充它。为什么我无法获取
d的地址?
尽管c的位置是c。和d被交换,d仍然没有在4字节边界上。但它运作良好,为什么?
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries. No, that''s not guaranteed at all. Where did you hear that?
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c; Why not just use chars?
uint16 d; And if you insist on 16 bits, try small int
...
}s;
s s1;
... func(..., &s1.d, ...){...}
But I always got crash when calling func(). I interchanged the
position of "c" and "d", anything went ok.
As the compiler will pad it right. Why I cannot fetch the address of
"d"?
Although the position of "c" and "d" is swapped, "d" is still not on
4 byte boundary. But it works well, why?






Andrew Poelstra写道:
Andrew Poelstra wrote:
2006-05-15,Thomas .Chang< Mi *********** @ gmail.com>写道:
On 2006-05-15, Thomas.Chang <Mi***********@gmail.com> wrote:


我们知道,编译器将填充结构使其在4字节边界上对齐。
Hi,

As we know, the compiler will pad structure to make it align on 4
byte boundaries.


不,那根本不保证。你在哪里听到的?


No, that''s not guaranteed at all. Where did you hear that?

我得到了这样的结构。
typedef struct {
uint8 a;
uint8 b;
uint8 c;
I got a structure like this.
typedef struct{
uint8 a;
uint8 b;
uint8 c;


为什么不使用字符?


Why not just use chars?




unsigned char在这种情况下是正确的建议。



unsigned char would be the correct recommendation in this case.

uint16 d;
uint16 d;


如果你坚持16位,试试小int


And if you insist on 16 bits, try small int




我不知道什么是小int但是在大多数实现中,相应的类型将是

unsigned short int。如果OP想要16个b / b
位,这可能不合适,因为short int可能超过16位。


Robert Gamble



I don''t know what a "small int" is but the corresponding type would be
unsigned short int on most implementations. If the OP wants exactly 16
bits this may not be suitable as short int can be more than 16 bits.

Robert Gamble


为结构设置编译器优化禁用。


像这样:


typedef struct {

uint8 a;

uint8 b;

uint8 c;

uint16 d;

...

} __ attribute __((打包))s;

set the compiler optimization disable for the structure.

like this:

typedef struct{
uint8 a;
uint8 b;
uint8 c;
uint16 d;
...
} __attribute__((packed)) s;


这篇关于2/4字节边界问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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