Dynamic Arrays Capacity

Salih Dincer salihdb at hotmail.com
Thu Jun 2 05:04:03 UTC 2022


Hi,

Do I misunderstand? A dynamic array is allocated memory according 
to the `nextpow2()` algorithm(-1 lapse); strings, on the other 
hand, don't behave like this...

```d
   string str = "0123456789ABCDEF";
   char[] chr = str.dup;

   assert(str.length == 16);
   assert(str.capacity == 0);

   import std.math: thus = nextPow2; //.algebraic

   assert(chr.capacity == thus(str.length) - 1);
   assert(chr.capacity == 31);
```

Also, `.ptr` keeps the address of the most recent first element, 
right?


```d
   write("str[0]@", &str[0]);
   writeln(" == @", str.ptr);

   write("chr[0]@", &chr[0]);
   writeln(" == @", chr.ptr);
```

**Print Out:** (No Errors)
> str[0]@5607593901E0 == @5607593901E0
> chr[0]@7F9430982000 == @7F9430982000

SDB at 79


More information about the Digitalmars-d-learn mailing list