Page 1 of 1

Segmentation Fault?

Posted: Sun Mar 16, 2008 10:28 pm
by KG_Brad
Hi,

I'm rewriting the 2D rendering engine for my Action-RPG and I just ran into a pretty tough segmentation fault.

Code: Select all

int render_tile(int x,int y,BITMAP *img)
{
	draw_sprite(scroll,img,x,y);
	
	return 0;
}

int render_map(int mapnum)
{
	int i,t;
	int tilecount;

	for (i=0;i<MAXWIDTH;++i)
	{
		for (t=0;t<MAXHEIGHT;++t)
		{	
  			for (tilecount=0;tilecount<MAXTILES>tilemap[i][t];
			
			if (tilemap[i][t] == 1)
			{				
				tile[tilecount]->x = t*32;
				tile[tilecount]->y = i*32;
				
				render_tile(tile[tilecount]->x,tile[tilecount]->y,tile_img[0]);
			}
		}
	}
	
	return 0;
}
I'm not 100% sure which line is causing the segmentation fault, but I think it's the

Code: Select all

render_tile(tile[tilecount]->x,tile[tilecount]->y,tile_img[0]);
line.

Everything is initialized properly (as far as I can tell, anyway.)

If somebody could point out my error, it'd be much appreciated.

Thanks,
-KG_Brad

EDIT: For some reason the code box is modifying the actual source. PM me if you think you could help and I'll send you the actual source.

Posted: Mon Mar 17, 2008 6:06 pm
by MarauderIIC
Yeah. I'm assuming that your arrays or whatever are declared as, for instance, int array[MAXHEIGHT]. Also we seem to be missing the third part of the last loop, but I'm assuming it's the same kind of thing.

In the declaration case, MAXHEIGHT is 1-based. And when you go to access them, you have to use a 0-based form. So you need to make it

Code: Select all

 for (i=0;i<MAXWIDTH-1;++i)
...
      for (t=0;t<MAXHEIGHT-1;++t)
...
And what is

Code: Select all

for (tilecount=0;tilecount<MAXTILES>tilemap[i][t];
supposed to do? Did you mean...
tilecount < MAXTILES && MAXTILES > tilemap[t] ? I had to disable html for that so I'm assuming that your condition was something involving &&.

Not that that condition would make much sense, although it's apparent that was the part that was messed up (perhaps try posting with Disable HTML checked).

If speed is important, I would make MAXWIDTH and MAXHEIGHT declared as a zero-based form (iow declare them as one less than they are right now), and declare the arrays using MAXWIDTH-1 and compare in the iteration using < MAXWIDTH. That way you save the subtraction (because the condition is checked on each loop, you perform a subtraction every iteration). Or declare a second constant or something. :P

Anyway, hope most of that made sense, I'm not feeling well.
Also, IIRC, for your future reference, a segfault is because you access something that you're not supposed to access, generally you're trying to go out-of-bounds on an array.

Posted: Mon Mar 17, 2008 9:56 pm
by KG_Brad
I solved the problem. You can close this if you want...

Thanks, anyway, though! I'll keep that bit of knowlege for future reference.

Posted: Mon Mar 17, 2008 10:31 pm
by Arce
What was the problem?

Posted: Tue Mar 18, 2008 7:45 am
by Falco Girgis
I was looking over this yesterday at work and didn't think we were given adequate information (or code) to properly assess the situation.

Posted: Tue Mar 18, 2008 8:54 am
by KG_Brad
Yeah, sorry about that. I posted this when I was very tired so I didn't even think to post more info. I solved it, though.