But, then I googled, that local reference gets destroyed, and I should never do that.
What is actually the proper way to code it here? I want that local variable to be created inside function scope, so passing it back as copy is the only way then?
You could try passing in a TArray by reference and then filling it inside the function, i.e.
void somefunction(TArray<>& largeArray)
{
// change 'largeArray' here
}
However, if you are returning the TArray<> into the initialization/construction of a TArray<> variable, i.e.
TArray<> myNewArray = someFunction();
There is the possibility that the compiler may optimize it via Return Value Optimization, however I think it best to code defensively and presume that the optimization won’t occur. Through that lens, returning by parameter argument would be a nice balance (although that isn’t what you asked about).