seg-fault on vector-auto-reserve

Discussion in 'C++' started by Matthias Kaeppler, Feb 27, 2005.

  1. Hi,

    I'm holding a vector of boost::filesystem::pathS and a vector of
    pointers to them. I use the reserve member function on both to reserve
    space for 500 files. That means, for the 501th file added, the vector
    will grow to hold 1000 files right?
    Well, as soon as this happens I am getting a segmentation fault. I'm
    sure this is the reason, because the program crashed when I entered
    /usr/bin which holds about 1700 files, and if I change the initial
    capacity to 2000 instead, the program won't crash.

    I'm totally lost, any idea what could cause the crash?

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Feb 27, 2005
    #1
    1. Advertisements

  2. Matthias Kaeppler wrote:
    > Hi,
    >
    > I'm holding a vector of boost::filesystem::pathS and a vector of
    > pointers to them. I use the reserve member function on both to reserve
    > space for 500 files. That means, for the 501th file added, the vector
    > will grow to hold 1000 files right?
    > Well, as soon as this happens I am getting a segmentation fault. I'm
    > sure this is the reason, because the program crashed when I entered
    > /usr/bin which holds about 1700 files, and if I change the initial
    > capacity to 2000 instead, the program won't crash.
    >
    > I'm totally lost, any idea what could cause the crash?
    >


    I did a segfault backtrace and recognized that it happens in
    std::partition invoked on the pointer-vector. Is this, because as soon
    as the vector gets resized, all pointers get invalidated?

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Feb 27, 2005
    #2
    1. Advertisements

  3. "Matthias Kaeppler" <> wrote...
    > Matthias Kaeppler wrote:
    >> Hi,
    >>
    >> I'm holding a vector of boost::filesystem::pathS and a vector of pointers
    >> to them. I use the reserve member function on both to reserve space for
    >> 500 files. That means, for the 501th file added, the vector will grow to
    >> hold 1000 files right?
    >> Well, as soon as this happens I am getting a segmentation fault. I'm sure
    >> this is the reason, because the program crashed when I entered /usr/bin
    >> which holds about 1700 files, and if I change the initial capacity to
    >> 2000 instead, the program won't crash.
    >>
    >> I'm totally lost, any idea what could cause the crash?
    >>

    >
    > I did a segfault backtrace and recognized that it happens in
    > std::partition invoked on the pointer-vector. Is this, because as soon as
    > the vector gets resized, all pointers get invalidated?


    Quite possible. Resizing may require reallocation. Reallocation makes
    all iterators and references (and pointers) invalid.

    V
     
    Victor Bazarov, Feb 27, 2005
    #3
    1. Advertisements

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. john smith

    vector.reserve

    john smith, Jul 25, 2003, in forum: C++
    Replies:
    5
    Views:
    924
    John Harrison
    Jul 25, 2003
  2. Alex Vinokur

    vector : reserve(LONG_MAX)

    Alex Vinokur, Apr 22, 2004, in forum: C++
    Replies:
    2
    Views:
    552
    Pete Becker
    Apr 22, 2004
  3. Gary Kuehn
    Replies:
    2
    Views:
    615
    Gary Kuehn
    Jul 19, 2005
  4. Alex Vinokur
    Replies:
    8
    Views:
    638
    Alex Vinokur
    Sep 23, 2005
  5. Piotr
    Replies:
    10
    Views:
    676
    Daniel T.
    Jan 30, 2006
  6. Replies:
    8
    Views:
    2,348
    Csaba
    Feb 18, 2006
  7. utab
    Replies:
    7
    Views:
    732
    Jonathan Mcdougall
    May 28, 2006
  8. Replies:
    2
    Views:
    631
    David Harmon
    Feb 9, 2007
Loading...