#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    81
    Rep Power
    12

    segmentation fault


    I get a segmentation fault in this code.
    Thanks,
    Lalitha



    typedef std::vector < int > IntVector;
    std::vector< IntVector > myVector;
    vector< IntVector >::iterator it;
    myVector.resize( 10 );
    int i=0;
    for( it = myVector.begin(); it < myVector.end(); ++it)
    {
    IntVector &in =*it; //I am not sure IntVector in =*it; or & in =*it;
    in.resize( 50 );
    cout<< in.size()<<endl;
    for( int j=0;j <in.size(); ++j)
    {
    myVector[i][j]=j;

    }
    ++i;
    }
  2. #2
  3. No Profile Picture
    Dinesh_P_V
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    India
    Posts
    259
    Rep Power
    0
    You have not posted the full code. But the code you posted seems to have no errors
    Let me have a program like

    #include <vector>
    #include <iostream>
    using namespace std;
    int main()
    {
    typedef std::vector < int > IntVector;
    std::vector< IntVector > myVector;
    vector< IntVector >::iterator it;
    myVector.resize( 10 );
    int i=0,j=0;
    for( it = myVector.begin(); it < myVector.end(); ++it)
    {
    IntVector &in =*it; //I am not sure IntVector in =*it; or & in =*it;
    in.resize( 5 );
    cout<< in.size()<<endl;
    for(j=0;j <in.size(); ++j)
    {
    myVector[i][j]=j;
    }
    ++i;
    }
    i=0;
    for( it = myVector.begin(); it < myVector.end(); ++it)
    {
    IntVector &in =*it; //I am not sure IntVector in =*it; or & in =*it;
    cout<<"\n";
    for(j=0;j <in.size(); ++j)
    {
    cout<<"\t"<<myVector[i][j];
    }
    i++;
    }
    // cout<<"\t"<<myVector[i+1][j+1];
    }

    In this when you uncomment the last line you will get the segmentation fault error. This is because you are accessing beyond the vector size. This is same in your case. Try to find where you access beyond the size of the vector.
    HTH
    -Murugesan

IMN logo majestic logo threadwatch logo seochat tools logo