Avatar of anubav
anubav

asked on 

Segmentation Fault Error

I've got a question concerning a segmentation fault error that I receive when I try to run a little app that I wrote. I'm new to coding in C++ and this particular error has really got me stumped (I've spent quite a while in gdb trying to make sense of what's going on).

I've attached the relevant snippet of code. The problem has to do with the function quad_obj::read which is basically just a function for reading data in from a .obj file and structuring it in a way that makes it easy for me to work with.  The problem arises from the last "for" loop (which is very pared down but still results in the error in question).

Here's what's confusing me. If in place of the last  "cout" command I put either (a) "cout<<face_list[i][0];"
or (b). "cout<<vert_list[1][0]", everything runs fine; but when I try to use "face_list[i][0]" (which is just an int) as a vector index, though the program compiles, I get a segmentation fault run-time error.  Even if I first copy "face_list[i][0]" into a new int variable - say g - and try to run "cout<<vert_list[g][0];" I get the same error.

I know that segfault errors generally have to do with illicit memory allocation, but I can't really see how the problem arises in this case. I'm sure it's something simple that I'm overlooking, but I'd appreciate whatever advice you can give me (plus, if you could tell me a little more about what, in general, causes segfault runtime errors, that would be really helpful to me).

Thanks!


#include "quad_obj.h"
#include <GL/gl.h>
#include<iostream>
#include<fstream>
#include<vector>
#include<cctype>
#include<string>
#include<cstdlib>
 
using namespace std;
 
typedef vector<GLdouble> coords;
typedef vector<coords> points;
typedef vector<vector<int> > faces;
 
/**************************************************************************
0. Auxiliary Functions (undeclared in header obj.h)
**************************************************************************/
bool space(char c)
 
{
 
    return isspace(c);
 
}
 
 
bool not_space(char c)
 
{
 
	return !isspace(c);
 
}
bool slash(char c)
{
	return c=='/';
}
bool not_slash(char c)
{
	return !(c=='/');
}
 
/**************************************************************************
1. quad_obj Class Function definitions
**************************************************************************/
//Reads a wavefront .obj file into a format that is easily drawn using GL vert array operations.
 
void quad_obj::read(const char* filename)
{
	//set the input filestream to the appropriate file. 
	ifstream in(filename);
	points vert_list;
	points norm_list;
	faces face_list;
 
	//first read enumerated lists of vertices, norms and faces. 
	while (in)
	{
		string line, prefix;
		getline(in, line);
		string::iterator i=line.begin();
		string::iterator j=find_if(i, line.end(), space);
		copy(i, j, back_inserter(prefix));
		i=find_if(j,line.end(),not_space);
		
		if(prefix=="v")
		{
			//store data in vert_list
			coords p;				
			while (i!=line.end())
			{
				string sub;
				j=find_if(i, line.end(), space);
				copy(i, j, back_inserter(sub));
				GLdouble d = atof(sub.c_str());	
				p.push_back(d);			
				i=find_if(j,line.end(), not_space);
			}			
			vert_list.push_back(p);	
		}
		
		else if (prefix=="vn")
		{
			//store data in norm_list
			coords p;				
			while (i!=line.end())
			{
				string sub;
				j=find_if(i, line.end(), space);
				copy(i, j, back_inserter(sub));
				GLdouble d = atof(sub.c_str());	
				p.push_back(d);	
				i=find_if(j,line.end(), not_space);
			}			
			norm_list.push_back(p);	
		}
		else if (prefix=="f")
		{
			//store data in face_list
			while(i!=line.end())
			{
				vector<int> f;
				string sub1, sub2;
				j=find_if(i,line.end(),slash);
				copy(i,j,back_inserter(sub1));
				f.push_back(atoi(sub1.c_str()));
				i=find_if(j,line.end(),not_slash);
				j=find_if(i,line.end(),space);
				copy(i,j,back_inserter(sub2));
				f.push_back(atoi(sub2.c_str()));
				face_list.push_back(f);				
				i=find_if(j,line.end(),not_space);
			}
						
		}
		else;		
	}
	
	//use vert_list, norm_list and face_list to order verts and norms
	for (faces::size_type i=0; i!=face_list.size(); ++i)
		cout<<vert_list[face_list[i][0]][0];		
			 	
}

Open in new window

C++

Avatar of undefined
Last Comment
anubav

8/22/2022 - Mon