Sign in to follow this  

how to make this work in gcc-3.4.5

Recommended Posts

This is a well known trick of finding out some structure's header size, specially when the content of the structure might be changed by several programmers.

#define Offset(M,S)   ((&(((S*)1)->M)) - 1)

struct PacketData {
   PacketHeader Header;
   char*        pData;

enum HeaderSizes {
    PacketDataHeaderSize = Offset( pData, PacketData ),   

But apparently this trick doesn't work in >=GCC-3.4.5 anymore, cuz the compiler doesn't allow setting anything other than "constant literals" for enumerations. Hence, last night i had to do the nasty quick fix by making PacketDataHeaderSize a "const int" global variable. But is there any elegant way to do this in >=GCC-3.4.5 ?

Share this post

Link to post
Share on other sites
Have you tried searching the headers for the "offsetof" macro? It should be located in "stddef.h".

The C version is slightly different than yours:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

// and the C++ version more different still:

#define offsetof(TYPE, MEMBER)
(__offsetof__ (reinterpret_cast <size_t>
(&reinterpret_cast <const volatile char &>
(static_cast<TYPE *> (0)->MEMBER))))

That macro formatting might not be preserved.

At any rate, the problem isn't likely to be the macro but how you're using it with the enum.

Share this post

Link to post
Share on other sites
goodness me !! that CPP version is kinda complicated !! hehehe... well i was compiling
Torque Engine code in Gentoo (gcc-3.4.5).. and it wouldn't compile the following

#ifndef Offset
#if defined(TORQUE_COMPILER_GCC) && (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))
#define Offset(m,T) ((int)(&((T *)1)->m) - 1)
#define Offset(x, cls) ((dsize_t)((const char *)&(((cls *)0)->x)-(const char *)0))

enum HeaderSizes
/// Byte offset to payload of a PacketReceiveEvent
PacketReceiveEventHeaderSize = Offset(data,PacketReceiveEvent),

/// Byte offset to payload of a ConnectedReceiveEvent
ConnectedReceiveEventHeaderSize = Offset(data,ConnectedReceiveEvent),

/// Byte offset to payload of a ConsoleEvent
ConsoleEventHeaderSize = Offset(data,ConsoleEvent)

As you can see the Torque guys wrote their own Offset macro. Later on i had to declare those
enums as Global constant integer variable. Anyway, the engine gives seg fault in gentoo.. i have no idea why... i remember it ran well in my old mandrake-10 box.

But i really dint know that offsetof macros was already defined in stddef.h !! just wondering why did the Torque guys re-wrote it then !!??

Share this post

Link to post
Share on other sites
Sign in to follow this