www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - structure alignment issues?

reply akcom <CppCoder gmail.com> writes:
Translating the following from windows:

--- C Code:

#define IMAGE_SIZEOF_SHORT_NAME              8

typedef struct _IMAGE_SECTION_HEADER {
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union {
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     } Misc;
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;

#define IMAGE_SIZEOF_SECTION_HEADER          40

--- D Code:

const uint IMAGE_SIZEOF_SHORT_NAME = 8;
const uint IMAGE_SIZEOF_SECTION_HEADER = 40;

align struct ImageSectionHeader
{
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union Misc
	{
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     }
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
}
alias ImageSectionHeader IMAGE_SECTION_HEADER;
alias ImageSectionHeader *PIMAGE_SECTION_HEADER;

the problem is that ImageSectionHeader.sizeof == 36
Any ideas as to why?
May 31 2006
parent reply akcom <CppCoder gmail.com> writes:
akcom wrote:
 Translating the following from windows:
 
 --- C Code:
 
 #define IMAGE_SIZEOF_SHORT_NAME              8
 
 typedef struct _IMAGE_SECTION_HEADER {
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union {
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     } Misc;
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
 
 #define IMAGE_SIZEOF_SECTION_HEADER          40
 
 --- D Code:
 
 const uint IMAGE_SIZEOF_SHORT_NAME = 8;
 const uint IMAGE_SIZEOF_SECTION_HEADER = 40;
 
 align struct ImageSectionHeader
 {
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union Misc
     {
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     }
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
 }
 alias ImageSectionHeader IMAGE_SECTION_HEADER;
 alias ImageSectionHeader *PIMAGE_SECTION_HEADER;
 
 the problem is that ImageSectionHeader.sizeof == 36
 Any ideas as to why?
edit: it appears as if when the union is replaced by a single DWORD, everything works as expected. Should this be filed as a bug?
May 31 2006
parent "Regan Heath" <regan netwin.co.nz> writes:
On Wed, 31 May 2006 20:31:42 -0400, akcom <CppCoder gmail.com> wrote:
 akcom wrote:
 Translating the following from windows:
  --- C Code:
  #define IMAGE_SIZEOF_SHORT_NAME              8
  typedef struct _IMAGE_SECTION_HEADER {
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union {
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     } Misc;
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
  #define IMAGE_SIZEOF_SECTION_HEADER          40
  --- D Code:
  const uint IMAGE_SIZEOF_SHORT_NAME = 8;
 const uint IMAGE_SIZEOF_SECTION_HEADER = 40;
  align struct ImageSectionHeader
 {
     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
     union Misc
     {
             DWORD   PhysicalAddress;
             DWORD   VirtualSize;
     }
     DWORD   VirtualAddress;
     DWORD   SizeOfRawData;
     DWORD   PointerToRawData;
     DWORD   PointerToRelocations;
     DWORD   PointerToLinenumbers;
     WORD    NumberOfRelocations;
     WORD    NumberOfLinenumbers;
     DWORD   Characteristics;
 }
 alias ImageSectionHeader IMAGE_SECTION_HEADER;
 alias ImageSectionHeader *PIMAGE_SECTION_HEADER;
  the problem is that ImageSectionHeader.sizeof == 36
 Any ideas as to why?
edit: it appears as if when the union is replaced by a single DWORD, everything works as expected. Should this be filed as a bug?
No. The D struct you give above declares a type called "Misc" which is a union containing 2 DWORDS, but, you don't actually declare a struct member of that type. In short, your struct does not contain the union at all, only it's type declaration. You have 2 options: struct ImageSectionHeader { .. union { DWORD PhysicalAddress; DWORD VirtualSize; } .. } this is an anonymous union, of 2 DWORDS, or: struct ImageSectionHeader { .. union NAMED_BOB { //creates type called NAMED_BOB DWORD PhysicalAddress; DWORD VirtualSize; } NAMED_BOB Misc; //includes a NAMED_BOB in the struct, called 'Misc' .. } both of these give a sizeof 40 for me. Regan
May 31 2006