DMD problem: incorrect struct.sizeof

Tomas Lindquist Olsen tomas at famolsen.dk
Thu Jan 25 12:37:49 PST 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);
}

-- 




More information about the Digitalmars-d mailing list