每个成员3个结构 [英] struct off by 3 per member

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

问题描述




当我通过添加到指针地址移动到下一个entrie时,我是
off 6个字节。然后我改变了我的代码,所以我的结构将有一个

额外成员,pad,然后它被9个字节关闭。选项和s在

内存地址0x019f0176当我添加到它应该在0x019f0179

而不是我得到0x019f017f没有pad和0x019f0182有pad。我不能

弄明白为什么它总是关闭。


struct option_entries {

unsigned char code;

unsigned char len;

unsigned char value;

unsigned char pad; //稍后添加以查看新的

成员会将其抵消多少。

};


const u_char *数据;


选项=(struct option_entries *)数据;

s =(unsigned char *)选项;


//选项 - > len = 1

选项=(option_entries *)s +选项 - > len + 2;



when I move to the next entrie by adding to the pointers address, I''m
off by 6 bytes. Then I changed my code so my struct would have an
extra member, pad, and then it was off by 9 bytes. option and s are at
memory address 0x019f0176 when I add to it should be at 0x019f0179
instead I get 0x019f017f without pad and 0x019f0182 with pad. I can''t
figure out why its always off.

struct option_entries {
unsigned char code;
unsigned char len;
unsigned char value;
unsigned char pad; //added later to check to see how much a new
member would offset it by.
};

const u_char *data;

option = (struct option_entries *)data;
s = (unsigned char *)option;

//option->len = 1
option = (option_entries *)s + option->len + 2;

推荐答案

js5895< Jo ***** @ nycap.rr.comwrites:
js5895 <Jo*****@nycap.rr.comwrites:

当我通过添加指针移动到下一个entrie时地址,我是
off 6个字节。然后我改变了我的代码,所以我的结构将有一个

额外成员,pad,然后它被9个字节关闭。选项和s在

内存地址0x019f0176当我添加到它应该在0x019f0179

而不是我得到0x019f017f没有pad和0x019f0182有pad。我不能

弄明白为什么它总是关闭。


struct option_entries {

unsigned char code;

unsigned char len;

unsigned char value;

unsigned char pad; //稍后添加以查看新的

成员会将其抵消多少。

};
when I move to the next entrie by adding to the pointers address, I''m
off by 6 bytes. Then I changed my code so my struct would have an
extra member, pad, and then it was off by 9 bytes. option and s are at
memory address 0x019f0176 when I add to it should be at 0x019f0179
instead I get 0x019f017f without pad and 0x019f0182 with pad. I can''t
figure out why its always off.

struct option_entries {
unsigned char code;
unsigned char len;
unsigned char value;
unsigned char pad; //added later to check to see how much a new
member would offset it by.
};



您假设结构的大小是其成员的大小为
的总和。请参阅comp.lang.c FAQ中的问题2.12和2.13,

< http://www.c-faq.com/>。


要确定结构的大小,请使用sizeof运算符;添加

会员的大小往往会给你一个错误的答案。


我很确定这是你问题的答案。以下是

回答你没有问过的其他几个问题,但应该是b

You''re assuming that the size of a struct is the sum of the sizes of
its members. See questions 2.12 and 2.13 in the comp.lang.c FAQ,
<http://www.c-faq.com/>.

To determine the size of a structure, use the sizeof operator; adding
up the sizes of the members will often give you a wrong answer.

I''m fairly sure this is the answer to your question. What follows are
answers to several other questions that you didn''t ask, but should
have.


const u_char * data;
const u_char *data;



我没有看到u_char的声明。我可以猜测,对于unsigned char来说,这是一个typedef

,但为什么不用它的真实名字来调用它,未签名

char?

而且你没有初始化数据。我认为你用你的真实代码做了 -

这就是为什么给我们展示*你的真实代码,而不是

,这是一个非常好的主意。它。你这次幸运的是,

是足够的信息来弄清楚你的问题是什么,但是如果你不知道这个问题那么你就不知道问题了知道你的实际的b $ b b代码的哪些部分是重要的,哪些不是。

I don''t see a declaration of u_char. I can guess that it''s a typedef
for unsigned char, but why not just call it by its real name, unsigned
char?

And you don''t initialize data. I presume you do in your real code --
which is why it''s a very good idea to *show us* your real code, rather
than some paraphrase of it. You lucked out this time in that there
was enough information to figure out what your problem is, but if you
don''t know the problem then you don''t know what parts of your actual
code are important and which aren''t.


option =(struct option_entries *)data;
option = (struct option_entries *)data;



声明选项在哪里?

Where is option declared?


s =(unsigned char *)选项;
s = (unsigned char *)option;



声明在哪里?

And where is s declared?


//选项 - > len = 1

option =(option_entries *)s + option-> len + 2;
//option->len = 1
option = (option_entries *)s + option->len + 2;



您还没有声明一个名为option_entries的类型。您已声明名为struct option_entries的

类型。如果以上一行编译,那么

要么有一个你不打算向我们展示的typedef,或者

