offsetof的使用? [英] Uses of offsetof?

查看:87
本文介绍了offsetof的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于个人的好奇心:)


人们使用offsetof()做什么?我的意思是,我可以理解为什么你会想要能够获取结构成员的地址,但是你只需&&b
就可以;(sa)或类似的。为什么你会关心

偏移量(这肯定取决于编译器如何选择在内存中放置

结构),我真的不知道。如果你确实这么做了,那么就不会抵消(s,a)只是&(sa) - & s?

Just out of personal curiosity :)

What do people use offsetof() for? I mean, I can understand why you''d
want to be able to take the address of a member of a struct, but you
can do that with just &(s.a) or similar. Why you''d care about the
offset (which surely depends on how the compiler chooses to lay the
struct out in memory), I don''t really know. And if you did really
care, won''t offset(s,a) just be &(s.a) - &s ?

推荐答案

3月24日上午12:06,Francine.Ne ... @ googlemail.com写道:
On Mar 24, 12:06 am, Francine.Ne...@googlemail.com wrote:

Just出于个人的好奇心:)


人们使用offsetof()做什么?我的意思是,我可以理解为什么你会想要能够获取结构成员的地址,但是你只需&&b
就可以;(sa)或类似的。为什么你会关心

偏移量(这肯定取决于编译器如何选择在内存中放置

结构),我真的不知道。如果你确实这么做了,那么就不会错过(s,a)只是&(s.a) - & s?
Just out of personal curiosity :)

What do people use offsetof() for? I mean, I can understand why you''d
want to be able to take the address of a member of a struct, but you
can do that with just &(s.a) or similar. Why you''d care about the
offset (which surely depends on how the compiler chooses to lay the
struct out in memory), I don''t really know. And if you did really
care, won''t offset(s,a) just be &(s.a) - &s ?



嗯,我自己一直在做一些实验...我的最后一个

声明似乎是假的。例如,考虑

以下代码:


#include< stdio.h>

#include< stdlib。 h>

#include< stddef.h>

#include< string.h>


#define STRING" ;只是一个测试


struct s {int a; char * b; int c; };

struct t {char * b; int c; };


main()

{

struct sa;

aa = 10,ab = malloc(strlen(STRING)+1),strcpy(ab,STRING),ac = 20;

struct t * b =& ab;

struct t * c =& a + offsetof(struct s,b);

printf("%s:%d \ n",b-> b,b-> c);

printf("%s:%d \ n",c-> b,c-> c);

}

现在看看会发生什么:

Hmmm, I''ve been doing some experiments with this myself... The last
statement of mine seems to be false. For example, consider the
following code:

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#define STRING "just a test"

struct s { int a; char *b; int c; };
struct t { char *b; int c; };

main()
{
struct s a;
a.a=10, a.b=malloc(strlen(STRING)+1), strcpy(a.b,STRING), a.c=20;
struct t *b=&a.b;
struct t *c=&a+offsetof(struct s,b);
printf("%s : %d\n", b->b, b->c);
printf("%s : %d\n", c->b, c->c);
}

Now see what happens:


./ a.out

只是一个测试:20
(null): - 1208827916


所以offsetof()对于找到子结构的方式甚至没有用!

./a.out
just a test : 20
(null) : -1208827916

So offsetof() isn''t even useful for finding your way to substructures!


在文章< 11 ********************** @ b75g2000hsg.googlegroups .com> ;,

< Fr ************ @ googlemail.comwrote:
In article <11**********************@b75g2000hsg.googlegroups .com>,
<Fr************@googlemail.comwrote:

struct sa;
struct s a;



[...]

[...]


struct t * c =& a + offsetof(struct s ,b);
struct t *c=&a+offsetof(struct s,b);



由于& a属于(struct s *)类型,因此添加的是

大小的单位。如果你这样做
struct t * c =((char *)& a)+ offsetof(struct s,b);


你会得到你期望的答案。


offsetof()的一个用途是有效地传递一个

结构的成员,当* which *成员时它在编译时是不知道的。

而是传递所需成员的偏移量。当然,你还需要以某种方式知道你想要操作的

对象的类型 - 或者至少是大小 -


- Richard

-

考虑到需要多达32个字符

字母" - 1963年的X3.4。

Since &a is of type (struct s *), the addition is in units of the
size of s. If you do

struct t *c=((char *)&a)+offsetof(struct s,b);

you will get the answer you expect.

One use of offsetof() is to effectively pass around a member of a
structure, when *which* member it is is not known at compile time.
Instead you pass the offset of the desired member. Of course, you
also need to somehow know the type - or at least the size - of the
object you wish to manipulate.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


这篇关于offsetof的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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