Thread overview
[Issue 8355] New: struct's sizeof has bug
Jul 07, 2012
soarowl@yeah.net
Jul 07, 2012
Nils
Jul 07, 2012
Walter Bright
Jul 07, 2012
Walter Bright
July 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8355

           Summary: struct's sizeof has bug
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: soarowl@yeah.net


--- Comment #0 from soarowl@yeah.net 2012-07-07 06:37:32 PDT ---
The following code extract from https://github.com/SiegeLord/Tango-D2.git

tango\util\compress\Zip.d
---------------------------------------------------------------------
import
    std.stdio;

align(1)
{
    struct LocalFileHeaderData
    {
        ushort      extract_version = ushort.max;
        ushort      general_flags = 0;
        ushort      compression_method = 0;
        ushort      modification_file_time = 0;
        ushort      modification_file_date = 0;
        uint        crc_32 = 0; // offsetof = 10
        uint        compressed_size = 0;
        uint        uncompressed_size = 0;
        ushort      file_name_length = 0;
        ushort      extra_field_length = 0;
    }
}

void main()
{
    writeln(LocalFileHeaderData.sizeof);
}
---------------------------------------------------------

The output should be 26 instread of 28.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8355


Nils <mailme+d@nilsb.dyndns.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mailme+d@nilsb.dyndns.org
           Severity|normal                      |regression


--- Comment #1 from Nils <mailme+d@nilsb.dyndns.org> 2012-07-07 12:54:29 PDT ---
printed "26" before https://github.com/D-Programming-Language/dmd/commit/154e44a006270d53745f99ec3e538a0ce526ae76

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8355


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |INVALID


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-07-07 13:21:52 PDT ---
If you're using head, you'll need to put the align(1) inside the struct declaration, rather than outside. The layout is now only affected by align attributes that are inside the declaration. Align attributes outside affect how the struct is aligned where the struct instance is placed.

I.e. this is an intentional change.

The previous behavior was broken in some respects, and was incompatible with doing things like having 256 byte alignment. Furthermore, the specific behavior of putting align outside and having it affect the inside was a bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8355


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #3 from bearophile_hugs@eml.cc 2012-07-07 13:58:13 PDT ---
(In reply to comment #2)
> If you're using head, you'll need to put the align(1) inside the struct declaration, rather than outside. The layout is now only affected by align attributes that are inside the declaration.

What's the right syntax? This prints three times 28:


struct Foo1 {
    align(1):
        ushort a,b,c,d,e;
        uint f, g, h;
        ushort i, j;
}
pragma(msg, Foo1.sizeof);

struct Foo2 {
    align(1) {
        ushort a,b,c,d,e;
        uint f, g, h;
        ushort i, j;
    }
}
pragma(msg, Foo2.sizeof);

struct Foo3 {
    align(1) ushort a,b,c,d,e;
    align(1) uint f, g, h;
    align(1) ushort i, j;
}
pragma(msg, Foo3.sizeof);

void main() {}



While this prints 26:

align(1) struct Foo4 {
    align(1):
        ushort a,b,c,d,e;
        uint f, g, h;
        ushort i, j;
}
pragma(msg, Foo4.sizeof);
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8355



--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2012-07-07 16:11:40 PDT ---
align inside sets the alignment of the fields. align outside sets the alignment of the instance as a single block.

So, you'll need both to get an unaligned size.

For example, consider an array of S. It should be an even multiple of the size of S. So consider how alignment must play into that, and the behavior becomes inevitable.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------