Skip navigation

See I wasn’t aware of offsetof.

Its really good.

Its defined as:

#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>(((s *)0)->m)

So when you do:

offsetof( STRUCT_TYPE, MEMBER_NAME )

You really get:

(size_t)&(char&)(((STRUCT_TYPE *)0)->MEMBER_NAME)

So its pretty neat. Basically what happens is, you get the number of bytes that MEMBER_NAME is from the beginning of the struct. .. refers to the address of member m in this object. Since the start address of this object is 0 (NULL), the address of member m is exactly the offset.

PRETTY cool. I can’t believe I’ve been listing numerical byte counts for this long..

and the hand of experience.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: