c语言中的offsetof()宏

来源:互联网 发布:免费教育网 建站 编辑:程序博客网 时间:2024/05/18 04:01

offsetof  :
    Retrieves the offset of a member from the beginning of its parent structure.

size_t offsetof(structName, memberName);

Parameters:
    structName : Name of the parent data structure.
    memberName :Name of the member in the parent data structure for which to determine the offset.

Return Value : offsetof returns the offset in bytes of the specified member from
          the beginning of its parent data structure. It is undefined for bit fields.
Remarks :
    The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.

Note  :
    offsetof is not a function and cannot be described using a C prototype.

 

 #define offsetof(s, m)   (size_t)&(((s *)0)->m)

s是一个结构名,它有一个名为m的成员(s和m 是宏offsetof的形参,它实际是返回结构s的成员m的偏移地址.

(s *)0 是骗编译器说有一个指向类(或结构)s的指针,其地址值0

&((s *)0)->m   是要取得类s中成员变量m的地址. 因基址为0,这时m的地址当然就是m在s中的偏移

最后转换size_t 型,即unsigned int。

有例子如:
struct   A
{
    int   i;
    int   j;
};
  
struct   A   *pA;
pA = new   A;
这时,pA实际上是一个Pointer, 指向某一确定的内存地址, 如0x1234;
而pA->i 整体是一个int型变量,其地址是&(pA->i), '&'为取址运算符;
那么&(pA->i)一定等于0x1234,因 i 是结构体A的第一个元素。
而&(pA->j)一定是0x1234 + 0x4 = 0x1238; 因为sizeof(int) = 4;
  
这个做法的巧妙之处就是:它把“0”作为上例中的pA,那么&(pA->j)就是 j 的offset

解析结果是:
(s*)0,将 0 强制转换为Pointer to "s"   
可以记 pS = (s*)0,pS是指向s的指针,它的值是0;
那么pS->m就是m这个元素了,而&(pS->m)就是m的地址,就是offset

下面是个offsetof应用的例子,其中宏OBJECT_HEAD_ADDRES的作用是根据一个对象或结构的某成员的地址,求其首地址。

 

完整的例子如下:

/* offsetof example */

#include "stdafx.h"
#include <stdio.h>
#include <stddef.h>

/************************************************************************/
/* Macro OBJECT_HEAD_ADDRESS : calculate the struct's address according
/* to one of its member's address
/************************************************************************/
#define OBJECT_HEAD_ADDRESS(ClassName,MemberName,Addre) /
Addre - offsetof(ClassName, MemberName)

struct S
{
int ID;
char *addr;
char name[20];
};

struct S1
{
S employee;
char *title;
};

struct S2
{
char singlechar;
int arraymember[10];
char anotherchar;
};

int main(int argc, char* argv[])
{
/* Example 1
S2 s2 = {'a', 10, 'b'};
int head = int(&(s2.singlechar));
int memb = int(&(s2.anotherchar));

int i = OBJECT_HEAD_ADDRESS(S2, anotherchar, memb);

printf ("offsetof(S2, singlechar) is %d/n", offsetof(S2, singlechar));
printf ("offsetof(S2, arraymember) is %d/n", offsetof(S2, arraymember));
printf ("offsetof(S2, anotherchar) is %d/n", offsetof(S2, anotherchar));
printf("s2.anotherchar's address is %x ==> s2's address is %x/n", memb, i);
*/
/* Example 2
S s = {100, "Nanjing", "Thomas"};
int id = int(&(s.ID));
int ad = int(&(s.addr));
int nm = int(&(s.name));

int j = OBJECT_HEAD_ADDRESS(S, addr, ad);

printf ("offsetof(S, ID) is %d/n", offsetof(S, ID));
printf ("offsetof(S, addr) is %d/n", offsetof(S, addr));
printf ("offsetof(S, name) is %d/n", offsetof(S, name));
printf ("s.ID's address : %x/ns.addr's address : %x/ns.name's address : %x/n", id, ad, nm);
printf("s.addr's address is %x ==> s's address is %x/n", ad, j);
*/
/* Example 3 */
S1 s1 = {{100, "Nanjing", "Thomas Chen"},"Thomas Chen"};
int td = int(&(s1.title));
int k = OBJECT_HEAD_ADDRESS(S1, title, td);

printf ("offsetof(S1, employee) is %d/n", offsetof(S1, employee));
printf ("offsetof(S1, title) is %d/n", offsetof(S1, title));
printf("s1.title's address is %x ==> s1's address is %x/n", td, k);

return 0;
}
/* Example 1 Output */
offsetof(S2, singlechar) is 0
offsetof(S2, arraymember) is 4
offsetof(S2, anotherchar) is 44
s2.anotherchar's address is 12ff7c ==> s2's address is 12ff50

/* Example 2 Output */
offsetof(S, ID) is 0
offsetof(S, addr) is 4
offsetof(S, name) is 8
s.ID's address : 12ff64
s.addr's address : 12ff68
s.name's address : 12ff6c
s.addr's address is 12ff68 ==> s's address is 12ff64

/* Example 3 Output */
offsetof(S1, employee) is 0
offsetof(S1, title) is 28
s1.title's address is 12ff7c ==> s1's address is 12ff60


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/thomas_nuaa/archive/2008/12/17/3542572.aspx

原创粉丝点击