3

I made a pair like this

typedef std::pair<int, Dish> OrderPair;

when Dish is an object. and I got a vector of OrderPair:

std::vector<OrderPair> orderList;

assuming that I want to delete one pair from the vector, does erase function is enough when I don't want to delete the dish from memory? I think the answer is yes, because creating a pair is not sitting in the heap, and therefore I don't need to release any memory. please tell me if I'm wrong.

Mr.O
  • 342
  • 2
  • 19
  • 10
    Only instance which were allocated using `new` need to be deleted using `delete`. – πάντα ῥεῖ Nov 05 '18 at 09:34
  • 1
    Possible duplicate of [When to use "delete"?](https://stackoverflow.com/questions/9888504/when-to-use-delete) – hellow Nov 05 '18 at 09:36
  • 1
    Q. When do I use `delete`? A. **Never**. Every `delete` you will ever need is already encapsulated inside `shared_ptr` and/or `unique_ptr`. (You may need `delete´ if you are writing your own smart pointer, but then you wouldn't be asking this question). – n. m. could be an AI Nov 05 '18 at 10:12

2 Answers2

5

When your OrderPair gets deleted, then the Dish that's part of it is also gone. When std::vector<OrderPair> orderList goes out of scope (end of the function it was declared in, etc.) then all the OrderPair within and thus all the Dish in there are also gone. When you erase an OrderPair using erase, then that Dish is gone.

does erase function is enough when I don't want to delete the dish from memory?

If you call erase, then that OrderPair and thus that Dish is gone from memory. If that's not what you want, you need a different data structure. For instance if OrderPair doesn't contain a Dish but a pointer to Dish, like this:

typedef std::pair<int, Dish*> OrderPair;

Then you could indeed erase that pair and the Dish isn't deleted from memory, just that one reference to it. But with just that small code snippet, it's hard to know your intentions.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • @Mr.OY You might also want to consider smart pointers here, all the _unique_, _shared_ and _weak_ might be of use here. It all depends on your scenario, we don't know the details since you didn't provide them. Raw pointers can be suitable as well. – Daniel Langr Nov 05 '18 at 09:44
  • @Blaze, I can't change the signature of the pair because it's part of code that I was given. with that obstacle, 'what is the best possible option I got? creating a copy of dish and set it into the pair? in that case you said that erase the pair (erase function of the vector) will delete the copied object from memory yes? – Mr.O Nov 05 '18 at 10:48
  • That's right, if you put a copy of `Dish` into the pair and delete that pair later, then only the copy is gone. – Blaze Nov 05 '18 at 10:51
3

Only objects that are explicitly allocated dynamically - using new or malloc, need to be explicitly deleted using delete or free. Heap memory is used for dynamic allocations.

In your case, you are pushing Dish object itself into the pair and not a pointer pointing to dynamic allocation.

>typedef std::pair <int, Dish> OrderPair;

So, the scope of the Dish object is bound to the scope of the pair object. When the pair in the vector is erased using erase method, the pair and with it the Dish object gets destroyed too.