Vectors in vectors - pointers and iterators

This is a discussion on Vectors in vectors - pointers and iterators within the C++ Programming forums, part of the General Programming Boards category; Hi, Perhaps you can help me with a problem I have, and which I can't solve... First of all , ...

  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
    21,648
    Could it be that some of those pointers are null pointers, or perhaps even invalid pointers?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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%.

  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,261
    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%.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    @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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 02:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21