D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4378 - Array Literals as Default Field Initializers Shared Across Instances.
Summary: Array Literals as Default Field Initializers Shared Across Instances.
Status: RESOLVED DUPLICATE of issue 2947
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-23 17:04 UTC by David Simcha
Modified: 2012-02-01 19:29 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2010-06-23 17:04:12 UTC
import std.stdio;

struct Foo {
    int[] bar = [1, 2, 3, 4, 5];
}

void main() {
    Foo foo;
    foo.bar[0] = 3;

    Foo foo2;
    writeln(foo2.bar);  // 3 2 3 2 5
}
Comment 1 Jonathan M Davis 2010-06-23 17:17:27 UTC
I think that you mean

writeln(foo2.bar);  // 3 2 3 4 5

but regardless, it's definitely a bug.
Comment 2 David Simcha 2010-06-23 17:45:44 UTC
Yeah, that's just a typo.
Comment 3 David Simcha 2010-06-23 17:46:59 UTC
Forgot to mention, this also applies to classes, not just structs.
Comment 4 nfxjfg 2010-06-24 05:32:46 UTC
How is this supposed to work "correctly"? Hidden allocation of an array on the heap every time a struct of that type is "constructed"? Disallow initializing non-immutable, non-static array members?

This is not a bug, it's just D _works_. If you assign an array to a global variable, of course it's shared by everyone. Dynamic arrays are reference types. The "variable" being a struct initializer doesn't change this.

I think this really should be marked as INVALID.

(I'm sure it's somewhat confusing for programmers coming from C++ or Java. Especially in Java, the array would be reconstructed every time the constructor is called. Stuff like this _never_ happens in D: structs are always initialized by what boils down to a memcpy.)
Comment 5 Jonathan M Davis 2010-06-24 10:07:27 UTC
bar is a member varible _not_ a class variable and should _not_ be global. As such, two independently constructed variables of type Foo should have bars which referenc totally separate arrays. If bar were static, then they'd be shared, but it's not.

Now, if the second Foo were constructed by copying the first one, then sure, that's going to result in both bars referencing the same array, and setting an element of one will affect the other. However, in this case, both variables of type Foo were constructed independently, so their bars should not point to the same array.
Comment 6 yebblies 2010-06-25 01:24:21 UTC
I can't get dmd to find core.thread atm for some reason, but does the same thing happen with tls globals?

eg.

import core.thread;
import std.stdio;

int[] x = [1, 2, 3];

void main()
{
    writeln(x);
    x[0] = 9;
    writeln(x);
    new Thread(&thread2);
}

void thread2()
{
    writeln(x);
}
Comment 7 yebblies 2012-02-01 19:29:15 UTC

*** This issue has been marked as a duplicate of issue 2947 ***