First of all I am not proficient programing, so please be lenient. 🙂
I was curious what causes the error called “Stack overflow”. I am using Visual C++ 2010 Express.
struct elem
{
BITMAP * colltile;
elem * next;
};
/* put some code here */
int collision_map (unsigned int poz_x, unsigned int poz_y)
{
elem * wsk = this->where_the_head_of_list_is;
int x,y;
x = poz_x%64; //coord x on tile (0-63px)
y = poz_y%64; //coord y on tile (0-63px)
poz_x /= 64; //preparing poz_x and poz_y to point on a tile on a grid
poz_y /= 64; //integers do not have to be floored
//for (int j=(poz_y*(this->size_x)+poz_x); j>0; j--) //normally works... but
for (int j=0; j<1000; j++) //this version is not
{
if ((!(wsk = wsk->next)) ||
((poz_x+1) > this->size_x) ||
((poz_y+1) > this->size_y))
{ //should check if there is no new pointer or just out of map
return -1;
}
}
return getpixel(wsk->colltile, x, y);
}
Why is the condition not working when j reaches the value of length of the list?
Things you can/have to do:
std::cerr << wsk << std::endlin your code and check if/when it becomes zero.elem* wsk= .... If this is the casewsk->nextwill access a null pointer. You have to check if the pointer is valid.this->where_the_head_of_list_is) you need to implement copy constructor, assignment operator and destructor, which you might not have done. See here for an explanation.