0

I'm having some trouble using the std container classes (list, map etc.) in C++. The problem seems to be to do with removing items for the containers. I'm trying to store pointers to objects within a container, and then when I want to remove the item from the container I use either erase or remove. When I do this, does the container actually delete the object that was in the container, or simply remove the pointer from the container?

What I'm hoping is that it just removes the pointer, because I'm using the containers to group objects and and object may be in more than one group, so I don't want it to be deleted when it's removed from a container!

Thanks!

James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • You can check for yourself by putting some debug code in the destructor for the item, for instance a `std::cout` statement. Anyway, what does the "some trouble" consist of exactly? – Mr Lister Mar 01 '12 at 11:09
  • Thanks for the destructor tip. It was hard to trace down what the issue was but it ended up being to do with interpreting corrupt UDP messages and crashing randomly and not a memory management issue! Just wanted to make sure I understood the C++ container classes correctly. Thanks! – James Bedford Mar 10 '12 at 10:50

4 Answers4

3

When I do this, does the container actually delete the object that was in the container, or simply remove the pointer from the container?

The latter.

... so I don't want it to be deleted when it's removed from a container!

This is in fact what happens, but I'd still like to point out std::shared_ptr, which can make pointer/container interactions a lot easier.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

When I do this, does the container actually delete the object that was in the container, or simply remove the pointer from the container?

Standard Library containers do not take the responsibility of deallocating dynamic memory of pointer elements. You will have to take care of deallocating the dynamic memory yourself, If you are storing a raw pointer in the container.

This is where power of the RAII concept in C++ comes to your rescue. You can use a Smart pointer instead of raw pointers inside the container and once you do that the deallocation will be implicitly handled by the smart pointer instead of you having the overhead of explicitly handling the memory management.

There are a number of smart pointers available and I purposefully restrained myself from suggesting a particular smart pointer because it will specifically depend on the ownership and the lifetime of the object involved.Check the link and choose a appropriate smart pointer as per your requirement.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Effective STL by Scott Meyer will give you the answer about storing raw pointer in container classes.

If you don't want to use smart_pointers or other similar things, but storing raw pointer, you have to explicitly delete it if you want to clear the memory associated.

When allocating, for example a std::vector then before clearing it, you have to delete each element of it manually.

std::vector<int*> v(10;
for (int i=0; i<10; i++)
   v.at(i) = new int;

wrong way

v.clear(); // !!! MEMORY LEAK 

the correct way is

for (int i=0; i<10; i++)
    delete v.at(i);

and then

v.clear();
linello
  • 8,451
  • 18
  • 63
  • 109
0

comipler doesn't remove object which placed in heap-memory

triclosan
  • 5,578
  • 6
  • 26
  • 50