weird behave of Array?

dmm dmm at test.com
Sun Jul 28 17:21:25 UTC 2019


import std.stdio : writefln;

void test(string str)
{
   writefln("%d, %d", str.length, str.capacity);
   str.length = 10;
   writefln("%d, %d", str.length, str.capacity);
}

void main()
{
   string str;
   str.reserve(1024);
   //str.length = str.capacity;
   writefln("%d, %d", str.length, str.capacity);
   test(str);
   writefln("%d, %d", str.length, str.capacity);
}

The output is:

0, 1358
0, 1358
10, 1358
0, 0

If I uncomment the line str.length = str.capacity;

then the output is:

1358, 1358
1358, 1358
10, 0
1358, 1358

why?


More information about the Digitalmars-d-learn mailing list