Advertisement

new and delete commands

Started by May 13, 2001 12:25 AM
9 comments, last by JSCFaith 23 years, 9 months ago
Hey I am trying to use the 'new' and 'delete' with a class with a linked list. The problem is this:
    
//I have a class as follows.


class Bullet : public GameObject {

public:

	Bullet() {

		Type = BULLET;
		Damage = 5;
		DistanceFlown = 0;

		// Add to linked List

		if( pBulletFirst == 0 )
		{
			// this is the first node

			pBulletFirst = this;
			pBulletFirst->pBulletPrev = NULL;
		}
		else
		{

			pBulletLast->pBulletNext = this;
			this->pBulletPrev = pBulletLast;

		}

		pBulletLast = this;
		this->pBulletNext = NULL;
	
	}

	~Bullet() {

		if (this->pBulletPrev == NULL)
			pBulletFirst = this->pBulletNext;
		else
			this->pBulletPrev->pBulletNext = this->pBulletNext;
		if (this->pBulletNext == NULL)
			pBulletLast = this->pBulletPrev;
		else
			this->pBulletNext->pBulletPrev = this->pBulletPrev;

	}

	int Draw(LPDIRECTDRAWSURFACE7 PrimarySurface);
	static int DrawBullets(LPDIRECTDRAWSURFACE7 PrimarySurface);
	//. Sets the damage

	void SetDamage(int NewDamage) {
		Damage = NewDamage;
	}
	// Set Velocity

	void SetBulletVelocity(int Direction);
	// Gets the Damage

	int GetDamage() {
		return Damage;
	}
	Bullet *GetNextBullet() {
		return(pBulletNext);
	}
	static Bullet *GetFirstBullet() {
		return(pBulletFirst);
	}
	void CheckBulletCollisions();

	// Shoots a bullet

	static void CreateBullet(float x, float y, int Direction);
	static void DestroyBullet(Bullet *pBullet);
	
protected:
	// Linked list stuff

	static Bullet *pBulletFirst;
	static Bullet *pBulletLast;
	Bullet *pBulletNext;
	Bullet *pBulletPrev;

	// Bullet Damgae

	int Damage;
	static int Range;
	int DistanceFlown;

};

// End of the class


// Now when I try to use the 'new' command like this:


Asteroid *NewAsteroid;

// Create the new asteroid

NewAsteroid = new Asteroid; // When ever I do this the program crashes.  

// Am I using it incorrectly?


  
Well I tried to use the new command with a very simple class with no linked list and it still crashed. So I know it is not the class that is messed up. (Well I am 90% sure) Does anyone know what I am doing wrong? Thanks... [/source] Edited by - JSCFaith on May 13, 2001 1:26:38 AM
assuming your compiler isn''t buggy, look closely at your constructors and make sure you aren''t dereferencing NULL or uninitialized pointers. Usually it''s something like that...
Advertisement
Well I looked at my constructor and it looks fine, but to be safe I just commented out all the linked list code in both Bullet and GameObject and it still crashed. Any other ideas?

One more thing, what do you mean by Dereferencing NULL or uninitialized pointers.

Well, thanks.

Later.
Dereferencing a null pointer:
struct Something{  int member;};int main (){  Something *nothing = NULL; // nothing is null pointer  return nothing->member; // run-time error--access violation}; 

99% of access violations come from:
- dereferencing null pointer
- dereferencing uninitialized pointer
- dereferencing deleted pointers (will point do data of 0xDDDDDDDD in debug builds of MSVC)
- exceeding array bounds
Well,you might want to post the code for your Asteroid. Is your asteroid a seperate class, or is it a struct?

Edem Attiogbe
Edem Attiogbe
Oops, sorry. That is supposed to say:

    Bullet *NewBullet;// Create the bulletNewBullet = new Bullet;    


Edited by - JSCFaith on May 13, 2001 6:18:17 PM
Advertisement
If all reasoning fails, you can try to trace your program in a debugger or insert some diagnostic prints into your code to find out where it''s crashing. narrowing down the possibilities of what went wrong helps A LOT.


anonymous guy
Yeah, I was thinking of doing that and I think I will. Well thanks for the help guys.

Later and Thanks..
Maybe try posting your GameObject constructor too if you still can''t find the problem. It might not actually be in the constructor for Bullet.
If you are using MSVC6.0 try running it in debug mode, if you get an access violation at offset 0x00000005, or something like that, then your probably trying to use something before its initialized

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement