seg-fault on vector-auto-reserve

M

Matthias Kaeppler

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?
 
M

Matthias Kaeppler

Matthias said:
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?
 
V

Victor Bazarov

Matthias Kaeppler said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top