determine member offset in unions

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Christian Ehlscheid (ehlscheid-no-spam_at_edv-ermtraud.de)
Date: 03/08/04


Date: Mon, 8 Mar 2004 13:04:35 +0100

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



Relevant Pages