how best to get the offset of a field within a struct?

T

tmp123

Keith said:
foo is a static pointer object; it's therefore initialized to NULL.
(Since it's not const, you could mess it up by assigning a value to
foo.)

Both are similar to the common definition of offset(), except that
they use foo rather than a literal 0. Both dereference a null pointer

Sorry, I've not been clear enough. Please, see at init part of the main
function (I repeat it because it has been snipped):

/* init */
int i;
foo=malloc(256);
for(i=0;i<256;i++) ((char *)foo)=i;

Moreover, method 2 doesn't uses pointer substraction.

Kind regards.
 
K

Keith Thompson

tmp123 said:
Keith said:
foo is a static pointer object; it's therefore initialized to NULL.
(Since it's not const, you could mess it up by assigning a value to
foo.)

Both are similar to the common definition of offset(), except that
they use foo rather than a literal 0. Both dereference a null pointer

Sorry, I've not been clear enough. Please, see at init part of the main
function (I repeat it because it has been snipped):

/* init */
int i;
foo=malloc(256);
for(i=0;i<256;i++) ((char *)foo)=i;

Moreover, method 2 doesn't uses pointer substraction.


Sorry, I didn't read carefully enough.

Both offsetof1 and offsetof2 depend on the existence of an object
"foo" (used to provide a valid address, where the usual implementation
uses a null pointer). offsetof1 invokes undefined behavior if the
offset is greater than 255 bytes; offset2 depends on the stored value,
and won't work at all for offsets greater than 255. For offsetof1 to
work in the general case, you'd need foo to be as large as the largest
possible struct; offsetof2 works only with 1-byte array elements (and
even then you'd want unsigned char rather than char).

I think offsetof1 is probably about as close as you can get to a
portable implementation, but every compiler supports a non-portable
implementation that doesn't have the same drawbacks.
 
F

funkyj

The suspicion that I was doing something fishy was what let me to post
my question. Your summary above touches on the key points.

The C standard authors put offsetof() in the standard library so that
the language need not provide a portable way of doing this by hand.
This language design choice provides the user with the needed
functionality (i.e. offsetof()) while giving the compiler writer more
latitude.

Thanks!
--jfc
 
K

Keith Thompson

funkyj said:
The suspicion that I was doing something fishy was what let me to post
my question. Your summary above touches on the key points.

What summary? There's nothing "above", and the article to which you
replied isn't available on my news server.

Please please *please* read said:
The C standard authors put offsetof() in the standard library so that
the language need not provide a portable way of doing this by hand.
This language design choice provides the user with the needed
functionality (i.e. offsetof()) while giving the compiler writer more
latitude.

Correct.
 
D

Dave Thompson

tedu wrote:

later corrected to (char*)&tmp_.field - (char*)&tmp_
struct tmp_ {int a; int b;} tmp_
size_t o;
if (flag)
almostoffsetof(tmp_, b, o);
else
almostoffsetof(tmp_, a, o);

Should show you a couple of potential problems, one of which has a
standard solution the other is rather harder to avoid.

That won't compile; first you're missing a semicolon, then (in C) tmp_
is not a typename as required by both his almostoffsetof() and
standard offsetof(). You could either use struct tmp_ for the
typename, or change the struct declaration to a typedef, and either
way I see no problem.

- David.Thompson1 at worldnet.att.net
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top