Program fails with return code 128

Started by
15 comments, last by Antheus 14 years, 2 months ago
Hey everyone. I'm having an interesting problem that I am hoping somebody can help me with. I am trying to look at a map that has only water and land. I created this function that sets up all water set to 0 and all land set to 1. I am now trying to go through each cell and call the below function to identify separate land masses. It seems to work for maps 64x64, 128x128, 256x256, but dies with a return code of 128 when the map is 512x512. This function is called on every cell.

int followLand(std::vector <std::vector <unsigned int> > & tmpmap, unsigned int x, unsigned int y, unsigned short group)
{
	std::cout << "Following land\n";
	fflush(stdout);
	if(tmpmap[x][y] == 1)
	{
		tmpmap[x][y] = group;
		std::cout << "X: " << x << "\n";
		std::cout << "Y: " << y << "\n";
		std::cout << (x-1+tmpmap.size())%tmpmap.size() << "\n";
		std:: cout << (y-1+tmpmap.size())%tmpmap.size() << "\n";
		fflush(stdout);
/*Dies between here but before going inside the function of the next line.*/
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah1\n";
		fflush(stdout);
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), y, group);
		std::cout << "Ah2\n";
		fflush(stdout);
		followLand(tmpmap, (x-1+tmpmap.size())%tmpmap.size(), (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah3\n";
		fflush(stdout);
		followLand(tmpmap, x, (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah4\n";
		fflush(stdout);
		followLand(tmpmap, x, (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah5\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), (y-1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah6\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), y, group);
		std::cout << "Ah7\n";
		fflush(stdout);
		followLand(tmpmap, (x+1+tmpmap.size())%tmpmap.size(), (y+1+tmpmap.size())%tmpmap.size(), group);
		std::cout << "Ah8\n";
		fflush(stdout);
	}
	std::cout << "Done Following land\n";
	fflush(stdout);
	return 0;
}

There is a lot of cout in the function for debugging purposes right now. The interesting thing is that the last line out for the cout is the line indicated instead of a line inside the function. I can't figure out what's going on. It seems to often die in the middle of the map; it's definitely not a segment fault. Interesting thing is when compiled in linux the program runs fine, but when compiled in windows with minGW it fails. Any ideas? Any recommendations for a better way to do this would be nice too. Thanks, -Zael-
Advertisement
Not sure what your issue is. But did you know?
std::cout << "Following land\n";fflush(stdout);

can be better written as:
std::cout << "Following land" << std::endl;


There is really only one way that your program would exit with the value 128 anyway. You either have a "return 128;" at the end of main. Or you or a library you used called "exit(128)".
Does the std::endl automatically do the fflush part? I knew it was an alternative for "\n", but if it does the fflush part to then I will start using it.
Quote:Original post by Zael
Does the std::endl automatically do the fflush part? I knew it was an alternative for "\n", but if it does the fflush part to then I will start using it.

Yes it does. Whenever you don't want that behavior, just use '\n' instead.

Some more stream manipulators, if you're interested.
Quote:Original post by KulSeran
There is really only one way that your program would exit with the value 128 anyway. You either have a "return 128;" at the end of main. Or you or a library you used called "exit(128)".


Hm... The only library being used in that function is iostream and vector. SDL is used in the program, but it isn't even initialized until after this function returns.
Is your map square? If not, I could see a potential problem. For both x and y you are keeping things in range by using the % operator with tmpmap.size(). The thing is, if tmpmap[0].size() were smaller than tmpmap.size(), you might have an out of bounds access.

[EDIT]
My bad, I just noticed you mention square maps in your original post. Keep this in mind for the future though :)
Yeah. The maps are all square. I might try non-square at somepoint (and make the appropriate fixes), but right now I don't really care about non-square rectangles.
At a guess, you are getting a stack overflow. Linux usually has larger stack sizes than Windows by default.
Quote:
It seems to often die in the middle of the map;

Sorry i didn't see that before. You have a recursive function there.
If it is dieing in the middle of the map, you probably ran out of stack space due to recursing too deep.
You can likely rewrite the whole function as a while() loop on a std::stack.
each "followLand" call would push a new item into the stack. Each time through the loop, you pop an item, check "if(tmpmap[x][y]==1)" and either push more items, or loop and pop again.


--edit: mattd beat me to that realization.
Quote:Original post by KulSeran
Quote:
It seems to often die in the middle of the map;

Sorry i didn't see that before. You have a recursive function there.
If it is dieing in the middle of the map, you probably ran out of stack space due to recursing too deep.



Hmm... Maybe, but the program never goes above 1% total memory usage according to top in linux. Anyway to increase stack space for a program?

Or maybe a better question is: Is there a better way to do what I am trying to accomplish?

This topic is closed to new replies.

Advertisement