Array expanding

bearophile bearophileHUGS at lycos.com
Sun Dec 18 15:14:40 PST 2011


RenatoL Wrote:

> Yes "in pratice" we can make in many different ways but "theoretically
> speaking" it seems (to me) a very bug prone behavior.... not nice.

See also this program:

import std.stdio;
void main() {
    auto a = [87, 40, 10, 2];
    a.length -= 1;
    auto b = a; // Now a and b refer to the same chunk
    a.assumeSafeAppend();
    a ~= 5; // Append to a
    writeln(a, " ", b);
    a[0] = 15; // Modify a[0]
    writeln(a, " ", b);
}


Without assumeSafeAppend():
[87, 40, 10, 5] [87, 40, 10]
[15, 40, 10, 5] [87, 40, 10]

With assumeSafeAppend():
[87, 40, 10, 5] [87, 40, 10]
[15, 40, 10, 5] [15, 40, 10]

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list