473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help please ! (Segmentation fault while sorting a vector)

1
Hi,
I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the x-coordinate. I am trying to use the "sort" function defined in algorithms.h and I am getting a segmentation fault. I have inlined the code below. Can someone please tell me what I am doing wrong ? I am compiling this code on linux ( fedora 6 x86_64 using gcc 4.0)
-
Many thanks
JWalker
PS: I am just learning C++ and STL perhaps I am making some fundamental mistake.
================================================== =====
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class _vertex{
public:
double x;
double y;
double z;
_vertex() {x=0.0, y =0.0; z =0.0;}
_vertex(double xa,double ya, double za){x=xa; y =ya; z=za;}
_vertex (const _vertex & b) {x = b.x; y=b.y; z=b.z;}
};

bool sortbyx(const _vertex& v1,const _vertex& v2){
if(v1.x<=v2.x)
return true;
else
return false;
}

void print(const _vertex& tmp){
cout<<tmp.x<<"\t"<<tmp.y<<"\t"<<tmp.z<<"\t"<<endl;
}

int main(){
vector<_vertex> nodes;

// read node information from files in vector "qvolist"
vector<string> qvolist;
qvolist =Read_qvofile_list("temp.txt");
for(int i =0; i<qvolist.size();i++){
ifstream qvofile;
string line;
int Nvertices, NFaces, NEdges;

qvofile.open((qvolist.at(i)).c_str());
cout<<(qvolist.at(i))<<endl;
if(qvofile.is_open())
{
getline(qvofile,line); // read dimensions dummy var
qvofile>>Nvertices>>NFaces>>NEdges;
_vertex tmp;
for(int j =0; j<Nvertices; j++){
qvofile>>tmp.x>>tmp.y>>tmp.z;
nodes.push_back(tmp);
}
qvofile.close();
}
else cout<<"unable to open qvo file"<<(qvolist.at(i)).c_str()<<endl;
}
// print list of vertices
for_each(nodes.begin(), nodes.end(),print);

//Sort vertices by x co-ordinate
sort(nodes.begin(), nodes.end(),sortbyx);<-----------segmentation fault here

================================================== ========
Jan 3 '07 #1
1 3024
The problem with your code is almost certainly with the part concerned with reading data from the file (or its a compiler bug). Without a definition for Read_qvofile_list, I can't tell if it's in there or not. I did notice, however, that you make no check for end-of-file in your for loop; that is bad practice, for if you opened a file that was too short, your program wouldn't behave as expected. This may be the source of your segfault; I have found that programs rarely segfault where the actual problem was.

As well, it is generally good C++ practice to have all data members private, and declare member functions to get and set them, not that this has any relavance to the segfault.
Jan 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
2
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ...
0
by: RLC | last post by:
Hello I am new to python SWIG. Recently I wrote a small program trying to import collada files(using colladadom) into python so I could use python cgkit to render them. However, during the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.