determine member offset in unions

  • Thread starter Christian Ehlscheid
  • Start date
C

Christian Ehlscheid

Hello,

i'm currently writing a C struct/union translator in Visual FoxPro

the problem i have is how to determine the offset of member's that are
contained in unions
e.g.

DEVMODE structure of wingdi.h

typedef struct _devicemode {
BCHAR dmDeviceName[CCHDEVICENAME];
WORD dmSpecVersion;
WORD dmDriverVersion;
WORD dmSize;
WORD dmDriverExtra;
DWORD dmFields;
union {
struct {
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
};
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
};

short dmColor;
short dmDuplex;
.....
union {
DWORD dmDisplayFlags;
DWORD dmNup;
}
...
}

a simple console app gives me the following output using: printf("Offset of
member = %d\n",offsetof(DEVMODE,member));

Offset of dmOrientation = 44 && seems to be the start boundary of the union
Offset of dmPosition = 44
Offset of dmDisplayOrientation = 52
Offset of dmDisplayFixedOutput = 56
Offset of dmDisplayFlags = 116
Offset of dmNup = 116

the question is now why are the members dmDisplayOrientation &
dmDisplayFixedOutput at offset 52 / 56 and not at offset 44 (the offset of
the union itself)?
or on what scheme (there must be one) does the the visual c compiler decide
at which offset to put union members?

Thanks in advance

Regards

Christian
 
T

tom_usenet

Hello,

i'm currently writing a C struct/union translator in Visual FoxPro

the problem i have is how to determine the offset of member's that are
contained in unions
e.g.

DEVMODE structure of wingdi.h

typedef struct _devicemode {
BCHAR dmDeviceName[CCHDEVICENAME];
WORD dmSpecVersion;
WORD dmDriverVersion;
WORD dmSize;
WORD dmDriverExtra;
DWORD dmFields;
union {
struct {
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
};

struct {
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
}

};

short dmColor;
short dmDuplex;
....
union {
DWORD dmDisplayFlags;
DWORD dmNup;
}
..
}

a simple console app gives me the following output using: printf("Offset of
member = %d\n",offsetof(DEVMODE,member));

Offset of dmOrientation = 44 && seems to be the start boundary of the union
Offset of dmPosition = 44
Offset of dmDisplayOrientation = 52
Offset of dmDisplayFixedOutput = 56
Offset of dmDisplayFlags = 116
Offset of dmNup = 116

Yes, that looks right.
the question is now why are the members dmDisplayOrientation &
dmDisplayFixedOutput at offset 52 / 56 and not at offset 44 (the offset of
the union itself)?

Because they are members of a struct, not the union. You've quoted the
header incorrectly.
or on what scheme (there must be one) does the the visual c compiler decide
at which offset to put union members?

All union members share the same address - this is guaranteed. e.g.

union u
{
type1 t1;
type2 t2;
//...
};

&u.t1 == &u.t2 == ... == &u.tN

Tom
 
C

Christian Ehlscheid

Hello,

ahh now i see .. i was totally confused .. i copied the struct definition
from the MSDN library help where it is outdated ..
(without the struct around the members)

Thanx

Christian

tom_usenet said:
Hello,

i'm currently writing a C struct/union translator in Visual FoxPro

the problem i have is how to determine the offset of member's that are
contained in unions
e.g.

DEVMODE structure of wingdi.h

typedef struct _devicemode {
BCHAR dmDeviceName[CCHDEVICENAME];
WORD dmSpecVersion;
WORD dmDriverVersion;
WORD dmSize;
WORD dmDriverExtra;
DWORD dmFields;
union {
struct {
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
};

struct {
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
}

};

short dmColor;
short dmDuplex;
....
union {
DWORD dmDisplayFlags;
DWORD dmNup;
}
..
}

a simple console app gives me the following output using: printf("Offset of
member = %d\n",offsetof(DEVMODE,member));

Offset of dmOrientation = 44 && seems to be the start boundary of the union
Offset of dmPosition = 44
Offset of dmDisplayOrientation = 52
Offset of dmDisplayFixedOutput = 56
Offset of dmDisplayFlags = 116
Offset of dmNup = 116

Yes, that looks right.
the question is now why are the members dmDisplayOrientation &
dmDisplayFixedOutput at offset 52 / 56 and not at offset 44 (the offset of
the union itself)?

Because they are members of a struct, not the union. You've quoted the
header incorrectly.
or on what scheme (there must be one) does the the visual c compiler decide
at which offset to put union members?

All union members share the same address - this is guaranteed. e.g.

union u
{
type1 t1;
type2 t2;
//...
};

&u.t1 == &u.t2 == ... == &u.tN

Tom
 

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

Top