By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,915 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,915 IT Pros & Developers. It's quick & easy.

segmentation fault when resizing a vector

P: n/a
I get a segmentation fault in my program when resizing a vector. This
seems strange because it is the second resize that causes the error.

std::vector<double> a;
std::vector<double> b;

a.resize(ndata - 1);
b.resize(ndata - 1); // fault occurs here

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sa************@hotmail.com wrote:
I get a segmentation fault in my program when resizing a vector. This
seems strange because it is the second resize that causes the error.

std::vector<double> a;
std::vector<double> b;

a.resize(ndata - 1);
b.resize(ndata - 1); // fault occurs here


And will you tell us what ndata is? :)

--
Matthias Kaeppler
Jul 23 '05 #2

P: n/a
2

Jul 23 '05 #3

P: n/a
I guess it should be

a.resize(ndata);
b.resize(ndata);

but it still does not explain the error

Jul 23 '05 #4

P: n/a
sa************@hotmail.com wrote:
2

#include <vector>

int main()
{
std::vector<double> a;
std::vector<double> b;

a.resize(1);
b.resize(1);
}

This compiles and runs without segfaulting, are you /sure/ that ndata-1
evaluates to 1?

Here is what the standard requires resize() to do:

if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
; //do nothing

I'm not sure what happens if sz is negative. My compiler issues a
warning if that happens.

--
Matthias Kaeppler
Jul 23 '05 #5

P: n/a
Changing to ndata instead of ndata - 1 solves the problem, at least the
execution point goes much further, perhaps the executable is using some
kind of look ahead and this is why the fault occurs at that point
rather than further on in my code. I am using g++ in cygwin. I think
I ndata is right rather than ndata - 1.

Matthias Kaeppler wrote:
sa************@hotmail.com wrote:
2

#include <vector>

int main()
{
std::vector<double> a;
std::vector<double> b;

a.resize(1);
b.resize(1);
}

This compiles and runs without segfaulting, are you /sure/ that ndata-1
evaluates to 1?

Here is what the standard requires resize() to do:

if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
; //do nothing

I'm not sure what happens if sz is negative. My compiler issues a
warning if that happens.

--
Matthias Kaeppler


Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.