Bug 1493 – Problem with dynamic array

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-09-11T05:12:00Z
Last change time
2014-02-17T22:51:19Z
Assigned to
bugzilla
Creator
keystuffs

Comments

Comment #0 by keystuffs — 2007-09-11T05:12:26Z
The dynamic array seems to be shared with all instances of a class: class A { int array1[] = [0]; // dynamic array int array2[1] = [0]; // static array } int main(char[][] p) { A a1 = new A(); A a2 = new A(); a1.array1[0] = 10; std.stdio.writefln(a1.array1[0]); // write 10: OK std.stdio.writefln(a2.array1[0]); // write 10: should be 0 a1.array2[0] = 20; std.stdio.writefln(a1.array2[0]); // write 20: OK std.stdio.writefln(a2.array2[0]); // write 0: OK return 0; } So I think that the variable array1 is shared with the objects a1 and a2.
Comment #1 by default_357-line — 2007-09-11T05:23:53Z
This isn't a bug. An array, behind the scenes, is nothing but a pointer and a length. So the dynamic array in your class gets initialized with a specific pointer and length, which is the same for all instances because the _literal_ is the same. The correct thing to do here is initialize the dynamic array in the constructor, for example, by dup-ing it :) --downs