Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中函数的偏移_C++ - Fatal编程技术网

C++中函数的偏移

C++中函数的偏移,c++,C++,这是代码 #include <stdio.h> #include <stddef.h> struct mystruct { char singlechar; char arraymember[10]; char anotherchar; }; int main () { printf ("offsetof(mystruct,singlechar) is %d\n",offsetof(mystruct,singlechar)); printf ("o

这是代码

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

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};

int main ()
{
  printf ("offsetof(mystruct,singlechar) is %d\n",offsetof(mystruct,singlechar));
  printf ("offsetof(mystruct,arraymember) is %d\n",offsetof(mystruct,arraymember));
  printf ("offsetof(mystruct,anotherchar) is %d\n",offsetof(mystruct,anotherchar));

  return 0;
}
我已经阅读了这个文档

http://www.cplusplus.com/reference/clibrary/cstddef/offsetof/
但我不懂不,不是因为我的英语,但我不懂文档本身,请任何人给我一个小例子或告诉我它是如何工作的? 谢谢你试过了吗?信不信由你,它有一篇关于宏偏移量的信息性文章:

offsetof既不是函数也不是过程,而是一个预处理器宏,它显然扩展为

#define offsetof(st, m) \
    ((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
作为一种可能的用途,Wikipedia提到了C语言中通用数据结构的实现

在谷歌代码搜索中快速搜索可以发现,它确实在许多项目中用于此目的和其他目的。

您尝试过吗?信不信由你,它有一篇关于宏偏移量的信息性文章:

offsetof既不是函数也不是过程,而是一个预处理器宏,它显然扩展为

#define offsetof(st, m) \
    ((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
作为一种可能的用途,Wikipedia提到了C语言中通用数据结构的实现


Google代码搜索中的快速搜索显示,它确实在许多项目中用于此目的和其他目的。

如果您有指向该结构的指针,offsetof返回的值指示您必须将指针调整为指向该结构的单个元素的指针的字节数


如果你不太明白它是如何工作的,你最好完全避免它。这是C++允许你在脚上开枪的一种方法。

< P>如果你有一个指向结构的指针,OffStof返回的值指示你需要调整多少个字节来指向一个结构的单独元素的指针。 如果你不太明白它是如何工作的,你最好完全避免它。这是C++允许你在脚上开枪的一种方法。

具有这种结构

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};
offsetoff函数以字节为单位告诉您同一结构中两个内存地址之间的距离

您将获得以下输出:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11
因为char使用1字节。使用 结构名称、结构元素的集合;计算从结构的第一个元素到结构元素的偏移量

offsetofmystruct,singlechar为0 因为singlechar被声明为 首先是结构内部。 OffsetOfSystemStruct,arraymember为1 因为arraymember被声明为 第二个在结构中,1byte之前被声明为singlechar。 OffsetOfSystem结构,arraymember为11 因为有11个字节来自 从另一个字符开始的结构 singlechar为1字节,singlechar为10字节 阵列成员。 希望这能让你明白…有这样的结构

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};
offsetoff函数以字节为单位告诉您同一结构中两个内存地址之间的距离

您将获得以下输出:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11
因为char使用1字节。使用 结构名称、结构元素的集合;计算从结构的第一个元素到结构元素的偏移量

offsetofmystruct,singlechar为0 因为singlechar被声明为 首先是结构内部。 OffsetOfSystemStruct,arraymember为1 因为arraymember被声明为 第二个在结构中,1byte之前被声明为singlechar。 OffsetOfSystem结构,arraymember为11 因为有11个字节来自 从另一个字符开始的结构 singlechar为1字节,singlechar为10字节 阵列成员。
希望这能说明问题……

offsetof告诉您在结构的内存分配中可以找到特定的成员

您定义的结构占用12个字节:

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};
字节0:singlechar 字节1:arraymember[0] 字节2:arraymember[1] 字节3:arraymember[2] ... 字节10:arraymember[9] 字节11:另一个字符 这就是您获得输出的原因:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11
如果分配该类型的对象,并在结构的开头获得一个字节*,则可以使用offsetof找出每个成员的位置。如果使用该指针偏移量,并将其转换回正确的类型,它将为您提供指向该成员的指针

mystruct s;
s.anotherchar = 5;
char* pBeginningOfS = (char*)(void*)(&s);
char* pAnotherChar = pBeginningOfS + offsetof(mystruct, anotherchar);
*pAnotherChar = 17;
printf("anotherchar is %d", s.anotherchar);
return 0;
这将输出:

anotherchar is 17

不能假定每个成员都是结构开头的特定偏移量的原因很复杂,并且依赖于编译器。如果您必须这样做,这是非常低级的工作,除非必须这样做,否则您应该避免这样做,然后使用类似offsetof的宏,而不是尝试自己手动指定偏移量。

offsetof告诉您在结构的内存分配中可以找到特定成员的位置

您定义的结构占用12个字节:

struct mystruct {
  char singlechar;
  char arraymember[10];
  char anotherchar;
};
字节0:singlechar 字节1:arraymember[0] 字节2:arraymember[1] 字节3:arraymember[2] ... 字节10:arraymember[9] 字节11:另一个字符 这就是您获得输出的原因:

offsetof(mystruct,singlechar) is 0
offsetof(mystruct,arraymember) is 1
offsetof(mystruct,anotherchar) is 11
如果分配该类型的对象,并在结构的开头获得一个字节*,则可以使用offsetof找出每个成员的位置。如果使用该指针偏移量,并将其转换回正确的类型,它将为您提供指向该成员的指针

mystruct s;
s.anotherchar = 5;
char* pBeginningOfS = (char*)(void*)(&s);
char* pAnotherChar = pBeginningOfS + offsetof(mystruct, anotherchar);
*pAnotherChar = 17;
printf("anotherchar is %d", s.anotherchar);
return 0;
这将 输出:


不能假定每个成员都是结构开头的特定偏移量的原因很复杂,并且依赖于编译器。如果你必须这样做,这是非常低级的事情,你应该避免,除非你必须这样做,然后使用像offsetof这样的宏,而不是试图自己手动指定偏移量。

你并不是真的告诉我们你不明白什么。你有一个例子。你了解什么?你想知道什么?工作补偿的原则keyword@davit-达图阿什维利:那不是很具体。你想知道它是怎么计算的吗?你能用它做什么?它为什么存在?offsetof是一个非常简单的宏,它背后没有什么大的原则。你并不是真的告诉我们你不明白的东西。你有一个例子。你了解什么?你想知道什么?工作补偿的原则keyword@davit-达图阿什维利:那不是很具体。你想知道它是怎么计算的吗?你能用它做什么?它为什么存在?OffStof是一个非常简单的宏,它背后没有大的原则。我们知道用C++来射击你自己的脚=你的整个腿的吹。我们知道用C++射击你自己的脚= =吹你的整个腿。没有要求它扩展到那个表达式,而较新的编译器可能会将其定义为一种特殊的形式,而GCC4使用的是uu内置的u offsetofst,例如m。您应该始终从中获得offsetof,而不是自己尝试定义它。不要求它扩展到该表达式,较新的编译器可能会将其定义为特殊形式,而GCC 4使用uuu内置的offsetofst,例如m。你应该总是摆脱它,而不是试图自己定义它。