简体   繁体   English

C 结构中的跳过偏移量

[英]Skip offset in C struct

When I write a struct I want that specific value will be into specific offset当我编写一个结构时,我希望该特定值将成为特定偏移量

struct test

{
   int dummy1[100];//dummy
   int id_s ;
   int dummy2[5];//dummy
   int id_t;

}

What I want from this struct only 2 members: id_s and id_t , and I want that id_s will be in offset of 400 (100 * 4) and id_t will be in offset of (100 * 4 +4 + 5*4)我想要从这个结构中得到的只有 2 个成员: id_sid_t ,我希望id_s的偏移量为 400 (100 * 4) 而id_t的偏移量为 (100 * 4 +4 + 5*4)

Is there any simple way to do that instead put some dummy fields?有什么简单的方法可以代替放置一些虚拟字段吗?

Is there any simple way to do that instead put some dummy fields?有什么简单的方法可以代替放置一些虚拟字段吗?

As a general portable solution: No作为通用便携式解决方案:否

The C standard does not require the type int to be 4 bytes. C 标准不要求int类型为 4 个字节。 Further, the C standard allows for padding in structs so that may also change layout.此外,C 标准允许在结构中进行填充,以便也可以更改布局。 So you can't just write a struct like yours and rely on the offsets being what you want on all systems.因此,您不能只编写像您这样的结构并依赖偏移量是您在所有系统上想要的。

But for most systems it will work as you expect.但对于大多数系统,它会按您的预期工作。

I would however do two changes.然而,我会做两个改变。

  1. Use char for dummies.使用char做假人。

Like:喜欢:

struct test
{
   char dummy1[400];//dummy
   int id_s ;
   char dummy2[20];//dummy
   int id_t;

};
  1. Make the struct packed.使结构打包。

Like:喜欢:

struct test
{
   char dummy1[400];//dummy
   int id_s ;
   char dummy2[20];//dummy
   int id_t;

} __attribute__((__packed__));

As you can't be 100% sure that it work, I would also add asserts (static) that veirifies that the layout is as expected.由于您不能 100% 确定它是否有效,我还将添加断言(静态)来验证布局是否符合预期。

Like:喜欢:

_Static_assert(offsetof(struct test, id_s) == 400, "Illegal offset for id_s in struct test");
_Static_assert(offsetof(struct test, id_t) == 424, "Illegal offset for id_t in struct test");

To take the integer size in to account and still get the offsets 400 and 424, you could do:要考虑 integer 大小并仍然获得偏移量 400 和 424,您可以执行以下操作:

struct test
{
   char dummy1[400];//dummy
   int id_s ;
   char dummy2[24 - sizeof(int)];//dummy
   int id_t;

} __attribute__((__packed__));

Is there any simple way to do that instead put some dummy fields?有什么简单的方法可以代替放置一些虚拟字段吗?

Write your own accessors.编写自己的访问器。

What I want from this struct only 2 members: id_s and id_t, and I want that id_s will be in offset of 400 (100 * 4) and id_t will be in offset of (100 * 4 +4 + 5*4)我想要从这个结构中得到的只有 2 个成员:id_s 和 id_t,我希望 id_s 的偏移量为 400 (100 * 4),而 id_t 的偏移量为 (100 * 4 +4 + 5*4)

Place them at specific byte-offsets yourself:自己将它们放置在特定的字节偏移处:

#define TEST_ID_S_OFFSET  (100*4)
// 100*4 +4 ? Why +4?
#define TEST_ID_T_OFFSET  (TEST_ID_S_OFFSET + sizeof(int) + 5*4)
struct test {
   char data[TEST_ID_T_OFFSET + sizeof(int)];
};

int test_get_id_s(struct test *t) {
      int ret;
      memcpy(&ret, &t->data[TEST_ID_S_OFFSET], sizeof(ret));
      return ret;
}
void test_set_id_s(struct test *t, int val) {
      memcpy(&t->data[TEST_ID_S_OFFSET], &val, sizeof(val));
}
int test_get_id_t(struct test *t) {
      int ret;
      memcpy(&ret, &t->data[TEST_ID_T_OFFSET], sizeof(ret));
      return ret;
}
void test_set_id_s(struct test *t, int val) {
      memcpy(&t->data[TEST_ID_T_OFFSET], &val, sizeof(val));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM