LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2006, 09:19 PM   #1
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Rep: Reputation: 15
pointer problems


Hello again friends and mentors.
I am having trouble with this bit of code .
I must misunderstand the way these pointers work .
Here is the code I have .
It is for a missile in my game .
I recently rewrote it so I am sure there are a lot of problems.
I'm mostly concerned with testing pointers and getting a class to delete itself.

Code:
 class explode
{
public:
int frame,worldposition;
static int count;
Mix_Chunk *sound;
SDL_Rect dest_rect;
SDL_Surface *pow[5];
explode(int x,int y)
	{
//   	Mix_HaltChannel(soundChannel);
 //   	soundChannel = -1;
 //   if(soundChannel < 0)
//		{
//		soundChannel = Mix_PlayChannel(-1, sound, -1);
//	   	}
	dest_rect.x=x;
	dest_rect.y=y;
	worldposition=(scrollx+x)/tilewidth;
	world[y/tilewidth][worldposition]='*';//for character collision detection
	pow[0]=IMG_Load("explosion/explode0.png");
	pow[1]=IMG_Load("explosion/explode1.png");
	pow[2]=IMG_Load("explosion/explode2.png");
	pow[3]=IMG_Load("explosion/explode3.png");
	pow[4]=IMG_Load("explosion/explode4.png");
	frame=0;
	}
//looped every frame
void boom()
	{
	if (frame>=5)
		{
		world[dest_rect.y/tilewidth][worldposition]='.';//for character collision detection
		frame=0;
		delete this;
		return;
		}
	else
		{
		SDL_BlitSurface(pow[frame],NULL,screen,&dest_rect);	
		frame++;
		}
	}
};

class missile
{
public:
int targety,speed;
static int count;
bool hit; 
bool direction;
explode *boom;
SDL_Rect  dest_rect;
SDL_Surface *miss;
missile(int x,int y,bool dir)
	{
	dest_rect.x=x;
	dest_rect.y=y;
	direction=dir;
	hit =false;
	speed=20;
	miss=IMG_Load("art/missile.png");	
	};
void fire()
	{
//	if(dest_rect.x<=0)delete this;
	if(!boom)
		{
		SDL_BlitSurface(miss, NULL, screen, &dest_rect);
		if (direction)dest_rect.x-=speed;
		else dest_rect.x+=speed;
		///rocket hit
		if(checkradius(&dest_rect,'!')!='.'){hit=true;boom=new explode(dest_rect.x,dest_rect.y);}
		}
	else
		{
		boom->boom();
		if(hit){delete boom;delete this;}
		}

	};

};
My debugger keeps quiting at if(!boom) when i call missile->fire().
Thanks for help.
 
Old 03-30-2006, 09:21 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I didn't look at your code carefully ....
... but where do you initialize your member "explode *boom"?

I mean "initialize the pointer to NULL, before you actually create a new instance of explode?"

SUGGESTION:
Code:
Missile::Missile(int x,int y,bool dir)
  : boom(0)
{
	dest_rect.x=x;
	dest_rect.y=y;
	direction=dir;
	hit =false;
	speed=20;
	miss=IMG_Load("art/missile.png");	
}

Last edited by paulsm4; 03-30-2006 at 10:58 PM.
 
Old 03-30-2006, 09:23 PM   #3
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
I'm afraid to give an answer right now since I can be very wrong .. when you say "quits" .. what do you mean ?? what's the error?

--- Edit:

Is this the full code?.. if you don't have a constructor.. or don't initialize boom anywhere .. you're at least lucky if it's always null .. although I'd expect the behaviour ot be undefined

Last edited by cupubboy; 03-30-2006 at 09:24 PM.
 
Old 03-30-2006, 09:28 PM   #4
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
Quote:
Originally Posted by paulsm4
I didn't look at your code carefully ....
... but where do you initialize your member "explode *boom"?

I mean "initialize the pointer to NULL, before you actually create a new instance of explode?"

SUGGESTION:
Code:
Missile::Missile(int x,int y,bool dir)
  : explode(0)
{
	dest_rect.x=x;
	dest_rect.y=y;
	direction=dir;
	hit =false;
	speed=20;
	miss=IMG_Load("art/missile.png");	
}
You mean ": boom(0)"
 
Old 03-30-2006, 09:34 PM   #5
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143

Original Poster
Blog Entries: 8

Rep: Reputation: 15
I didn't initialize the pointer to null I was assuming it would be null after I delete it,am i wrong?
The error is "Program received signal SIGSEGV, Segmentation fault."
 
Old 03-30-2006, 09:37 PM   #6
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
Quote:
Originally Posted by cyb0rg777
I didn't initialize the pointer to null I was assuming it would be null after I delete it,am i wrong?
The error is "Program received signal SIGSEGV, Segmentation fault."
Well you didn't initialize it to anything ...
It's good to initialize it to NULL in the constructor so that even if you don't assign it to anything else along the way .. you can do a check in the destructor for example:

if (boom != NULL)
delete boom;

I don't know what the behaviour is if you don't initialize it at all .. the result should be undefined .. and that's probably why you get a segfault when running the program

