Segmentation Fault? [Solved]

Started by
1 comment, last by MrPickle 15 years, 11 months ago
Firstly, what is a Segmentation Fault? Secondly, why is this code giving me a "Segmentation Fault" The specific function that's causing me to get this error is Terrain::Terrain(const string &Path), all the others are called by or related to the constructor.
using namespace std;

string getWord(string Line, int Position)
{
    stringstream ss(Line);
    vector<string> vec;
    string Word;
    while( ss >> Word )
        vec.push_back(Word);
    
    try {
	return vec.at(Position);
    } catch(out_of_range) {
	return "0";
    }
}

int toInt(string Number){
	std::istringstream i(Number);
	int x;
	if (!(i >> x))
		throw 0;
	
	return x;
}

float toFloat(string Number){
	std::istringstream i(Number);
	float x;
	if (!(i >> x))
		throw 0;
	
	return x;
}

class Terrain{
	private:
		int Width, Length;
		float **Heights;
	
	public:
		Terrain(const std::string &Path);
		~Terrain(void);
		int getWidth(void){ return Width; }
		int getLength(void){ return Length; }
		float getHeight(int x, int z){ return Heights[z][x]; }
		void setHeight(int x, int z, int y){ Heights[z][x] = y; }
		void Draw(void);
};

Terrain::Terrain(const string &Path){
	int Position = 0;
	ifstream File;
	string Line;
	File.open(Path.c_str(), ios::in);
	
	if(File.is_open()){
		while(!File.eof()){
			getline(File, Line);
			if(Position == 0){
				Width = toInt(getWord(Line, 0));
				Length = toInt(getWord(Line, 1));
				
				Heights = new float*[Length];
				for(int i = 0; i < Length; i++){
					Heights = new float[Width];
				}
				
			} else {
				for(int i = 0; i < Width; i++){
					Heights[Position - 1] = toFloat(getWord(Line, i));
				}
			}
			Position++;
		}
	}
}

Terrain::~Terrain(void){
	for(int i = 0; i < Length; i++){
		delete[] Heights;
	}
	delete[] Heights;
}


[EDIT] According to wiki - "A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). I don't see why this is giving me the error though, because it was working earlier. [Edited by - MrPickle on May 10, 2008 6:38:22 PM]
Here to learn :)
Advertisement
the error is probably here: Heights = new float*[Length]; or here: Heights[Position - 1] = toFloat(getWord(Line, i));
make sure you're not writting out of bonds of the array, also check if its being properly inicialised. use the debugger to check in what line is actually blowing the program.

ps: this was probably better on the "general programming" section.
yet, another stupid signature..
Yeah, I think it should be in that forum too :P

It is this line: Heights[Position - 1] = toFloat(getWord(Line, i));
Position - 1 gets too high, I've fixed it by adding a check whether it was less than Length.

Thanks :)
Here to learn :)

This topic is closed to new replies.

Advertisement