Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Obtain sizeof struct element using offsetof()?

Reply
Thread Tools

Obtain sizeof struct element using offsetof()?

 
 
Mark A. Odell
Guest
Posts: n/a
 
      09-27-2004
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 ->
--
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-27-2004
Mark A. Odell wrote:
> Is there a way to obtain the size of a struct element based only upon its
> offset within the struct? [...]


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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      09-27-2004
Eric Sosman <(E-Mail Removed)> wrote in
news:cj9d6n$f6c$(E-Mail Removed):

> Mark A. Odell wrote:
>> Is there a way to obtain the size of a struct element based only upon
>> its offset within the struct? [...]

>
> 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.


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 ->
--
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-27-2004
In <Xns95716F8514343CopyrightMarkOdell@130.133.1.4> "Mark A. Odell" <(E-Mail Removed)> writes:

>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:


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: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-28-2004
Eric Sosman wrote:
> Mark A. Odell wrote:
>
>> 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]; };
>
> [ . . . ] No (legitimate) computation "based only" on the input value
> zero could produce both 1 and 100 as outputs.


That said, you can certainly calculate the size of each member of any
struct using only offsetof and sizeof:
sizeof(struct x) - offsetof(struct x, cx) == 1
sizeof(struct x) - offsetof(struct x, cy) == 100
It doesn't seem like you could gain much by doing this, though, unless
you're creating some kind of metaprogramming tool.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      09-28-2004
"Mark A. Odell" <(E-Mail Removed)> wrote in message
news:Xns95716F8514343CopyrightMarkOdell@130.133.1. 4...
> 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>
>

typedef unsigned char Apple_t;
typedef unsigned char Butter_t;
typedef unsigned short Pear_t;
typedef unsigned long Peach_t;

typedef struct mem_map
{
Apple_t apple;
Butter_t butter;
Pear_t pear;
Peach_t peach;
} MemMap;

typedef enum mem_map_offsets
{
MemMapApple = offsetof(MemMap,apple),
MemMapButter = offsetof(MemMap,butter),
MemMapPear = offsetof(MemMap,pear),
MemMapPeach = offsetof(MemMap,peach)
} MemMapOffsets;

static 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(MemMapOffsets offset, void *pVal)
> {
> int failures = 0;


switch(offset)
{
case MemMapApple:
memMap.apple = *(Apple_t *) pVal;
break;

case MemMapButter:
memMap.butter = *(Butter_t *) pVal;
break;

case MemMapPear:
memMap.pear = *(Pear_t *) pVal;
break;

case MemMapPeach:
memMap.peach = *(Peach_t *) pVal;
break;

default:
failures = 1;
}

>
> return failures;
> }
>
> Any help much appreciated.
>
> --
> - Mark ->
> --



 
Reply With Quote
 
j0mbolar
Guest
Posts: n/a
 
      09-28-2004
Derrick Coetzee <(E-Mail Removed)> wrote in message news:<cjaatr$ije$(E-Mail Removed)>...
> Eric Sosman wrote:
> > Mark A. Odell wrote:
> >
> >> 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]; };
> >
> > [ . . . ] No (legitimate) computation "based only" on the input value
> > zero could produce both 1 and 100 as outputs.

>
> That said, you can certainly calculate the size of each member of any
> struct using only offsetof and sizeof:
> sizeof(struct x) - offsetof(struct x, cx) == 1
> sizeof(struct x) - offsetof(struct x, cy) == 100
> It doesn't seem like you could gain much by doing this, though, unless
> you're creating some kind of metaprogramming tool.



This is incorrect. If you have the following:

struct foo {
char cx;
char cy[100];
};

then you can have padding after cy which is included in the size
of the actual struct. so sizeof(struct foo) does not give you the
size, in bytes, of the summation of all member sizes.
 
Reply With Quote
 
Ivan A. Kosarev
Guest
Posts: n/a
 
      09-28-2004

"j0mbolar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

> This is incorrect. If you have the following:
>
> struct foo {
> char cx;
> char cy[100];
> };
>
> then you can have padding after cy which is included in the size
> of the actual struct. so sizeof(struct foo) does not give you the


Practically, there are no chances to have any paddings with this code.

Instead, the following:

struct foo {
int i;
char c;
};

certainly will have the tail one.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-28-2004
In <cjaatr$ije$(E-Mail Removed)> Derrick Coetzee <(E-Mail Removed)> writes:

>Eric Sosman wrote:
>> Mark A. Odell wrote:
>>
>>> 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]; };
>>
>> [ . . . ] No (legitimate) computation "based only" on the input value
>> zero could produce both 1 and 100 as outputs.

>
>That said, you can certainly calculate the size of each member of any
>struct using only offsetof and sizeof:


Can you?

>sizeof(struct x) - offsetof(struct x, cx) == 1
>sizeof(struct x) - offsetof(struct x, cy) == 100


What about the padding the compiler can insert between any two members or
at the end?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
Derrick Coetzee
Guest
Posts: n/a
 
      09-28-2004
j0mbolar wrote:
>>That said, you can certainly calculate the size of each member of any
>>struct using only offsetof and sizeof:
>>sizeof(struct x) - offsetof(struct x, cx) == 1
>>sizeof(struct x) - offsetof(struct x, cy) == 100
>>It doesn't seem like you could gain much by doing this, though, unless
>>you're creating some kind of metaprogramming tool.

>
> This is incorrect. [ . . . ] you can have padding after cy which is included in the size
> of the actual struct.


I'm sorry, of course you're right. If you have an instance of the struct
handy, though, you can always use sizeof directly on the elements:

struct x {
int a;
char b;
};

int main() {
struct x y;
int c = sizeof(y.a);
int d = sizeof(y.b);
}

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[TinyCC] sizeof of element of struct returned by function Maciej Labanowicz C Programming 18 02-22-2013 09:50 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments