# Array has changed during ranged-for iteration

**URL:** <https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123>\
**Category:** Audio\
**Tags:** unreal-engine, loop, UE4, question, CPP\
**Created:** [April 14, 2016, 7:57am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123 "2016-04-14T07:57:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_08c2fdfc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08c2fdfc](https://forums.unrealengine.com/u/anonymous_user_08c2fdfc)\
**Post date:** [April 14, 2016, 7:57am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/1 "2016-04-14T07:57:23Z")

</div>

I upgraded to 4.11 and now I have the following error:

> Array has changed during ranged-for  
> iteration

This happens because I add some elements in my TArray while iterating over it. I am aware of what I am doing, the elements I add in the array should be processed in the same for loop.

```
TArray<FEdge*> Edges;
// [...] Fill Edges
for(FEdge* Edge:Edges)
{
   Process(Edge);
   if(Condition(Edge))
   {
       Edges.Add(new FEdge()); // Add a new edge to be processed later
   }
}

```

What is the prefered method of doing so? Using a while loop?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_08c2fdfc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08c2fdfc](https://forums.unrealengine.com/u/anonymous_user_08c2fdfc)\
**Post date:** [April 14, 2016, 8:05am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/2 "2016-04-14T08:05:21Z")

</div>

Using a index based for loop fixes the problem, but I’m am not sure it’s the best solution.

```
for (int32 i = 0 ; i < Edges.Num() ; i++)
{
    FEdge* Edge = Edges[i];
}

```

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [April 14, 2016, 9:32am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/3 "2016-04-14T09:32:13Z")

</div>

Hi Arthur,

Modifying a container while iterating over it with a ranged-for has always been undefined behaviour, but happened to work with TArray. A change was made to optimise TArray’s ranged-for iteration which now prevents that case, so the assert was added to catch when you do that.

Using an index-based loop will be fine in your case.

Steve

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_08c2fdfc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08c2fdfc](https://forums.unrealengine.com/u/anonymous_user_08c2fdfc)\
**Post date:** [April 14, 2016, 9:50am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/4 "2016-04-14T09:50:23Z")

</div>

What is the difference between using a ranged-for and an index? I don’t understand how it can differ, unless TArrays do not use contiguous storage locations for their elements?

---

<div class="post-metadata">

**Author:** ![Steve\_Robb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Robb](https://forums.unrealengine.com/u/Steve_Robb)\
**Post date:** [April 14, 2016, 9:55am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/5 "2016-04-14T09:55:01Z")

</div>

TArray is contiguous. But ranged-for uses iterators, not indices. Invalidating the begin or end iterator (which adding to a container does) while iterating is undefined behaviour.

Further reading: [http://stackoverflow.com/questions/17076672/add-elements-to-a-vector-during-range-based-loop-c11](http://stackoverflow.com/questions/17076672/add-elements-to-a-vector-during-range-based-loop-c11)

(std::vector here, but the situation is still identical)

Steve

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_08c2fdfc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_08c2fdfc](https://forums.unrealengine.com/u/anonymous_user_08c2fdfc)\
**Post date:** [April 14, 2016, 10:05am UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/6 "2016-04-14T10:05:04Z")

</div>

Ok, I missed the disctinction between `for(auto _begin = r.begin(); _begin != r.end(); ++begin)` and `for(auto _begin = r.begin(), _end = r.end(); _begin != _end; ++_begin)` (the cacheing of r.end()) .

---

<div class="post-metadata">

**Author:** ![brigadier-bren](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@brigadier-bren](https://forums.unrealengine.com/u/brigadier-bren)\
**Post date:** [July 18, 2024, 8:39pm UTC](https://forums.unrealengine.com/t/array-has-changed-during-ranged-for-iteration/354123/7 "2024-07-18T20:39:28Z")

</div>

how do i edit this code in UE4.26?

i’ve got a

[File:F:\DEV\ND23-BuildPublicEditor-EpicRelease\Engine\Source\Runtime\Core\Public\Containers/Array.h] [Line: 216]  
Array has changed during ranged-for iteration!

error, i’m not using code, just the graphical interface, how can i solve this?

Regards,  
M
