Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

TArray.Remove(TArray[i]) Causes a Crash

I have a TArray and another int32 value. If my other integer value is in my TArray, I want to remove it.

However, the line MyArray.Remove(MyArray[i]) causes a crash (and unfortunately a nondescript error log.)

 for (int32 i = 0; i < MyArray.Num(); i++)
     if (MyArray[i] == OtherIntegerValue)
         //MyArray.Remove(MyArray[i]); Commented out in case, somehow, MyArray doe not contain this?
         if (MyArray.Contains(MyArray[i]))
             UE_LOG(LogTemp, Log, TEXT("We have it!"));
             UE_LOG(LogTemp, Log, TEXT("We oddly enough do NOT have it..."));

I know that the array contains this element, as if I comment out the Remove line, I still print out "We have it!" to the log. However, the lines int32 ThisValue = MyArray[i]; MyArray.Remove(ThisValue) works. I was curious why this is?

Product Version: UE 4.10
more ▼

asked Mar 31 '16 at 02:13 PM in C++ Programming

avatar image

165 18 27 39

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first


TArray::Remove removes all instances of an element. So if your array contained { 1, 2, 1, 2 }, and you removed '1', you'd be left with { 2, 2 }.

TArray::Remove's argument is taken by reference, so if your reference is to an element within the container being removed from, you'd get { 2, 1 }, because it'll remove the first element (which compares equal with itself), then assign the following 2 over it, then continue to remove 2s because that's now the element you're looking for.

You can use RemoveSingle, which only removes the first element it finds (and thus is not susceptible to this problem). Or you should probably call RemoveAt(i) instead, which removes the element at a given index, which you have.

However, given the code you're written, it would be more succinct to replace it all with:

 int32 NumRemoved = MyArray.Remove(OtherIntegerValue);
 UE_CLOG(NumRemoved > 0, LogTemp, Log, TEXT("We oddly enough do NOT have it..."));

Hope this helps,


more ▼

answered Mar 31 '16 at 04:46 PM

avatar image

Steve Robb STAFF
2.8k 58 31 101

avatar image Kowbell Mar 31 '16 at 05:38 PM

I ended up going with that final suggestion, thanks.

Also, I've never seen UE_CLOG and can't find documentation. It looks pretty useful; do you use it the same as UE_LOG but just put in a condition first?

avatar image Steve Robb STAFF Mar 31 '16 at 05:42 PM

Yep, that's right. Not sure why UE_CLOG isn't documented, but it's been in UE4 since the first release.


avatar image Kowbell Mar 31 '16 at 05:44 PM

Cool, thanks!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question