你正在编译你的代码C ++编译器。如果它没有b
编译,或者你还没有尝试编译它,那么将它发布在这里

是浪费时间(除非你''再问为什么它不编译,但是

然后你需要向我们展示编译器的错误信息。


你''使用指向你结构的指针和指向

unsigned char的指针。除非你有特定的理由以字节的形式访问你的
结构,否则只需在整个

中使用``struct option_entries *'''并删除演员表。


推荐阅读:< http://www.catb.org/~esr/faqs/smart-questions.html>。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,Yes Minister

You haven''t declared a type named "option_entries". You''ve declared a
type named "struct option_entries". If the above line compiles, then
either there''s a typedef that you haven''t bothered to show us, or
you''re compiling your code with a C++ compiler. If it doesn''t
compile, or if you haven''t tried to compile it, then posting it here
is a waste of time (unless you''re asking why it doesn''t compile, but
then you''d need to show us the compiler''s error message).

You''re using a mixture of pointers to your structure and pointers to
unsigned char. Unless you have a specific reason to access your
structures as bytes, just use ``struct option_entries*'''' throughout
and drop the casts.

Recommended reading: <http://www.catb.org/~esr/faqs/smart-questions.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


>当我搬家时通过添加指针地址到下一个entrie,我是
>when I move to the next entrie by adding to the pointers address, I''m

> off 6个字节。
>off by 6 bytes.



您需要通过添加到char *而不是

struct option_entries *来移动到下一个条目。如果你移动3个结构,你移动9

字节(假设没有垫的结构版本),

是6个额外的,并对应你的正在看。

You need to move to the next entry by adding to a char *, not to a
struct option_entries *. If you move by 3 structs, you move by 9
bytes (assuming the version of the struct without the pad), which
is 6 extra, and corresponds to what you are seeing.


>然后我改变了我的代码,所以我的结构将有一个额外的成员,pad,然后它被9个字节关闭。选项和s在内存地址0x019f0176,当我添加它应该在0x019f0179
而不是我得到0x019f017f没有焊盘和0x019f0182与焊盘。我无法弄明白为什么它总是关闭。

struct option_entries {
unsigned char code;
unsigned char len;
unsigned char value ;
unsigned char pad; //稍后添加,以查看新的
成员将多少抵消它。
};

const u_char * data;

选项=(struct option_entries *)data;
s =(unsigned char *)选项;

// option-> len = 1
option =(option_entries *)s +选项 - > len + 2;
>Then I changed my code so my struct would have an
extra member, pad, and then it was off by 9 bytes. option and s are at
memory address 0x019f0176 when I add to it should be at 0x019f0179
instead I get 0x019f017f without pad and 0x019f0182 with pad. I can''t
figure out why its always off.

struct option_entries {
unsigned char code;
unsigned char len;
unsigned char value;
unsigned char pad; //added later to check to see how much a new
member would offset it by.
};

const u_char *data;

option = (struct option_entries *)data;
s = (unsigned char *)option;

//option->len = 1
option = (option_entries *)s + option->len + 2;



我在这里看不到s的声明,但我希望它是'unsigned char *。

I don''t see a declaration of s here but I hope it''s unsigned char *.


> option =(option_entries *)(s + option-> len + 2);
>option = (option_entries *)(s + option->len + 2);


js5895写道:
js5895 wrote:

hi,


当我通过添加指针地址移动到下一个entrie时,我是
off 6个字节。然后我改变了我的代码,所以我的结构将有一个

额外成员,pad,然后它被9个字节关闭。选项和s在

内存地址0x019f0176当我添加到它应该在0x019f0179

而不是我得到0x019f017f没有pad和0x019f0182有pad。我不能

弄明白为什么它总是关闭。


struct option_entries {

unsigned char code;

unsigned char len;

unsigned char value;

unsigned char pad; //稍后添加以查看新的

成员会将其抵消多少。

};


const u_char *数据;


选项=(struct option_entries *)数据;

s =(unsigned char *)选项;


//选项 - > len = 1

选项=(option_entries *)s + option-> len + 2;


when I move to the next entrie by adding to the pointers address, I''m
off by 6 bytes. Then I changed my code so my struct would have an
extra member, pad, and then it was off by 9 bytes. option and s are at
memory address 0x019f0176 when I add to it should be at 0x019f0179
instead I get 0x019f017f without pad and 0x019f0182 with pad. I can''t
figure out why its always off.

struct option_entries {
unsigned char code;
unsigned char len;
unsigned char value;
unsigned char pad; //added later to check to see how much a new
member would offset it by.
};

const u_char *data;

option = (struct option_entries *)data;
s = (unsigned char *)option;

//option->len = 1
option = (option_entries *)s + option->len + 2;



除了Keith和Gordon的评论之外,你的问题可能会更好地适用于comp.lang.c ++而不是这个新闻组。 (要么

那么,或者你已经省略了*太多的实际代码。)


-
Er ********* @ sun.com


这篇关于每个成员3个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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