Thread: Vectors in vectors - pointers and iterators

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    Vectors in vectors - pointers and iterators

    Hi,

    Perhaps you can help me with a problem I have, and which I can't solve...

    First of all , I have two classes Pad and Trigger. Pad contains a vector of pointers to Trigger objects declared like:

    Code:
    vector<Trigger*> triggers
    The program also has a vector of pointers to Pad objects, declared like

    Code:
    vector<Pad*> pads
    This seams to work well in most situations and I can access variables in a trigger in a pad.

    But I need to iterate through every pad in the vector and every trigger in the vector in pad.

    I do it like this:

    Code:
    vector<Pad*>::iterator padIt;
    for (padIt = pads.begin(); padIt != pads.end(); padIt++)
    {
    
    vector<Trigger*>::iterator triggIt = (*padIt)->triggers.begin(); for( ; triggIt != (*padIt)->triggers.end(); triggIt++ ) {
    cout << (*triggIt)->getName().c_str();
    }
    Everything works fine until the line
    Code:
    cout << (*triggIt)->getName().c_str();
    where I get a segmentation fault.

    I don't know what is wrong. I can access member methods of triggers when not using iterators like this, but not when I use it like this. The thing that got me thinking is that (*padIt) works fine but I can't access (*triggIt) above!

    Is there any obvious wrong with my code here or do you want more of it?

    Thanks

    Fisherking

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Could it be that some of those pointers are null pointers, or perhaps even invalid 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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Could it be that some of those pointers are null pointers, or perhaps even invalid pointers?
    I'm with laserlight's thinking: at least one pointer pointer being NULL or invalid (eg uninitialised, a dangling pointer) is the most likely explanation.

    It is also possible that some other code has molested an unrelated NULL or invalid pointer, and happened to overwrite part of one of the vectors or (since the vectors contain pointers) one of the associated objects.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    yes, thanks! It was a invalid pointer... of some sort. I added a new element to the vector like this (the pads vector, not the Trigger vector)

    Code:
    Pad* pad = newPad;
    delete newPad;
    pads.push_back(pad);
    Didn't work that well ;-)

    Changed to
    Code:
    pads.push_back(newPad);
    and it worked!

    Thanks!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yep. You added a dangling pointer. After deleting newPad, the object no longer exists.

    The preceding line "pad = newPad;" does not, in any way, contribute to keeping the object alive. It only copies the address of the object, which you then immediately destroy.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about you actually do
    Code:
    std::vector<std::shared_ptr<Pad>> Pads;
    Pads.push_back( std::make_shared(new Pad) );
    ?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    @Elysia:
    Won't that cause the container to always have a reference to the shared_ptr? In other words the shared_ptr will never go out of scope so the container forces them to stay in scope for the life of the program.

    If the pointers do go out of scope then the vector will maintain pointers to invalid memory which is equally not good.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know the scope of the vector. Unbeknowingst this, I simply suggested the use of a smart pointer in the container.
    The whole point is that they should stay alive until the container goes out of scope or until they're erased from the vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The whole point is that they should stay alive until the container goes out of scope or until they're erased from the vector.
    Makes sense.
    Last edited by VirtualAce; 07-27-2010 at 09:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithms over vector: offsets or iterators for speed?
    By pheres in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 02:23 AM
  2. dereferencing vector iterators
    By mmfuller in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2008, 01:48 PM
  3. iterators heap or stack
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2004, 03:40 PM

Tags for this Thread