bug or feature?
Derek Parnell
derek at psych.ward
Tue Dec 5 07:27:32 PST 2006
On Tue, 05 Dec 2006 15:56:25 +0200, bobef wrote:
>
> //outputs "asdf as asdf"
That is because setting the length to zero does not deallocate the RM for
the array any more. What you are getting is three slices of 'a' in 'b'.
Here is your code with this shown ...
import std.stdio;
import std.string;
void main()
{
char[] a;
char[][] b;
a~="1234";
writefln("%s[%s]", a.ptr, a.length);
b~=a;
a.length = 0;
a~="&&";
writefln("%s[%s]", a.ptr, a.length);
b~=a;
a.length = 0;
a~="asdf";
writefln("%s[%s]", a.ptr, a.length);
b~=a;
writefln(join(b," "));
}
Outputs:
870FE0[4]
870FE0[2]
870FE0[4]
asdf as asdf
> //if we (replace a~="..." with a="...") || (replace a.length=0 with
> a=null) || (replace b~=a with b~=a.dup) then it outputs "1234 && asdf"
The first two ensure that the array is reallocated on each assignment. The
last option explcitly takes a copy of the slice into 'b'.
I'm not sure what you were hoping to achieve with this type of coding. The
simple solution I think is the first alternative that you give.
import std.stdio;
import std.string;
void main()
{
char[] a;
char[][] b;
a="1234";
b~=a;
a="&&";
b~=a;
a="asdf";
b~=a;
writefln(join(b," "));
}
> //i don't know if this is bug or new feature since last time i used d
Definitely a feature. If your code was trying to avoid multiple allocation
for the array, here is one way that can be done now ...
import std.stdio;
import std.string;
void main()
{
char[] a;
char[][] b;
int i;
// Allocate an initial buffer size
a.length = 200;
writefln("%s[%s]", a.ptr, a.length);
// Reset the length but keep the allocation
a.length = 0;
i = a.length;
a~="1234";
writefln("%s[%s]", a.ptr, a.length);
b~=a[i..$];
i = a.length;
a~="&&";
writefln("%s[%s]", a.ptr, a.length);
b~=a[i..$];
i = a.length;
a~="asdf";
writefln("%s[%s]", a.ptr, a.length);
b~=a[i..$];
writefln(join(b," "));
}
--
Derek Parnell
More information about the Digitalmars-d-bugs
mailing list