Dynamic array of strings and appending a zero length array

H. S. Teoh hsteoh at qfbox.info
Sat Jul 8 20:01:08 UTC 2023


On Sat, Jul 08, 2023 at 05:15:26PM +0000, Cecil Ward via Digitalmars-d-learn wrote:
> I have a dynamic array of dstrings and I’m spending dstrings to it. At
> one point I need to append a zero-length string just to increase the
> length of the array by one but I can’t have a slot containing garbage.
> I thought about ++arr.length - would that work, while giving me valid
> contents to the final slot ?

Unlike C/C++, the D runtime always ensures that things are initialized
unless you explicitly tell it not to (via void-initialization). So
++arr.length will work; the new element will be initialized to
dstring.init (which is the empty string).


T

-- 
If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell


More information about the Digitalmars-d-learn mailing list