3

I have a function which processes and stores a lot of data in it and then it returns the results as a vector of class. The amount of data stored in this function is tremendous and I want to clear the storage memory of the function after it finished its job. Is it necessary to do so (does the function automatically clear the memory) or should I clear the memory by some function?

Update:

vector<customers> process(char* const *filename, vector<int> ID)
{
    vector<customers> list_of_customers;
    (perform some actions)
    return list_of_customers;
}
LihO
  • 41,190
  • 11
  • 99
  • 167
POD
  • 509
  • 8
  • 20

3 Answers3

6

Variables defined locally within the function will be automatically released at the end of the function scope. The destructors for any objects will also be called, which should free any memory allocated by those objects (if the objects were coded correctly). An example of such an object would be a std::vector.

Anything you've allocated yourself with new must have a corresponding delete to release the storage. Try to avoid doing your own allocation and use RAII instead, i.e. containers or smart pointers.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
3

"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?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

If the objects within the function have suitable destructors or they are not allocated in heap by malloc or new. They will be disposed automatically. Otherwise you should delete them.

For example, if you're using a standard container such as std::vector<Object> v; the object v will be disposed automatically.

In case of using smart pointers it's possible to new an object and expect it be freed automatically.

masoud
  • 55,379
  • 16
  • 141
  • 208