Sign in to follow this  
michael879

another insane std::vector bug (C++)

Recommended Posts

ok this one is just bizarre. It kind of resembles my last post but Im almost positive they have different causes. Anyway here is the function:
ostream &operator<<(ostream &out, const Foo &foo)
{
	out << foo.m_sName << endl;
//	const vector<vector<char> > &track (foo.m_mTrack);
//	const vector<vector<char> > track (foo.m_mTrack);
	for(unsigned int i = 0; i < foo.m_mTrack.size(); i++)
	{
//		for(unsigned int j = 0; j < foo.m_mTrack[i].size(); j++)
//		{
//			out << ' ' << foo.m_mTrack[i][j];
//		}
		out << endl;
	}
}

Foo::Foo(char const * const file, const string &name)
	:m_sName(name)
{
	ifstream in(file);
	unsigned int rows;
	unsigned int cols;
	in >> rows >> cols;
	m_mTrack = vector<vector<char> >(rows,vector<char>(cols,'.'));
	for(unsigned int i = 0; i < rows; i++)
		for(unsigned int j = 0; j < cols; j++)
			in >> m_mTrack[i][j];
}

Im using the following file:
10 10
........##
.....##.##
.###.##.##
..##.##..F
...#.#####
##.#...###
##.##..###
#..#...###
#..#..####
#SS#.....F
Ok heres my problem. This code looks fine to me. However I get a segmentation fault right at the end of the operator<< function. It happens at the very end. All of the commented code was originally uncommented. I logically thought that the error was coming from track's deconstructor. However commenting out track and simply using the Foo member function gives the same error! If I copy this code into the Foo constructor after the initialization I get no errors. However if I call cout << *this; in the constructor I do get the segmentation fault. Another thing that just confuses the hell out of me is that if I get rid of for loop and replace it with just a cout << foo.m_mTrack.size(); I GET NO ERRORS!? I must be missing something but I just cant find it. This makes absolutely no sense to me..

Share this post


Link to post
Share on other sites
Quote:
Original post by michael879
I must be missing something but I just cant find it. This makes absolutely no sense to me..


One easy way to find this sort of error is to enable warnings in your compiler. FOr GCC, that's -Wall -W. Make sure you pay attention to everything your compiler tells you.

--smw

Share this post


Link to post
Share on other sites
I would even go as far as advise -W -Wall -ansi -pedantic for g++.

In Visual Studio, I suggest enabling all warnings up to level 4, and also enabling the "treat warnings as errors" option (which does absolute wonders for your motivation to correct warnings).

Share this post


Link to post
Share on other sites
why was this moved to for beginners? Im not a beginner, and Im pretty good at debugging normally. I spent hours and couldnt find the problem. That kind of suggests its not a beginner bug doesnt it?

however, still agreeing with the fact that it shouldnt have been moved, I think the first reply was right. That would explain everything. It casts the ostream & return to an (int)0 and if I say cout << Foo() << endl it would throw a segmentation fault. Thanks for the help.

o, and I do compile in g++ in debug mode with all of the warning options. None of those picked this up, how f*cked up is that?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this