digitalmars.D - DMD problem: incorrect struct.sizeof
- Jascha Wetzel (27/27) Jan 25 2007 Something's weird if you put a ubyte, byte oder char at the end of a
- Tomas Lindquist Olsen (27/55) Jan 25 2007 They are being padded to play nice with 32 bit alignment.
- Jascha Wetzel (2/3) 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>