Segmentation Fault?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
KG_Brad
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sun Mar 16, 2008 10:03 pm

Segmentation Fault?

Post 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.
PROJECTS: Action-RPG, SWORDS (Shooter)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
KG_Brad
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sun Mar 16, 2008 10:03 pm

Post 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.
PROJECTS: Action-RPG, SWORDS (Shooter)
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Post by Arce »

What was the problem?
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
KG_Brad
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sun Mar 16, 2008 10:03 pm

Post 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.
PROJECTS: Action-RPG, SWORDS (Shooter)
Post Reply