Is removing elements of AA in foreach loop safe?
H. S. Teoh
hsteoh at quickfur.ath.cx
Fri Aug 30 18:03:09 UTC 2019
On Fri, Aug 30, 2019 at 04:45:20PM +0000, berni via Digitalmars-d-learn wrote:
> On Friday, 30 August 2019 at 15:00:59 UTC, Paul Backus wrote:
> > Whether you actually get an error at runtime depends on the load
> > factor of the AA. If it drops below a certain threshold, the AA will
> > be resized [1], and its original memory will be freed [2].
>
> It could still work, depending on how the foreach loop is implemented.
> If the keys were stored away before starting the loop it would work.
> But for one thing, it isn't implemented that way and for the other,
> one shouldn't rely on it, because the implementation could change.
> What I hoped for, was, that the specs enforce somewhere, that this is
> to be implemented in a safe manner.
>
> I'll replace this loops by something better, e.g. the mentioned
> filter. But I've never worked with AAs and filters yet. Will see, if I
> manage to do that. Else I'll probably just copy the keys and use them
> for an independent loop.
In general, modifying a container (of any kind) while iterating over it
is a bad idea, because it leads to corner cases with counter-intuitive
semantics. In some cases, it can be made to work if the container
supports deletion of the *current* element being iterated over. But this
requires support from the container.
General insertion/deletion during iteration over a container, generally
speaking, leads to corner cases with "strange" behaviour. The problem is
that iteration order becomes non-obvious once arbitrary changes can
happen during iteration. If you're iterating over elements E1, E2, E3,
etc., and then somebody inserts a new element E, should the current
iteration include E or not? In an unordered container like an AA, this
becomes an arbitrary choice (depends on implementation details like the
hash function). If inserting/deleting from a container entails
reorganization, what happens to the order of the ongoing iteration?
Depending on how iteration is implemented, you may end up visiting the
an element more than once, inadvertently skipping over some elements, or
in rare cases end up iterating forever (if the container reorg moves
your current position back while triggering more additions, and
iterating over the added elements triggers a similar reorg).
The basic problem is that the meaning of "iteration" becomes ill-defined
once the container is subject to change in the middle of iteration. The
exact semantics become dependent on the implementation details of the
container, and you basically have to know exactly how the container
works under the hood in order to predict the effects. When the
implementation details are not known / should not to be known
(encapsulation), this should generally be avoided. It's better to keep a
list of changes in a separate list, and finish the current iteration
first, then apply the changes in the list to the container.
T
--
Programming is not just an act of telling a computer what to do: it is also an act of telling other programmers what you wished the computer to do. Both are important, and the latter deserves care. -- Andrew Morton
More information about the Digitalmars-d-learn
mailing list