c++ Bug

Started by
9 comments, last by NIm 18 years, 10 months ago
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]
Advertisement
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....
Are you checking the return value of new if its heap memory? or if its stack are you sure you're not running out of space?
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
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].wall=1;} //it's etither this line
else
{map[j].wall=0;} //or this line that segfaults
map[j].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[j+1].wall==0 && map[j-1].wall==0) ||
(map[j+1].wall==1 && map[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.
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]. Stick with a[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).
Here's a visual of what Illco has said [wink]

[source] Your text here [/source]

Your text here 


[code] Your text here [/code]

Your text here 
And now Illco's wondering how to include the word source within square brackets without making it source...
Aren't you supposed to be indexing from 0 rather than from 1 in your for loops? :D
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.

This topic is closed to new replies.

Advertisement