0

I want to free the memory from my set. I tried how to free memory from a set post, but it didn't work for me.

This is my code:

#include <iostream>
#include <set>
using namespace std;

struct Deleter
{
    void operator () (int *ptr)
    {
        delete ptr;
    }
};

int main()
{
    set<int> myset;
    myset.insert(100);
    myset.insert(1);
    myset.insert(12);
    myset.insert(988);
    set<int>::iterator it;
    for (it = myset.begin() ; it!=myset.end() ; it++)
    {
        Deleter(it);
        cout<<"size of set: "<<myset.size()<<endl;
    }

    return 0;
}

The output is:

size of set: 4
size of set: 4
size of set: 4
size of set: 4

How does this code free the memory although the size of set is still 4? What does it delete by Deleter(it) ? If I use myset.clear() at the end, would it free all memory from set?

Thanks.

Community
  • 1
  • 1
  • 3
    Why are you trying to delete something that hasn't being allocated by you ? You should use that Deleter, if set contains object that are pointers. – P0W Jul 31 '14 at 14:48
  • a pointer can point to something that was not allocated using new. Just because something is a pointer does not mean it's safe to use delete on it. – Boumbles Jul 31 '14 at 14:51
  • 1
    Calling `delete` on a member of a set either deletes what the member points to, or it is an error. It does not remove the member from the set. For that, you need `set::erase` – Logicrat Jul 31 '14 at 14:53

2 Answers2

3

In C++ you use delete to free up memory that was allocated when you new up an object. When you new up an object, you are saying you will manage the memory for it.

In this case, you are not newing anything up, you are simply inserting an element into the set. To remove the item from the set use the erase method.

It's overloaded a couple of different ways, so depending on if you want to remove it by iter or value you will pass different arguments. Here is a good reference for set erase.

    myset.erase(100);
    myset.erase(100);
    myset.erase(12);
    myset.erase(988);

Also, if you want to remove everything in the set, simply call the clear() method.

myset.clear();
Brian S
  • 3,096
  • 37
  • 55
  • @Boumbles Thanks. But the fact is that I insert objects of a class in the real code. I wondered why clear() function does not free all the memory itself although it destroys all elements of the set and why I need to delete the content of every element of my set to make it free? – farnush.farhadi Jul 31 '14 at 16:09
  • ARe you saying that the clear does not remove the elements from the set? What are you using to determine if memory is freed? – Brian S Jul 31 '14 at 17:21
1

The word delete is to be used on data that has been dynamically allocated on the heap using new.

If you want to remove something from an stl set you can do so using the erase function.

If your set contained pointers to dynamically allocated objects then you could call delete on them but your set would still contain those pointers. You would still need to erase the pointers from your set.

Boumbles
  • 2,473
  • 3
  • 24
  • 42