Simple question about vectors of pointers

Question: Is it dangerous, or at least bad practice, to have multiple vectors of pointers to the same objects? For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <vector>

class MyClass{};

int main() {
  std::vector<MyClass *> vec;
  for(int i = 0; i < 10; ++i) {
    vec.push_back(new MyClass);
  }

  std::vector<MyClass *> secVec;
  secVec.resize(vec.size());
  for(int i = 0; i < vec.size(); ++i) {
    secVec[i] = vec[i];
  }

  for(int i = 0; i < vec.size(); ++i) {
    delete vec[i];
    vec[i] = 0;
  }
  vec.clear();

  for(int i = 0; i < secVec.size(); ++i) {
    secVec[i] = 0;
  }
  secVec.clear();

  system("PAUSE");
  return 0;
}


I've been told (on a non-programming forum) that this is a terrible thing to do, because calling insert(), erase(), etc. on the vector will cause problems. However, after testing it fairly extensively I can't seem to get any errors, and it seems that the pointers are pointing to the objects addresses in memory, not to their positions in the vector.

I know that when using iterators calling insert() and such will screw up the iterator, but my feeling is I have been given incorrect information, and there is nothing wrong with having several vectors storing pointers to the same set of objects (provided I don't end up calling delete multiple times on the same object).

Please advise, oh wise and knowing C++ people.
it's always dangerous to have two pointers to the same object whether vectors or not. The problem is that you may access the object pointed to while it's already deleted.

to circumvent this use auto_ptr -> http://www.cplusplus.com/reference/std/memory/auto_ptr/ or better boost Smart Pointers -> http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/smart_ptr.htm which destroy the object at the right time
You cannot use std::auto_ptr in container classes. Use the boost::shared_ptr for that.
what you've done there isn't a problem because the two lists are idetical and you've only called delete on one of them. If they weren't identical lists though you'd need to delete the contents of both, and could end up calling delete on a pointer with an invalid memory address.

So I'd suggest when you're deleting the poiters check theyre not NULL, then delete the data, then set the pointer to NULL so that if you do the same an a duplicate pointer in a different list it won't try to call delete.

You also need to be sure when deleting a pointer that its data isn't needed anywhere else in the program. Is there a real reason you need to store duplicate pointers? If not then you're best off avoding it.
Duoas wrote:
You cannot use std::auto_ptr in container classes. Use the boost::shared_ptr for that.
OOps I never used auto_ptr (Didn't want to spread false information)
Thanks for your help. I'm not familiar with smart pointers or boost so I will have to familiarize myself with that before I start using it. I believe this answers my question though.

@quirkyusername:
The vectors of duplicates was to make choosing a random item from a subset of a much larger set easier. The pointers was because each item would be an instance that would have either copies of or pointers to many other instances. Making all those copies, and copies within copies, seemed like it would take up an awful lot of space, hence using pointers instead. I'll have to think more about how I want it to work and which way will really be easiest for me.
Last edited on
Topic archived. No new replies allowed.