Jump to content
  • Advertisement
Sign in to follow this  
michael879

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

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.size(); j++)
//		{
//			out << ' ' << foo.m_mTrack[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[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
Advertisement
I don't know if it's the only problem but you're definately missing a return out; at the end of the operator<< function.

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
Just a guess but it may have been moved to the beginner forum because a lot of beginners might run in to this kind of trouble and now they will know how to fix this kind of error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!