digitalmars.D - DMD problem: incorrect struct.sizeof
- Jascha Wetzel <"[firstname]" mainia.de> Jan 25 2007
- "Tomas Lindquist Olsen" <tomas famolsen.dk> Jan 25 2007
- Jascha Wetzel <"[firstname]" mainia.de> Jan 25 2007
Something's weird if you put a ubyte, byte oder char at the end of a
struct. The program below prints
str1.sizeof: 4
str2.sizeof: 8
str3.sizeof: 12
Reproducable with DMD 1.0, 1.001 and 1.002 on win32 - I didn't check
older versions.
import std.stdio;
struct str1 {
ushort a;
ubyte b;
}
struct str2 {
uint a;
ubyte b;
}
struct str3 {
ushort a;
uint c;
ubyte b;
}
void main()
{
writefln("str1.sizeof: %d", str1.sizeof);
writefln("str2.sizeof: %d", str2.sizeof);
writefln("str3.sizeof: %d", str3.sizeof);
}
Jan 25 2007
Jascha Wetzel wrote:Something's weird if you put a ubyte, byte oder char at the end of a struct. The program below prints str1.sizeof: 4 str2.sizeof: 8 str3.sizeof: 12 Reproducable with DMD 1.0, 1.001 and 1.002 on win32 - I didn't check older versions. import std.stdio; struct str1 { ushort a; ubyte b; } struct str2 { uint a; ubyte b; } struct str3 { ushort a; uint c; ubyte b; } void main() { writefln("str1.sizeof: %d", str1.sizeof); writefln("str2.sizeof: %d", str2.sizeof); writefln("str3.sizeof: %d", str3.sizeof); }
They are being padded to play nice with 32 bit alignment. Try: import std.stdio; struct str1 { align(1): ushort a; ubyte b; } struct str2 { align(1): uint a; ubyte b; } struct str3 { align(1): ushort a; uint c; ubyte b; } void main() { writefln("str1.sizeof: %d", str1.sizeof); writefln("str2.sizeof: %d", str2.sizeof); writefln("str3.sizeof: %d", str3.sizeof); } --
Jan 25 2007
oh, duh, sorry for that... Tomas Lindquist Olsen wrote:They are being padded to play nice with 32 bit alignment.
Jan 25 2007








Jascha Wetzel <"[firstname]" mainia.de>