delete NULL

In my text game when the character has the same x and y co-ordinate as the enemy I am trying to delete them however this does not seem to be working as they will just move again even though they both landed on the same co-ordinates on that turn.

1
2
3
4
5
6
7
8
9
10
11
void Variables::killMonster(){
   //checks for collision, if collision = true assign the monster NULL and delete NULL. *Not working
   for (int d=1;d<10;d++){
   if (cList[d]!=NULL){
   if (cList[0]->x == cList[d]->x && cList[0]->y == cList[d]->y){
   cList[d]=NULL;
   delete cList[d];
   }      
  }
 }
}


Thanks for your time.
Any suggestions?
Last edited on
It looks like you mean && not &.

&& = logical AND
& = bitwise ADD (which you aren't trying to do here).
I did originally have &&, I just decided to try out & with no luck :(

sorry, i should have mentioned in the first post
Last edited on
I'm guessing that cList is a list of character pointers. If you assign the value of cList at position d = to NULL, you won't be able to delete the thing at that point. You'll want to delete it first, then assign it to NULL.
Hi pogrady thanks for your post, I just tried it and your suggestion did not work it just provided the same results as having the delete after assigning null
What pogrady said is the problem in this function. If it still doesnt work the problem is elsewhere
Debug function :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Variables::killMonster(){
   //checks for collision, if collision = true assign the monster NULL and delete NULL. *Not working
   for (int d=1;d<10;d++){
   if (cList[d]!=NULL){
   if (cList[0]->x == cList[d]->x && cList[0]->y == cList[d]->y){
   cList[d]=NULL;
   delete cList[d];
 cout <<"Done." << endl;
   }    
else // if (cList[0]->x != cList[d]->x || cList[0]->y != cList[d]->y)
   {cout <<  "Index ( " << d << " ) " << "cList[0]->x != cList[d]->x or cList[0]->y != cList[d]->y" << endl;
    cout <<  "cList[0]->x = " << cList[0]->x  << "cList[d]->x = " << cList[d]->x << endl; 
    cout <<  "cList[0]->y = " << cList[0]->y  << "cList[d]->y = " << cList[d]->y << endl; 
   }

	}
   else //if (cList[d]==NULL)
 cout <<"The pointer is null" << endl; 
   }
 cout <<"Failed." << endl;
}
Last edited on
This is a good idea. Otherwise you'll spend LOTS AND LOTS OF TIME DEBUGGING A SIMPLE @%^!@ ERROR THAT COULD HAVE BEEN CAUGHT.

Sorry, just ranting. lol. Had a similar situation occur.
Last edited on
Topic archived. No new replies allowed.