Segmentation fault

T

trxrse

Hello,

I've got the following piece of code:

#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>

vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;
}
}

vector <vector <double> > data_occupied;
for (long i=0;i<Count_Grid_Easting-1;i++){
data_occupied.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data_occupied[j]=0;
}
}

Every time I run it I get "Segmentation Fault". What am I doing
wrong, here, people???


Thanks
trxrse
 
A

Andre Kostur

Hello,

I've got the following piece of code:

#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>

vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;


data[j] is referring to an element in the vector that you just pushed
onto data. That vector is still empty, thus you've accessed beyond the
end of the container. You probably want to replace your loop over j
with:

data.resize(Count_Grid_Northing);

}
}

vector <vector <double> > data_occupied;
for (long i=0;i<Count_Grid_Easting-1;i++){
data_occupied.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data_occupied[j]=0;


Same as above.
}
}

Every time I run it I get "Segmentation Fault". What am I doing
wrong, here, people???

Accessing beyond the end of a container. Undefined Behaviour, which
happens to be manifesting as a segfault.
 
R

Rolf Magnus

trxrse said:
Hello,

I've got the following piece of code:

#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>

vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());

Simpler and quite a bit more efficient than repeated push_back() in the loop
would be a:

data.resize(Count_Grid_Easting-1);

before the loop.
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;


Here is your problem. The inner vectors are all empty, i.e. have a size of
0. In the outer loop, you did it right by using push_back(). Why not in the
inner loop? The last line should be:

data.push_back(0);

Or again replace the for loop with a:

data.resize(Count_Grid_Northing-1);

which will also default-initialize all the elements.


In total, you can replace the whole thing with:

vector <vector <double> > data(Count_Grid_Easting-1,
vector<double>(Count_Grid_Northing-1));
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello,

I've got the following piece of code:

#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>

vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());

Adds an empty vector to data.
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;


Tries to access the j'th element in the empty vector.

If you want to add a vector of size Count_Grid_Easting - 1 with all
elements 0 you should use
data.push_back(vector<double>(Count_Grid_Easting - 1, 0));
instead.
 
J

Jerry Coffin

[ ... ]
vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;
}
}


Perhaps something like this would work a bit better:

vector<double> temp(Count_Grid_Northing-1);
vector<vector<double> data(Count_Grid_Easting-1, temp);

By default, the elements of temp will be default constructed, which
means zero-filled in the case of arithmetic types. data is then
constructed with each element a copy of temp, so we end up with a vector
of vectors, all filled with zeros. The one shortcoming is that we're
creating an extra vector (temp) just to initialize data. If that's a
major problem, you could do something like this:

vector<vector<double> > data(Count_Grid_Easting-1);

for (size_t i=0; i<data.size(); i++)
data.resize(Count_Grid_Northing);
 
T

trxrse

Thanks very much - you're really kind people. Indeed, this was the
problem.

Thanks
trxrse


[ ... ]
vector <vector <double> > data;
for (long i=0;i<Count_Grid_Easting-1;i++){
data.push_back(vector <double>());
for (long j=0;j<Count_Grid_Northing-1;j++){
data[j]=0;
}
}


Perhaps something like this would work a bit better:

vector<double> temp(Count_Grid_Northing-1);
vector<vector<double> data(Count_Grid_Easting-1, temp);

By default, the elements of temp will be default constructed, which
means zero-filled in the case of arithmetic types. data is then
constructed with each element a copy of temp, so we end up with a vector
of vectors, all filled with zeros. The one shortcoming is that we're
creating an extra vector (temp) just to initialize data. If that's a
major problem, you could do something like this:

vector<vector<double> > data(Count_Grid_Easting-1);

for (size_t i=0; i<data.size(); i++)
data.resize(Count_Grid_Northing);

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
K

Kai-Uwe Bux

Old said:
Top-posting was correct in this case. The OP's comments
did not refer to any specific piece of the message he
was replying to, and did not require any context to
interpret.

Actually, the way I read it, the "this" in "Indeed, this was the problem"
refers to a specific piece of information, and the quoting style of the OP
turns the pronoun into a dangling reference.


Best

Kai-Uwe Bux
 
O

Old Wolf

Actually, the way I read it, the "this" in "Indeed, this was the problem"
refers to a specific piece of information, and the quoting style of the OP
turns the pronoun into a dangling reference.

Nevertheless, it is clear that the message is a thankyou
note, and 'this' refers to the problem that the OP had
and is now solved. It's not necessary to know what the
actual problem was, in order to understand the purpose
and content of the post.
 
D

Default User

Old said:
Top-posting was correct in this case.

Impossible, as top-posting is never correct. If the quotes weren't
relevant to the reply, then they didn't belong at all. And of course,
you're wrong because the person was replying directly to something in
the quotes, i.e. the solution to the problem.

I try to put "- TPA" in the header when I post this message. If it
bothers you, consider filtering it.




Brian
 

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

Similar Threads

Filter sober in c++ don't pass test 0
C program: memory leak/ segmentation fault/ memory limit exceeded 0
Crossword 2
TF-IDF 1
Crossword 14
Need help with this Python code. 2
Fibonacci 0
Quick sort algorithm 1

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top