使用offsetof()获取sizeof struct元素? [英] Obtain sizeof struct element using offsetof()?

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

问题描述

有没有办法只根据结构中的

偏移量来获取struct元素的大小?我似乎无法找到一种方法来做到这一点

(没有比较每个元素的偏移与< offset>)。我想要做的就是创建一个这样的API:


#include< stddef.h>


struct MemMap

{

unsigned char apple; //我平台上的8位

unsigned char butter;

unsigned short pear; //我平台上的16位

无符号长桃; //我平台上的32位

};


静态结构MemMap memMap;


/ *<偏移>是MemMap结构元素的偏移量

**这个函数用< pVal>指向的适当的

**大小的值写入。

* /

int writeMemMapReg(size_t offset,void * pVal)

{

int failures = 0;

size_t size = / * sizeof元素位于< offset> * /;

unsigned char * p8Bits;

unsigned short * p16Bits;

unsigned long * p32Bits;


开关(大小)

{

案例1:

p8Bits =(unsigned char *)pVal;

/ *以某种方式将* p8Bits写入memMap struct

**< offset> ;.

* /

break;


案例2:

p16Bits =(无符号短*)pVal;

/ *以某种方式将* p16Bits写入

**< offset> ;.

* /

休息;


案例4:

p32Bits =(unsigned long *)pVal;

/ *以某种方式将* p32Bits写入memMap struct

**< offset>。 />
* /

休息;


默认值:

/ *错误* /

失败=!0;

休息;

}


返回失败;

}


任何帮助非常感谢。


-

- 马克 - >

-

Is there a way to obtain the size of a struct element based only upon its
offset within the struct? I seem unable to figure out a way to do this
(short of comparing every element''s offset with <offset>). What I would
like to do is create an API something like this:

#include <stddef.h>

struct MemMap
{
unsigned char apple; // 8 bits on my platform
unsigned char butter;
unsigned short pear; // 16 bits on my platform
unsigned long peach; // 32 bits on my platform
};

static struct MemMap memMap;

/* <offset> is the offset of the MemMap struct element
** that this function is to write with the appropriately
** sized value pointed to by <pVal>.
*/
int writeMemMapReg(size_t offset, void *pVal)
{
int failures = 0;
size_t size = /* sizeof element at <offset> */;
unsigned char *p8Bits;
unsigned short *p16Bits;
unsigned long *p32Bits;

switch (size)
{
case 1:
p8Bits = (unsigned char *) pVal;
/* somehow write *p8Bits to memMap struct at
** <offset>.
*/
break;

case 2:
p16Bits = (unsigned short *) pVal;
/* somehow write *p16Bits to memMap struct at
** <offset>.
*/
break;

case 4:
p32Bits = (unsigned long *) pVal;
/* somehow write *p32Bits to memMap struct at
** <offset>.
*/
break;

default:
/* Error */
failures = !0;
break;
}

return failures;
}

Any help much appreciated.

--
- Mark ->
--

推荐答案

Mark A. Odell写道:
Mark A. Odell wrote:
有没有办法只根据结构中的
偏移来获取struct元素的大小? [...]
Is there a way to obtain the size of a struct element based only upon its
offset within the struct? [...]




编号考虑


struct x {char cx; };

struct y {char cy [100]; };


这里,`offsetof(struct x,cx)== offsetof(struct y,cy)'',

但是`的大小` cx''和'cy''元素是不同的。

否(合法)计算仅基于关于输入值

零可以产生1和100作为输出。


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



No. Consider

struct x { char cx; };
struct y { char cy[100]; };

Here, `offsetof(struct x, cx) == offsetof(struct y, cy)'',
yet the sizes of the `cx'' and `cy'' elements are different.
No (legitimate) computation "based only" on the input value
zero could produce both 1 and 100 as outputs.

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


Eric Sosman< er ********* @ sun.com>写在

新闻:cj ********** @ news1brm.Central.Sun.COM:
Eric Sosman <er*********@sun.com> wrote in
news:cj**********@news1brm.Central.Sun.COM:
Mark A. Odell写道:
Mark A. Odell wrote:
有没有办法只根据结构中的偏移来获取struct元素的大小? [...]
Is there a way to obtain the size of a struct element based only upon
its offset within the struct? [...]



不。考虑

struct x {char cx;结构y {char cy [100];这里,'offsetof(struct x,cx)== offsetof(struct y,cy)'',
还有'cx'和'cy''的大小元素是不同的。
没有(合法的)计算仅基于关于输入值
零可以产生1和100作为输出。



No. Consider

struct x { char cx; };
struct y { char cy[100]; };

Here, `offsetof(struct x, cx) == offsetof(struct y, cy)'',
yet the sizes of the `cx'' and `cy'' elements are different.
No (legitimate) computation "based only" on the input value
zero could produce both 1 and 100 as outputs.




当然。谢谢Eric。如果我想简化调用者的

界面,我想我必须做一些关于结构的假设的宏观魔法。

。 />

问候。


-

- 马克 - >

-



Of course. Thanks Eric. I guess I''ll have to do some macro magic with some
built in assumptions about the struct if I want to simplify the caller''s
interface.

Regards.

--
- Mark ->
--


在< Xn ******************************** @ 130.133.1.4> Mark A. Odell < OD ******* @ hotmail.com>写道:
In <Xn********************************@130.133.1.4> "Mark A. Odell" <od*******@hotmail.com> writes:
有没有办法只根据结构中的
偏移量来获取struct元素的大小?


即使你也知道下一个成员的抵消将是不可能的,因为成员之间的填充。

我似乎无法找到一种方法来做到这一点
(没有比较每个元素的偏移与< offset>)。


这种方法没有错。

我想做的是创建这样的API:
Is there a way to obtain the size of a struct element based only upon its
offset within the struct?
Even if you also knew the offset of the next member that would be
impossible, due to the padding between members.
I seem unable to figure out a way to do this
(short of comparing every element''s offset with <offset>).
Nothing wrong with this approach.
What I would like to do is create an API something like this:




我无法想象任何实际需要。您还可以传递

类型的值,使用临时约定进行编码:调用者*必须*

知道它。被调用者现在拥有了解如何获取指向数据所需的所有信息。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de

目前在欧盟寻找工作



I can''t imagine any practical need for that. You can also pass the
type of the value, encoded using an ad hoc convention: the caller *must*
know it. The callee now has all the information needed to know how to
access the pointed data.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union


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

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