The Average Investor's Blog

A software developer view on the markets

A C++ macro to find offsets of structure members

Posted by The Average Investor on Sep 23, 2011

A few large structures dumped in hex, how does one find the offset of a member to look up the corresponding value in the hex dump? That’s what I was dealing with recently. Came up with the following neat macro:

#include <iostream>

// Just for the example
#include <stdio.h>

using namespace std;

#define STRUCT_OFFSETOF( ss, mm )   \
   cout << #ss <<                   \
   "::" <<                          \
   #mm <<                           \
   ": " <<                          \
   dec <<                           \
   offsetof( ss, mm ) <<            \
   " (0x" <<                        \
   hex <<                           \
   offsetof( ss, mm ) <<            \
   ")" <<                           \
   endl;

int
main( )
{
   STRUCT_OFFSETOF( FILE, _base );
   return 0;
}
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: