Thread: vector of pointers

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    vector of pointers

    Please read the following snippets:

    Code:
    class B
    {
    public:
    B() {}
    ~B() {}
    };
    
    class A
    {
    public:
    A()
    {
    
    B* b1 = new B();
    B* b2 = b1;
    B* b3 = new B();
    B* b4 = b2;
    m_list.push_back(b1);
    m_list.push_back(b2);
    m_list.push_back(b3);
    m_list.push_back(b4);
    } ~A() {
    for (std::vector<B*>::size_type i = 0; i < m_list.size(); i++)
    {
    if (m_list[i] != 0)
    {
    delete m_list[i];//crash when deleting b2
    m_list[i] = 0;
    }
    }
    } private: std::vector<B*> m_list; }; void main() { A * a = new A(); delete a; }
    It will crash when I attempt to delete b2 in the destructor. But I need to have a vector storing one object but is pointed by two or more vectors (like b1 and b2). I knew it is not the proper way to implement like this. Please could someone give any hint how to implement this without crashing?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Each pointer must be deleted only once. B1 B2 and B4 are the same (they point to the exact memory location).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a std::vector<std::tr1::shared_ptr<B> > instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Use a std::vector<std::tr1::shared_ptr<B> > instead.
    TR1 is fairly new and it may not be supported in linux. Please could you suggest other approaches apart from using shared pointer? Thanks.
    Last edited by chesschi; 04-06-2011 at 12:38 PM.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by chesschi View Post
    TR1 is fairly new and it may not be supported in linux. Please could you suggest other methods apart from using shared pointer? Thanks.
    It is supported. shared_ptr - Boost 1.46.1

    Methods to what? I have already pointed out what is wrong with the code - and THIS was the point of this thread.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by kmdv View Post
    It is supported. shared_ptr - Boost 1.46.1

    Methods to what? I have already pointed out what is wrong with the code - and THIS was the point of this thread.
    I knew the reason why it crashes. The question I was asking is
    "what is the proper way to handle the situation of two or more pointers pointing to the same object and being stored in the same container like vector"
    The container only copies the pointers and so they no longer share the same address.

    I am using a pretty old compiler and am keen to have my code compatible with different platforms and so I ask for alternative approach.

    Sorry for any misleading questions.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, short of writing your own smart pointer class, a possible solution is to use another vector to store the objects, then in this vector of pointers, you never use delete on any of the pointers. You only insert and erase elements of the vector.

    When you want to remove an element from that other vector, you first remove all of the pointers to that element in this vector of pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by chesschi View Post
    I am using a pretty old compiler and am keen to have my code compatible with different platforms and so I ask for alternative approach.
    If you use an old compiler, don't expect your application to be cross-platform.
    Smart pointers are the way to go.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by chesschi View Post
    I knew the reason why it crashes. The question I was asking is
    "what is the proper way to handle the situation of two or more pointers pointing to the same object and being stored in the same container like vector"
    The container only copies the pointers and so they no longer share the same address.

    I am using a pretty old compiler and am keen to have my code compatible with different platforms and so I ask for alternative approach.

    Sorry for any misleading questions.
    The other way is generally to change the logic of the code. So first to ask "why do you need to store a duplicate pointer"? Can you make a check before you store or before you delete a pointer? (is this performance wise efficient for you?)
    The point is that you should first think of your initial design to see if this can be avoided.

    Try building your own smart pointer. Its an interesting project. Apart from that there is no other automatic/generic way to solve this, since what you are asking is having a smart pointer, at least the reference counting part.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If you are about to build you own smart pointer, do it only for educational purposes. If you do not have enough knowledge, you will run into a load of problems. I have implemented a set of smart pointers because of my special needs. What is implemented in boost will be enough, if not too much, for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM