470,850 Members | 1,029 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,850 developers. It's quick & easy.

Help please ! (Segmentation fault while sorting a vector)

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
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{
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){
return true;
return false;

void print(const _vertex& tmp){

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;

getline(qvofile,line); // read dimensions dummy var
_vertex tmp;
for(int j =0; j<Nvertices; j++){
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 2686
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

Post your reply

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

Similar topics

3 posts views Thread by diyanat | last post: by
5 posts views Thread by Fra-it | last post: by
1 post views Thread by samuel.y.l.cheung | last post: by
reply views Thread by RLC | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.