Also somewhere along the way in you're program you're going to have to do a boom = new explode(...) .. in order to use boom properly

-- Edit
Wich will also raise new problems because of the explode class .. and the pointers in it

Last edited by cupubboy; 03-30-2006 at 09:43 PM.
 
Old 03-30-2006, 11:02 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, cyb0rg777 -

No - the pointer won't magically become null after you delete it. Nor is it magically null before you initialize it. It's definitely good practice, however, to do both manually. In general, you should *ALWAYS* initialize a pointer when you declare it, and I'd strongly urge you to make a habit of setting it to null after you "delete" or "free()" it.

Your .. PSM

Last edited by paulsm4; 03-31-2006 at 11:23 AM.
 
Old 03-31-2006, 06:25 AM   #8
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143

Original Poster
Blog Entries: 8

Rep: Reputation: 15
Please bear with me if you will.
Can I overload delete operator like this.
Code:
void operator delete(void *p)
{
if (p!=NULL)
	{
	free(p);
	p=NULL;
	}
}
And The second part of my question.
What about the This pointer?
If I call "delete this" from inside explode->boom() Does explode disapear?And will it set itself to NULL?
 
Old 03-31-2006, 06:54 AM   #9
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
You can't delete this .. it makes no sense in my opinion .. your object will get destructed when it goes out of scope ... or as soon as it is not needed anymore if it's an automatic object. Your explode pointer (boom) get's initialized befor the body of Missile's constructor gets executed .. if you assign it a diffrent meaning somewhere along the life time of your missle object (like boom = new explode(..)) .. It is your job to delete boom. If you don't the memory ocupied by boom will be lost to the program forever.

It's important to know how objects get constructed/destructed .. unfortunatly I'm not really up2date on that
A few things that I can remember is that (I hope I'm right)
- global objects get constructed when the program begins and get destructed after main returns
- a local object get's destructed when it goes out of scope for example:

void function SomeFunc()
{
MyClass A; //A gets constructed
{
MyClass B;//Be gets constructed
}//At this point B's destructor is called
return;
//Now A's destructor is called
}

- automatic objects get destructed as soon as they are no longer needed
for example
{
MyClass A; //A's constructor gets called
MyClass B; //B's constructor gets called
MyClass C = A + B; // What happens here is that a temporary (automatic ??!) object gets constructed form A + B then C's copy constructor is called with the temporary object as a parameter .. C is constructed
//The temporary object's destructor gets called
}
// A,B,C's destructors get called (Actually C,B,A in that order)

objects created on the free store (eg. with the new operator) get destructed when the user chooses so for example
void SomeFunc()
{
MyClass * A = new MyClass(); //A's constructor gets called
//do something with A
delete A; //A's destructor is called

}

compared to:

void SomeFunc()
{
MyClass * A = new MyClass(); //A's constructor gets called
//do something with A
}

In the latter case .. because A is a local variable after the function returns we have no more access to it .. since we didn't delete A (wich also calls A's destructor) .. the memory used by A is lost to us forever in this program

Point is Destructors are a means for the programmer to cleanup all memory used by an object

There are a few things I didn't go into .. because I don't remember them that well .. and I'd rather not say silly things (presuming I haven't already)

Cheers

Last edited by cupubboy; 03-31-2006 at 06:55 AM.
 
Old 03-31-2006, 11:21 AM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's only NULL if you make it NULL...

cyb0rg777 -

1. Yes, you can definitely override the C++ "delete" operator (and/or the corresponding C++ "new" operator).

2. However: "delete" != "free()", and "new" != "malloc()".
The C++ "new" and "delete" operators are absolutely, completely different from the C library functions "malloc()" and "free()". You CANNOT - absolutely CANNOT - mix'n'match them at will.

3. Finally, neither "delete" nor "free()" set the pointer to null.
The only way ANY pointer is EVER set to "0" is if you do it yourself.

4. Setting your pointers to NULL is a good habit I'd encourage you to get into.

Some might say it's a waste of machine cycles. I would disagree.

I strongly believe that getting into the habit of explicitly initializing every pointer variable to "0" before you assign it, and then setting it to "0" after you've freed it, is both good discipline (that leads to better code), and very inexpensive insurance (that leads to more robust, less likely to fail, easier to troubleshoot) programs.

Your .. PSM

PS:
Yes, "delete this" is both acceptable C++ and a common idiom. You can indeed do it.

PPS:
Yes, you can overload the "delete" operator. And no, you shouldn't do it like you did. The justification for overriding "new" and "delete" would be for your own memory management scheme (something you're very likely to do in, say, game programming). But your "delete" is, frankly, just wrong. I'd worry about other things (like getting your basic program working) for now.

Last edited by paulsm4; 03-31-2006 at 11:30 AM.
 
Old 03-31-2006, 11:31 AM   #11
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143

Original Poster
Blog Entries: 8

Rep: Reputation: 15
O.K. Thanks for explaining.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
eGalax Touchscreen and xorg: Driver problems (No core pointer) superhausi Linux - Hardware 5 08-03-2004 03:20 PM
linux mouse pointer problems najeeb_a_99 Linux - Newbie 0 10-06-2001 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration