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. Advertising

  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. Advertising

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

    vector.reserve

    john smith, Jul 25, 2003, in forum: C++
    Replies:
    5
    Views:
    690
    John Harrison
    Jul 25, 2003
  2. Piotr
    Replies:
    10
    Views:
    546
    Daniel T.
    Jan 30, 2006
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. Replies:
    2
    Views:
    499
    David Harmon
    Feb 9, 2007
  5. linkswanted
    Replies:
    1
    Views:
    902
Loading...

Share This Page