Setting array length without initializing/reallocating.

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Dec 12 01:46:51 UTC 2020


On Sat, Dec 12, 2020 at 12:53:09AM +0000, Jonathan Levi via Digitalmars-d wrote:
> Wow, there went several hours of debugging.
> 
> Increasing the length of a slice, by setting its length, will
> initialize the new elements and reallocate if necessary.
> 
> I did not realize length was "smart", I guess I should have guessed.
> 
> Anyway, to work around this, and probably also be more clear, create a
> new slice from the same pointer.
> 
> `array = array.ptr[0..newLength];`

I highly recommend reading the following article if you work with D
arrays in any non-trivial way:

	https://dlang.org/articles/d-array-article.html


T

-- 
Fact is stranger than fiction.


More information about the Digitalmars-d mailing list