[Bug 185] New: typedefs not properly initialised when changing dynamic array length
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 8 08:14:14 PDT 2006
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 at digitalmars.com
ReportedBy: deewiant at 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.
--
More information about the Digitalmars-d-bugs
mailing list