Im trying to create an array of tm* structs, and then return them at the end of the function. This is what i have at the moment:
struct tm* BusinessLogicLayer::GetNoResponceTime()
{
struct tm* time_v[3];
struct tm* time_save;
int s = 3;
time_save = LastSavedTime();
time_v[0] = time_save;
sleep(5);
time_save = LastSavedTime();
time_v[1] = time_save;
sleep(5);
time_save = LastSavedTime();
time_v[2] = time_save;
return time_v;
}
I understand that given the code i have now it will not be possible to return the array, because it will be destroyed when the function ends. Can anyone help me with regards to how i would go about being able to access the array from the returned value after the function has ended?
Regards
Paul
Either allocate the array on the heap, but you will need to remember to
delete[]the array when you have finished using it.Or as this is C++, a better option would be to use a vector
The vector itself is allocated on the stack but it holds an array which is allocated on the heap. When you copy the vector (for instance returning it) the contents of the underlying array are also copied.
You could also simplify the function by not using the temp variable
time_saveThe C style struct declarations are also not required in C++