Structs and Alignment

  • Thread starter David Rasmussen
  • Start date
D

Derrick Coetzee

David said:
struct foo
{
char bar;
long baz;
};

How can I know exactly how many bytes into struct foo baz is?

This is what the offsetof macro, defined in <stddef.h>, is for (ref C99
7.17.3). However, if your program's behaviour relies on this value, it
is fundamentally nonportable, even between different versions of the
same compiler. Don't use this for loading/saving, in particular.
 
X

xarax

Derrick Coetzee said:
This is what the offsetof macro, defined in <stddef.h>, is for (ref C99
7.17.3). However, if your program's behaviour relies on this value, it
is fundamentally nonportable, even between different versions of the
same compiler. Don't use this for loading/saving, in particular.

<confused>
I had to read that a few times. At first, I thought you
were saying that relying on offsetof() makes your program
non-portable. Now, I think you mean that hard-coding a
numeric value, say 4, and comparing it to the result of
offset() is non-portable.
</confused>
 
O

Old Wolf

Keith said:
Apart from the fact that the alternative won't work (&f.baz and &f are
of incompatible types), why do you need an alternative when the
language provides offsetof() for exactly this purpose?

Some compilers, although 99% compliant to ANSI C, don't
provide offsetof().
 
B

Ben Pfaff

Old Wolf said:
Some compilers, although 99% compliant to ANSI C, don't
provide offsetof().

I'd prefer to work around the problem if faced by such a
situation:

#include <stddef.h>

#ifndef offsetof
/* From the C FAQ. Normally works. */
#define offsetof(type, mem) ((size_t) \
((char *)&((type *)0)->mem - (char *)(type *)0))
#endif
 
D

Derrick Coetzee

xarax said:
Derrick Coetzee said:
This is what the offsetof macro, defined in <stddef.h>, is for (ref C99
7.17.3). However, if your program's behaviour relies on this value, it
is fundamentally nonportable [ . . . ]

<confused>
I had to read that a few times. At first, I thought you
were saying that relying on offsetof() makes your program
non-portable. Now, I think you mean that hard-coding a
numeric value, say 4, and comparing it to the result of
offset() is non-portable.
</confused>

Sorry, my language was unclear. More specifically, the assumption that
it either has a particular value or that the value is the same on two
different platforms is what would be in error, since it's
implementation-defined. I suppose that goes without saying, but it's not
unlike the common error of fwrite'ing a struct to disk and fread'ing it
back in on another platform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top