The Average Investor's Blog

A software developer view on the markets

Archive for the ‘C++’ Category

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;
}

Posted in C++ | Leave a Comment »

 
%d bloggers like this: