如何最好地获取结构中字段的偏移量? [英] how best to get the offset of a field within a struct?

查看:66
本文介绍了如何最好地获取结构中字段的偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:

我想用一个结构来计算一个字段的字节偏移量,而不是实例化结构的一个实例,而不是实例化结构的实例

运行时间我希望以符合ANSI标准的方式执行此操作

时尚。


讨论:


以下是演示我的解决方案的示例程序。我的问题

是:


(1)符合ANSI C标准吗?

(2)有更好的方法解决这个问题?

我认为我在安全的地方,因为施放和获取

的地址不会导致内存被访问(即指针

dereferenced)。无论如何我似乎对GCC感到满意:

cd c:/ cygwin / home / fj / offset /
make offset&& ./offset
gcc -ansi -pedantic -o offset offset.c
MY_OFFSET(verybig_t,a)= 0x0
MY_OFFSET(verybig_t,g)= 0x20
MY_OFFSET(verybig_t, j)= 0x202a
MY_OFFSET(verybig_t,p)= 0x203a

编译于2月17日星期五11:41:58完成




问候,

--jfc


================开始示例程序offset.c == ==============

#include< stdio.h>

typedef struct {

long a,b,c;

短e,f;

char name [13];

long g,h;

char stuff [1<< 13];

短i;

char j;

长k,l,m;

char n ,o,p;

} verybig_t;


#define MY_OFFSET(类型,字段)((unsigned long)&(((type *)

0) - >字段))


int main(int argc,char * argv [])

{

printf(" MY_OFFSET(verybig_t,a)= 0x%x \ n",MY_OFFSET(verybig_t,

a));

printf( MY_OFFSET(verybig_t,g)= 0x%x \ n,MY_OFFSET(verybig_t,

g));

printf(" MY_OFFSET(verybig_t,j) = 0x%x \ n",MY_OFFSET(verybig_t,

j));

printf(" MY_OFFSET(verybig_t,p)= 0x%x \ n" ,MY_OFFSET(verybig_t,

p));

返回(0);

}

解决方案

funkyjaécrit:

#define MY_OFFSET(type,field)((unsigned long)&(((type *)
0) - > field))



你应该把它投到uintptr_t而不是unsigned long。





funkyj在02/17/06 14:44写道:

问题声明:
我想在编译时用结构计算字段的字节偏移量在运行时实例化struct的实例我希望以符合ANSI标准的方式执行此操作。


使用在< stddef.h>中定义的offsetof()宏。

讨论:

下面是一个示例演示我的解决方案的程序。我的问题是:

(1)这符合ANSI C吗?


No.

(2)有没有更好的方法来解决这个问题?


使用在< stddef.h>中定义的offsetof()宏。

[剪断了非便携式ANSI前替代品的变体
用于offsetof()宏,在< stddef.h>中定义。




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


谢谢!显然我并不是那么遥远。我的本地包含文件

实现如下:


#define __offsetof(type,field)((size_t)(&((type *)0 ) - >字段))

/ * ansi.h * /

#define offsetof(type,member)__offsetof(type,member)/ *

stddef.h * /


PROBLEM STATEMENT:
I want to calculate the byte offset of a field with a struct at
compile time without instantiating an instance of the struct at
runtime AND I want to do this in an ANSI standard compliant
fashion.

DISCUSSION:

Below is a sample program that demonstrates my solution. My questions
are:

(1) is this ANSI C compliant?
(2) is there a better way to solve this problem?
I think I''m on safe ground because casting and taking the address of
stuff does not cause memory to be accessed (i.e. pointers
dereferenced). In any event I seem to have satisfied GCC:

cd c:/cygwin/home/fj/offset/
make offset && ./offset
gcc -ansi -pedantic -o offset offset.c
MY_OFFSET(verybig_t, a) = 0x0
MY_OFFSET(verybig_t, g) = 0x20
MY_OFFSET(verybig_t, j) = 0x202a
MY_OFFSET(verybig_t, p) = 0x203a

Compilation finished at Fri Feb 17 11:41:58



Regards,
--jfc

================ begin sample program offset.c ================
#include <stdio.h>
typedef struct {
long a, b, c;
short e, f;
char name[13];
long g, h;
char stuff[1 << 13];
short i;
char j;
long k, l, m;
char n, o, p;
} verybig_t;

#define MY_OFFSET(type, field) ((unsigned long ) &(((type *)
0)->field))

int main(int argc, char * argv[])
{
printf("MY_OFFSET(verybig_t, a) = 0x%x\n", MY_OFFSET(verybig_t,
a));
printf("MY_OFFSET(verybig_t, g) = 0x%x\n", MY_OFFSET(verybig_t,
g));
printf("MY_OFFSET(verybig_t, j) = 0x%x\n", MY_OFFSET(verybig_t,
j));
printf("MY_OFFSET(verybig_t, p) = 0x%x\n", MY_OFFSET(verybig_t,
p));
return(0);
}

解决方案

funkyj a écrit :

#define MY_OFFSET(type, field) ((unsigned long ) &(((type *)
0)->field))



You should cast it to uintptr_t rather than unsigned long.




funkyj wrote On 02/17/06 14:44,:

PROBLEM STATEMENT:
I want to calculate the byte offset of a field with a struct at
compile time without instantiating an instance of the struct at
runtime AND I want to do this in an ANSI standard compliant
fashion.
Use the offsetof() macro, defined in <stddef.h>.
DISCUSSION:

Below is a sample program that demonstrates my solution. My questions
are:

(1) is this ANSI C compliant?
No.
(2) is there a better way to solve this problem?
Use the offsetof() macro, defined in <stddef.h>.
[snipped a variant of the non-portable pre-ANSI substitute
for the offsetof() macro, defined in <stddef.h>]



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


Thanks! Apparently I wasn''t that far off. My local include files
implement it like this:

#define __offsetof(type, field) ((size_t)(&((type *)0)->field))
/* ansi.h */
#define offsetof(type, member) __offsetof(type, member) /*
stddef.h */


这篇关于如何最好地获取结构中字段的偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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