Sign in to follow this  
NIm

c++ Bug

Recommended Posts

NIm    210
I am writing a roguelike game using ncurses, and its been going fairly well, but I've hit something I just can't figure out how to debug. The program runs fine, untill I try to initialize a 200x200 array of structures. that procedes fine, untill it gets to the 200x153 row of the array, whereupon it segfaults. I realize that people have better things to do than debug my program, so just point me in the right direction, and I'll figure it out from there. I can e-mail someone code if this isn't enough information to figure out what's going on. Thank you for your time. [Edited by - NIm on May 24, 2005 4:43:53 PM]

Share this post


Link to post
Share on other sites
haora    108
You could post the peace of code that initializes that array.., here on the forum..., that way we can help you..., otherwise..., it's kind of imposible for us to know what your doing....

Share this post


Link to post
Share on other sites
snakeeater    122
if you are initializing the array like that
array[200][200]

you get an array from 0 to 199 in every dimension, so if you try to acces an item like eg
 array[200][anything]
you will get a segfault here

Share this post


Link to post
Share on other sites
NIm    210
Sorry.

This is part of the lines that I think are getting skipped.
mapmaxx and mapmaxy are both defined as 200.

struct cell map[mapmaxx][mapmaxy];


and here's the part that initializes the array:

for(i=1; i<mapmaxy; i++)
{
for(j=1; j<mapmaxx; j++)
{
if (dice (1,100)<=percentrock)
{map[j][i].wall=1;} //it's etither this line
else
{map[j][i].wall=0;} //or this line that segfaults
map[j][i].door=0;
}
}

The part that generates the segfault is the first line of this if statement, where i=199 and j=152:

if ((map[i+1][j].wall==1 && map[i-1][j].wall==1 && map[i][j+1].wall==0 && map[i][j-1].wall==0) ||
(map[i][j+1].wall==1 && map[i][j-1].wall==1 && map[i+1][j].wall==0 && map[i-1][j].wall==0))

another question: how do I indent stuff in this forum? It just deletes all spaces before a line.

Share this post


Link to post
Share on other sites
Illco    928
The array is bounded by maxx in the first dimension and maxy in the second. Then i loops until maxx and j until maxy. Then you should not access in switched mode like a[j][i]. Stick with a[i][j]. This is part of the error if maxx != maxy. Otherwise something else is going on.

Ok I see both are 200 now. You were also accessing the array like a[i+1][j]. If the bound is 200 then a[199][j] is the last element in the first dimension. Calling a[i+1][j] on it will give problems, because a[199+1][j] is a[200][j] which does not exist. I think you want your loop to loop until but not including the last element:


for ( i = 1; i < maxx - 1; i++ )
...


Greetz,

Illco

PS: you can include short indented pieces of code using the code tags (put code and slash code in square brackets). Larger pieces of code are included between source tags (just the same) and will be highlighted (woo).

Share this post


Link to post
Share on other sites
Illco    928
And now Illco's wondering how to include the word source within square brackets without making it source...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Aren't you supposed to be indexing from 0 rather than from 1 in your for loops? :D

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Illco
And now Illco's wondering how to include the word source within square brackets without making it source...


Escape one or more of the characters with an HTML entity.

Share this post


Link to post
Share on other sites
NIm    210
Thanks, I got it figured out. actually, the reason I've got it initialized from one is the same reason that I caused a segfault: the +1/-1 causes it to reach beyond the edge of the array. So, I put a -1 after max, and problem solved. Now I can go after the door placement code. Thank you all for being so helpful.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this