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

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

## 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 on other sites
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 on other sites
Quote:
 Original post by michael879I 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 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 on other sites
Moved to For Beginners.

##### 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 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 on other sites
o ok that makes sense, I was just a little insulted.