www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24324] New: A default-initialized variable is not identical

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

          Issue ID: 24324
           Summary: A default-initialized variable is not identical to its
                    init value when it contains a default-initialized
                    member variable that is a dynamic array
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: issues.dlang jmdavisProg.com

struct S
{
    int[] arr = [1, 2, 3];
}

void main()
{
    S s1;
    S s2;

    assert(s1 is s2); // passes
    assert(s1 is S.init); //fails
}

For some reason, the member variable ends up pointing to a different block of
memory in the init value than it does in default-initialized values of the
struct type. The elements are identical in both the init value and the
default-initialized structs, but the arrays themselves are not identical.

For plenty of code, the difference won't matter, but it does create the bizarre
situation where a default-initialized struct is not identical to its init
value, and you lose the ability to check whether a value of that struct type
has been mutated, which you can normally do with all types with "value is
typeof(value).init".

--
Jan 07