Vector element erase cause SIGSEGV

Discussion in 'C++' started by Bin Chen, Jan 18, 2007.

  1. Bin Chen

    Bin Chen Guest

    Hi,

    I don't know what's wrong with my program, it causes SIGSEGV:

    #include <vector>

    using namespace std;

    int main(void)
    {
    vector<int> v;
    vector<int>::iterator iter;

    int i,j,k;

    i = 9;j = 10;k = 11;

    v.push_back(i);
    v.push_back(j);
    v.push_back(k);

    for (iter = v.begin();iter != v.end(); ++iter) {
    printf("*********%d\n", *iter);
    iter = v.erase(iter);
    }

    return 0;
    }

    Thanks.
    ABAI
    Bin Chen, Jan 18, 2007
    #1
    1. Advertising

  2. Bin Chen wrote:

    > I don't know what's wrong with my program, it causes SIGSEGV:
    >
    > #include <vector>
    >
    > using namespace std;
    >
    > int main(void)
    > {
    > vector<int> v;
    > vector<int>::iterator iter;
    >
    > int i,j,k;
    >
    > i = 9;j = 10;k = 11;
    >
    > v.push_back(i);
    > v.push_back(j);
    > v.push_back(k);
    >
    > for (iter = v.begin();iter != v.end(); ++iter) {


    Change the above line to:

    for (iter = v.begin();iter != v.end();) {

    > printf("*********%d\n", *iter);
    > iter = v.erase(iter);
    > }
    >
    > return 0;
    > }


    std::vector::erase returns an iterator to the element immediately
    following the one that was erased. So in effect, the expression "iter
    = v.erase(iter)" causes iter to to be incremented. But your for loop
    also increments iter. In other words, iter is incremented _twice_ each
    time through the loop. The first time through the loop, iter begins
    pointing to v[0] and ends pointing to v[2]. The second time through
    the loop, iter begins pointing to v[2]. After printing the value of
    v[2], the program erased v[2], and increments iter so that it is equal
    to v.end(). Then, the program attempts to increment iter again (as
    happens every time through your for loop), leading to undefined
    behavior - in this instance, a seg fault.

    To fix your program, change the line indicated above.

    Best regards,

    Tom
    Thomas Tutone, Jan 18, 2007
    #2
    1. Advertising

  3. Bin Chen

    Guang Han Guest

    "Bin Chen дµÀ£º
    "
    > Hi,
    >
    > I don't know what's wrong with my program, it causes SIGSEGV:
    >
    > #include <vector>
    >
    > using namespace std;
    >
    > int main(void)
    > {
    > vector<int> v;
    > vector<int>::iterator iter;
    >
    > int i,j,k;
    >
    > i = 9;j = 10;k = 11;
    >
    > v.push_back(i);
    > v.push_back(j);
    > v.push_back(k);
    >
    > for (iter = v.begin();iter != v.end(); ++iter) {
    > printf("*********%d\n", *iter);
    > iter = v.erase(iter);

    erase will return the next element after the iter. assuming you are
    erasing the last element, then iter will be v.end (), and then in your
    for loop you'll still ++iter, so it should be some errors.....
    > }
    >
    > return 0;
    > }
    >
    > Thanks.
    > ABAI
    Guang Han, Jan 18, 2007
    #3
  4. If the question can be reformulated as how to fix,
    then this is something you need to do

    int Size = v.size();
    for (int I = 0; I < Size; I++) {
    printf("*********%d\n", *v.begin());
    v.erase(v.begin());
    }


    If you want really to know why it is not working,
    this may give you a hint:


    for (iter = v.begin();iter != v.end(); ++iter) {
    printf("BEF *********%d\n", *iter);
    iter = v.erase(iter);
    printf("AFT *********%d\n", *iter);
    }

    Erasing any element except the last causes the vector to
    be "rearanged", and return value is iterator to next element.
    Erasing the last element seems not to return v.end() in my tests,
    but returns the last element and this could be because the pointer
    to the last location is in memory.

    -haro
    -----------------------------------------
    Sorry if this is not complete answer, I just thought it
    might help you quickly in case you are not trying to deeply
    understand what is going on.


    Bin Chen wrote:
    > Hi,
    >
    > I don't know what's wrong with my program, it causes SIGSEGV:
    >
    > #include <vector>
    >
    > using namespace std;
    >
    > int main(void)
    > {
    > vector<int> v;
    > vector<int>::iterator iter;
    >
    > int i,j,k;
    >
    > i = 9;j = 10;k = 11;
    >
    > v.push_back(i);
    > v.push_back(j);
    > v.push_back(k);
    >
    > for (iter = v.begin();iter != v.end(); ++iter) {
    > printf("*********%d\n", *iter);
    > iter = v.erase(iter);
    > }
    >
    > return 0;
    > }
    >
    > Thanks.
    > ABAI
    >
    Haro Panosyan, Jan 18, 2007
    #4
  5. Bin Chen

    Nate Barney Guest

    Thomas Tutone wrote:
    >
    > for (iter = v.begin();iter != v.end();) {
    >
    > printf("*********%d\n", *iter);
    > iter = v.erase(iter);
    > }
    >


    I would be tempted to write the loop this way:

    for (iter = v.begin(); iter != v.end(); iter = v.erase(iter))
    printf("*********%d\n", *iter);

    Does anyone care to comment on whether or not this is good style?

    --
    Nate
    Nate Barney, Jan 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mosfet

    STL vector and erase : HELP

    mosfet, Nov 14, 2003, in forum: C++
    Replies:
    2
    Views:
    587
    Ivan Vecerina
    Nov 14, 2003
  2. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    356
    Pete Becker
    Mar 30, 2006
  4. Anil
    Replies:
    5
    Views:
    430
    Jim Langston
    Dec 18, 2007
  5. sravanreddy001

    Vector push_back() giving SIGSEGV error

    sravanreddy001, Sep 22, 2011, in forum: C++
    Replies:
    5
    Views:
    1,247
    Jorgen Grahn
    Sep 23, 2011
Loading...

Share This Page