"I want to clear the storage memory of the function after it finished its job" ~> As long as you are using objects with automatic storage duration, everything is taken care of (in terms of memory management). Problems with memory management start when you start using dynamic allocation, which makes you the one, who is responsible for cleaning up. Basically: for every new
that you call a delete
should be called and for every new[]
that you call a delete[]
should be called. So if you can, avoid allocating stuff dynamically always when it is possible.
Now note that your function takes the second argument by value:
vector<customers> process(char* const *filename, vector<int> ID)
so when you pass a std::vector<int>
object into your function, a copy of this vector is created. Also passing filename
in form of char* const*
seems to be more complicated than necessary. Much more reasonable prototype in this case would be:
std::vector<customers> process(const char* filename, const std::vector<int>& ID)
Now in the body of this function:
{
std::vector<customers> list_of_customers;
...
return list_of_customers;
}
list_of_customers
is an object with automatic storage duration. With no optimization, upon return
call a copy of this vector would be created and the original object would be properly destroyed. Yet, there are various optimization techniques like NRVO that will take care of redundant copies being created so you don't need to worry about it. In case you will use this function in following way:
std::vector<customers> list_of_customers = process(filename, ID);
copy elision will most likely take place so no copy will be created even without NRVO.
Have a look at: What are copy elision and return value optimization?