[Issue 6153] Inserting to An Array!T inside an Array!(Array!T) causes a segfault.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 18 06:46:47 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=6153


Christophe Travert <christophe.travert at normalesup.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |christophe.travert at normales
                   |                            |up.org


--- Comment #4 from Christophe Travert <christophe.travert at normalesup.org> 2012-07-18 06:46:14 PDT ---
(In reply to comment #3)
> The reason for the failure is that for
> 
> arr2D[0].insert(1);
> 
> the opIndex() method is invoked which returns by value. In theory this is okay
> since the array is stored by reference and the returned value contains a
> reference to the payload data of the inner array.
> 
> What happens here is that arr2D[0] is uninitialized (the pointer to the
> RefCounted object is null) and this guy is returned. It is then changed (the
> insert method initializes the temporary and creates the ref-counted object with
> the inserted content).
> 
> One way to resolve this is to make opIndex return a reference. Or is there an
> alternative?

I think both should be corrected:
 - uninitialised Arrays do not behave as reference values: you can copy them,
change the copy, and this does not affect the original. This breaks the
intended 'reference value' behavior. The cost of creating a payload for each
empty Array may be compensated by not having to check the payload is
initialised in each of Array's methods.
 - opIndex should return a reference. Any struct with a non-const method will
suffer the same problem when 'array[i].method' is called: the method is called
on a copy of the contained data.

Note that Array!bool cannot return a reference to a bool, and can't be
corrected the same way. I think this is not a problem, but I may miss
something.

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


More information about the Digitalmars-d-bugs mailing list