www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Bug 185] New: typedefs not properly initialised when changing dynamic array length

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/bugzilla/show_bug.cgi?id=185

           Summary: typedefs not properly initialised when changing dynamic
                    array length
           Product: D
           Version: 0.160
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: deewiant gmail.com


import std.math;
void main() {
        typedef double type = 5.0;

        type t;
        assert (t == type.init);

        type[] ts = new type[10];
        foreach (i; ts)
                assert (i == type.init);

        ts.length = 20;
        foreach (i; ts)
                assert (i == type.init);
                //assert (i == type.init || isnan(i));
}

--

Half the assertions in the last loop of the above code fail.

This is because, when setting the length, DMD does not set the new myints to
their typedef's initialiser, 7 - it sets them to 0. Note the commented-out
assertion statement (imagine it reads "i == cast(type)double.init", which
wouldn't work since you can't compare nans that way) - even worse, it would
fail as well!

This contradicts the spec, which states the following at
http://www.digitalmars.com/d/arrays.html under the heading "Setting Dynamic
Array Length": "If the new array length is longer, the remainder is filled out
with the default initializer." Even if this referred to the base type's (as
opposed to the typedef's) default initialiser - which is doubtful -, DMD's
behaviour would be wrong.

This bug is quite annoying and potentially tricky to track down. It can
fortunately be easily worked around, either by manually setting the new
elements to type.init, or by something like the following:

if (ts.length > 20)
        ts.length = 20;
else if (ts.length < 20)
        ts ~= new type[20 - type.length];

But this way is particularly ugly, and the other is an annoyance as well,
especially if you have code which modifies a typedef-array's length often and
you suddenly run into this bug, causing you to have to add at least two lines
to every such point in the code.


-- 
Jun 08 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

d-bugmail puremagic.com schrieb am 2006-06-08:
 http://d.puremagic.com/bugzilla/show_bug.cgi?id=185

            Summary: typedefs not properly initialised when changing dynamic
                     array length

test cases: http://dstress.kuehne.cn/run/a/array_initialization_20_A.d http://dstress.kuehne.cn/run/a/array_initialization_20_B.d http://dstress.kuehne.cn/run/a/array_initialization_20_C.d http://dstress.kuehne.cn/run/a/array_initialization_20_D.d http://dstress.kuehne.cn/run/a/array_initialization_20_E.d http://dstress.kuehne.cn/run/a/array_initialization_20_F.d http://dstress.kuehne.cn/run/a/array_initialization_20_G.d http://dstress.kuehne.cn/run/a/array_initialization_20_H.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFEiYzC3w+/yD4P9tIRAsP3AJ4x3RZRqwB9xO9PUfmDATue91OyLACfXDmd X8QLusH5k2fiDK1mkAFDCMM= =9eXb -----END PGP SIGNATURE-----
Jun 09 2006