重新定义,并指向结构 [英] redefining, and casting pointers to, structs

查看:59
本文介绍了重新定义,并指向结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些似乎与此类似的代码


struct foo {

char offset [XXX];

int d;

};


struct foo {

int a;

int b;

char c;

int d; //与其他d相同$

};


结构在不同文件中的定义不同。我试着通过从一个

类型向另一个转换指向这些结构的指针来自己做这个。我希望偏移后的成员对齐,所以我尝试设置XXX = sizeof(int)* 2 + sizeof(char)的
。但编译器

用3个字节填充了这个char;所以,显然它没有用。然后

我尝试嵌入一个定义为


struct inner_foo的结构{

int a;

int b;

char c;

};


我在foo中使用了它。然后我将XXX设置为sizeof(inner_foo)。并且

有效。所以我的问题是,这是安全和便携的吗?如果不是,

请提出建议。另外,编译器是否总是按照你声明的顺序布置

结构成员?


谢谢


-Todd

解决方案

2006年1月3日15:25:46 -0800,在comp.lang.c中,
go******@signalsguru.net 写道:

我看到一些似乎与此类似的代码

struct foo {
char offset [XXX];
int d;
};

struct foo {
int a;
int b;
char c;
int d; //与其他d
}相同;

....所以我的问题是,这是安全和便携的吗?


编号类型的宽度因系统而异,编译器允许
在结构中的对象之间添加填充。它可以在你的inner_foo上以不同的方式填充



如果不是,请提出建议。


我不知道为什么会这样做 - 如果你有foo的定义

为什么不使用它?

另外,编译器是否总是按照你声明它们的相同顺序布置
结构成员?




是的,但不一定全部依偎在一起。

马克麦金太尔

-


---- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全的Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----


go ****** @ signalsguru.net 写道:

我看到一些代码似乎做类似的事情

struct foo {
char offset [XXX];
int d;
};

struct foo {
int a;
int b;
char c;
int d; //与其他d相同
}

结构在不同的文件中定义不同。


要么你误读了代码,要么代码是*最好*高度

平台特定的,最糟糕的是完全坏了。

我尝试通过将指针从一种类型的指针转​​换为另一种类型来自行完成。我希望偏移后的成员对齐,所以我尝试设置XXX = sizeof(int)* 2 + sizeof(char)。但是编译器用3个字节填充了那个char;所以,显然它没有用。


事实上,它是允许的。编译器可以出于任何原因在结构的任何

成员之后插入填充。

然后
我尝试嵌入定义为
的结构 struct inner_foo {
int a;
int b;
char c;
};

我在foo中使用了它。然后我将XXX设置为sizeof(inner_foo)。并且
有效。所以我的问题是,这是安全和便携的吗?


编号编译器可以选择在struct inner_foo之后放置填充。

见上面的评论。

如果不是,

我建议不要这样做。

此外,编译器是否总是按照你声明的顺序布置
结构成员?




是的。


如果你告诉我们你要解决的问题,以及

你写的代码(减少到合理的大小,但仍然完整

和可编辑),我们可能会建议其他方法。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址说垃圾邮件,但这是真实的,我读了它。


如果不是,请提出建议。



我不确定为什么会这样做 - 如果你有foo的定义
为什么不使用它?



我认为原因是因为你可能没有定义。假设

你将指针传递给不关心偏移量的代码。

该代码只需要访问成员d。它可能正在接收

指向它不知道的各种定义的结构。

如果定义可以改变,它也不会知道。

-Todd


I saw some code that appeared to do something similar to this

struct foo {
char offset[XXX];
int d;
};

struct foo {
int a;
int b;
char c;
int d; //same as other d
};

And the structs are defined differently in different files. I tried
doing this myself by just casting pointers to these structs from one
type to the other. I want the members after offset to align, so I
tried setting XXX = sizeof(int)*2+sizeof(char). But the compiler
padded that char with 3 more bytes; so, obviously it didn''t work. Then
I tried embedding a struct defined as

struct inner_foo {
int a;
int b;
char c;
};

And I used that inside foo. Then I set XXX to sizeof(inner_foo). And
that worked. So my question is, is that safe and portable? If no,
please make a suggestion. Also, does the compiler always layout the
struct members in the same order you declare them?

thank you

-Todd

解决方案

On 3 Jan 2006 15:25:46 -0800, in comp.lang.c ,
go******@signalsguru.net wrote:

I saw some code that appeared to do something similar to this

struct foo {
char offset[XXX];
int d;
};

struct foo {
int a;
int b;
char c;
int d; //same as other d
};
....So my question is, is that safe and portable?
No. The width of types varies from system to system and the compiler
is allowed to add padding between objects in a struct. It could pad
around your inner_foo differently on different implementations.
If no, please make a suggestion.
I''m not sure why one would do this - if you have a definition of foo
why not use it?
Also, does the compiler always layout the
struct members in the same order you declare them?



Yes, but not necessarily all snuggled up together.
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


go******@signalsguru.net wrote:

I saw some code that appeared to do something similar to this

struct foo {
char offset[XXX];
int d;
};

struct foo {
int a;
int b;
char c;
int d; //same as other d
};

And the structs are defined differently in different files.
Either you are misreading the code or the code is at *best* highly
platform specific and at worst completely broken.
I tried
doing this myself by just casting pointers to these structs from one
type to the other. I want the members after offset to align, so I
tried setting XXX = sizeof(int)*2+sizeof(char). But the compiler
padded that char with 3 more bytes; so, obviously it didn''t work.
As, indeed, it is allowed to. The compiler can insert padding after any
member of a struct for any reason.
Then
I tried embedding a struct defined as

struct inner_foo {
int a;
int b;
char c;
};

And I used that inside foo. Then I set XXX to sizeof(inner_foo). And
that worked. So my question is, is that safe and portable?
No. The compiler could choose to place padding after struct inner_foo.
See comment above.
If no,
please make a suggestion.
I suggest not doing it.
Also, does the compiler always layout the
struct members in the same order you declare them?



Yes.

If you tell us what problem you are trying to solve, together with the
code you have written (cut down to a sensible size but still complete
and compilable), we might then be able to suggest alternative approaches.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


If no, please make a suggestion.



I''m not sure why one would do this - if you have a definition of foo
why not use it?


I think the reason is because you may not have a definition. Suppose
you pass a pointer around to code that does not care what''s in offset.
That code would only need to access member d. It may be receiving
pointers to structs of various definitions that it does not know about.
Nor could it konw if the definition can change.

-Todd


这篇关于重新定义,并指向结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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