Archived

This topic is now archived and is closed to further replies.

Ozguxxx

Hopelessss, help needed...

Recommended Posts

Hi everybody. I am working on a linked list class and the following code snippet is a part of it. This code is supposed to remove an element of linked list. The element is specified by its index in the list. DXBullet is the class which represents a node in the linked list. getNextBullet() function simply returns DXBullet* lpNextBullet (next bullet''s pointer in the list). firstBullet is a variable (whose type is DXBullet*) in list class. Code seems to be working alright with a console application (under windows 98, Visual C++ 6.0) however,when I try to use this code with the game demo I am trying to implement, when the first bullet is removed, third bullet is also removed from the list. I mean, for instance you shoot three bullets, when the first bullet leaves the screen, it should be removed from list and its resources should be freed. But when the first bullet leaves screen third bullet that you shot also disappeares from screen.(I don''t know why). This should mean that when first bullet is removed from list, in some way, third bullet is also removed from te list. Please tell me everything that you can think of about the problem, I am very very hopelesssss. Any idea is welcome. Thank you. void removeBullet(int index) { DXBullet* currentBullet = this->firstBullet; if(index == 0) { if(this->numberOfBullets != 1) { DXBullet* newFirst = this->firstBullet->getNextBullet(); delete(this->firstBullet); this->firstBullet = NULL; this->firstBullet = newFirst; this->numberOfBullets--; } else { delete(this->firstBullet); this->firstBullet = NULL; this->numberOfBullets--; } } else { ...

Share this post


Link to post
Share on other sites
I didn't see that problem in your code, but here:

quote:

this->firstBullet = NULL;
this->firstBullet = newFirst;



It is pointless to set firstbullet first to NULL.

The bug you were searching for might be elsewhere in the program.

There's also another bug in your code:

quote:

if(this->numberOfBullets != 1)



If numberOfBullets==0, then your program is likely to crash.

Also, that is not the optimal design for a linked list..


[edited by - dr_w0rd on August 12, 2002 5:33:35 AM]

Share this post


Link to post
Share on other sites
My suggestion to you is not to reinvent the wheel. Linked lists is IIRC implemented in the STL. It saves you time and problems! Good luck with whatever you are doing:-)

Share this post


Link to post
Share on other sites