Strange behavior on shrinking a Dynamic Array -- Capacity reduces to 0

d coder dlang.coder at gmail.com
Wed Jun 22 21:15:12 PDT 2011


>
>
> Yes. Capacity is only non-zero for arrays which can safely be extended. If
> you shrink an array from length 10 to length 5, the GC is smart enough to
> know that that there may be a dangling reference to elements [5..10] and
> thus if the length 5 array was appended to, it might stomp on someone else's
> data. Long ago, the GC was dumber and it caused a massive hole in the
> const/immutable system. If you know that no other aliases exist (i.e. you're
> buffering, etc) you can use the function assumeSafeAppend to reset the
> capacity. However, I'd strongly recommend switching to Appender whenever
> you're tempted to use assumeSafeAppend.
>

Thanks All

I would give Appender a try. Any idea if it is safe to use this struct in
multithreaded environment. Would it be OK to add elements to it from
multiple threads -- of course I would be using synchronized code to make
sure that only one element gets added at a time. Any other precautions?

Regards
- Puneet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110623/f10ce85c/attachment.html>


More information about the Digitalmars-d mailing list