www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13947] New: Padding in empty structs is compared

https://issues.dlang.org/show_bug.cgi?id=13947

          Issue ID: 13947
           Summary: Padding in empty structs is compared
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: yebblies gmail.com
          Reporter: yebblies gmail.com

This assert fails, because despite being empty, code is emitted to memcmp the
structs.  While this example is clearly asking for it, this can happen
naturally when passing such structs around, as the padding does not have to be
preserved.

The simple solution is to make empty structs always compare as equal.

import core.stdc.string;

struct S
{
}

void main()
{
    static assert(S.sizeof == 1);
    S a;
    S b;
    memset(&a, 1, S.sizeof);
    memset(&b, 2, S.sizeof);
    assert(a == b);
}

--
Jan 07 2015