This is my implementation of GetValidWords().
It works fine but I just have a question on why it works. The return statement returns the address of the locally declared array ‘TArray ValidWords’. Surely when the function returns the local variable goes out of scope and the address of the local variable is invalid. Shouldn’t the local variable be made static or new memory allocated for the array to be returned?
Or is a copy of the whole array made and returned?
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
TArray<FString> ValidWords;
for (int32 i = 0; i < WordList.Num(); i++)
{
int32 WordLength = WordList[i].Len();
if (WordLength >= 4 && WordLength <= 8)
{
if (IsIsogram(WordList[i]))
ValidWords.Emplace(WordList[i]);
}
}
// Debug print statements
//for (int32 i = 0; i < ValidWords.Num(); i++)
//{
// PrintLine(TEXT("%s"), *ValidWords[i]);
//}
return ValidWords;
}