STL vector core dump

This is a discussion on STL vector core dump within the C++ Programming forums, part of the General Programming Boards category; Hey everybody. I have an issue where pushing a new item onto an STL vector, I get a core dump. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    STL vector core dump

    Hey everybody. I have an issue where pushing a new item onto an STL vector, I get a core dump. Now, the capacity of the vector is not exceeded (its only 3). The vector contains integers. Now, in my application, I push stuff to the vector and works fine until that one point. Basically, I do this:

    Code:
    vector<int> vec;
    vec.push_back(newInt);
    and get a core dump. Now, the function which involves this, is part of the class reached from the outside by a few iterator references (in one instance one of those iterators is a const_iterator, and it is converted into a regular iterator). All the references are valid, and I know that because I put couts before and after the push_back. I see the first cout, but not the second...Ay ideas..help is cool.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,639
    All the references are valid, and I know that because I put couts before and after the push_back.
    That is no guarantee that the iterators are valid. I suggest that you post the smallest and simplest compilable program that demonstrates the problem.
    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 2007
    Posts
    34
    I realized something else too: if I cout the newInt, it is correct. However, if I try to push it, a core dump happens. Now, here is the kicker: if I replace newInt with some other integer (local variable or simply a constant, the core dump dissapears). Very strage.

    Code:
    void somClass::addNewInt(const int& newInt)
    { 
        vec.push_back(newInt);
    }

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Yet even stranger:

    If I add the following code, the core dump goes away:

    Code:
    void somClass::addNewInt(const int& newInt)
    { 
        vector<int> vec2;
        vec2.push_back(newInt);
    
        vec.push_back(newInt);
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Just out of curiosity, are there any calls to erase on this vector at some point?

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Quote Originally Posted by swoopy View Post
    Just out of curiosity, are there any calls to erase on this vector at some point?
    Nope.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Regardless of the existence of iterators, valid or not, a call to push_back() on a vector should never fail. I would suspect you've caused memory corruption somewhere.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by creativeinspira View Post
    I realized something else too: if I cout the newInt, it is correct. However, if I try to push it, a core dump happens. Now, here is the kicker: if I replace newInt with some other integer (local variable or simply a constant, the core dump dissapears). Very strage.

    Code:
    void somClass::addNewInt(const int& newInt)
    { 
        vec.push_back(newInt);
    }
    since newInt is a reference, could you show us where you've called your addNewInt() function from, and the source of newInt - perhaps include the block of code where you've declared 'vec' aswell (I suspect there may be a dangling reference somewhere)

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Thanks to you for all who tried to help. I can't post detailed code. I will just figure this out myself.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    One difference between one of the working code snippets and the not working code snippet is the declaration of the vector. When it works it is declared locally, but when it fails it is apparaently declared as a member of a class.

    One place to look is that class object itself. Oftentimes, you will get this kind of error when the object that the function is called on (in this case a somClass object) is bad. So I would go down the call stack and see if a pointer to somClass is used and make sure it is valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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