I was reviewing a friend's code and got into an interesting debate on how C/C++ allocates memory on the stack and manages its release. If I were to create an array of 10 objects in a function, but return said array, does it release when the function pops (hence making the given data invalid) or is it placed into the heap (which raises the question of how do we release it?).
Sample code as follows:
Gene* GetTopTen()
{
// Create 10 genes (or 10 objects, doesn't matter)
Gene Ten[10];
// Sort out external pool data
Sort();
// Copy over data to the array of 10 objects
for(int i = 0; i < 10; Ten[i++] = pool[i]);
// Here is the core of my question:
return Ten;
}
Any help is greatly appreciated, this is turning into a very interesting question my friends and I can't answer.