function reads true regardless???

checkexplode reads true even if nothing is found. collisionhandler::checkexplode searches through vector of player objects, searching for next character that is not on your team (int good). I don't understand how it reads true in rocket::checkexplode but still returns null pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int collisionhandler::checkexplode(collarea *area,int good)
{
	int s=0;
	int size = players.size();
	if(internal_ptr<size)
	{
		while(players[internal_ptr].good==good)
		{
			internal_ptr++;
		}
		if(internal_ptr<size)
		{
			area = playerarea[!good];
			area->setarea(players[internal_ptr].x,players[internal_ptr].y,players[internal_ptr].theta);
			internal_ptr++;
			s=1;
		}
		else {
			internal_ptr=0;
		}
	}
	else 
	{
		internal_ptr=0;
	}
	return s;
}


called in checkexplode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void rocket::checkexplode()
{
	printf("in rocket, checkexplode\n");
	collarea *a=0;
	int g = rockets.size();
	while(c->checkexplode(a,good))
	{
		if(a==0)
		{
			printf("\n\n\ncheckexplode read true but area is null\n\n");
		}
		//fprintf(stdout,"in for loop, c->checkexplode read true\n");
		for(int i=0;i<g;i++)
		{
			rk temp = rockets.at(0);
			rockets.erase(rockets.begin());
			if((!temp.seq)&&(a!=0))
			{
				float x=temp.x;
				float y=temp.y;
				float xx,yy;
				a->toxy(&xx,&yy,a->getbl());
				float bx,by;
				a->toxy(&bx,&by,a->getbr());
				xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
				yy = (abs(y-yy)<abs(y-by)) ? yy : by;
				a->toxy(&bx,&by,a->gettl());
				xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
				yy = (abs(y-yy)<abs(y-by)) ? yy : by;
				a->toxy(&bx,&by,a->gettr());
				xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
				yy = (abs(y-yy)<abs(y-by)) ? yy : by;
				float dd = sqrt((x-xx)*(x-xx) + (y-yy)*(y-yy));
				if(dd < killdist)
				{
					temp.seq=1;
					handler->createexplosion(exptype,x,y);
					c->eraserocket(temp.idd,good);
				}
			}
			rockets.push_back(temp);
		}
	}
}


you can guess what the terminal output is.
Last edited on
closed account (j2NvC542)
You can guess what the problem is.
Provide full code.
You are passing the pointer by copy
By the way, there is a bool type

Also, you may step out of bounds in the 7--10 loop.
I don't know if this is your problem or not, but you are stepping out of bounds of your vector and reading from garbage memory:

In collisionhandler::checkexplode
1
2
3
4
		while(players[internal_ptr].good==good)
		{
			internal_ptr++;
		}


This loop never checks to make sure internal_ptr stops at the end of your 'players' vector. So you will be spilling over past the end of the vector and reading garbage data (and potentially even getting an access violation and crashing the program).

I also don't see why you have internal_ptr at all, here. Why not just keep it local to the function? Then you don't have to deal with wrapping at all.
I eventually got around it by checking if area == 0 and doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int collisionhandler::checkexplode(collarea *area,int good)
{
	printf("In collisionhandler, checkexplode ,players.size(): %i\n",(int)players.size());
	int s=0;
	if(internal_ptr<players.size())
	{
		int found=0;
		while((internal_ptr<players.size())&&!found)
		{
			printf("In collisionhandler, checkexplode while loop, internal_ptr: %i\n",internal_ptr);
			if((players[internal_ptr].good==(!good))&&(internal_ptr<players.size()))
			{
				found=1;
			}
			else
			{
				internal_ptr++;
			}
		}
		if(found)
		{
			fprintf(stdout,"collisionhandler, checkexplode, found is true. internal_ptr: %i\n",internal_ptr);
			s=1;
			area = playerarea[!good];
			fprintf(stdout,"area: %p, playerarea[!good]: %p\n",area,playerarea[!good]);
			fprintf(stdout,"x: %f, y: %f, theta: %f\n",players[internal_ptr].x,players[internal_ptr].y,players[internal_ptr].theta);
			area->setarea(players[internal_ptr].x,players[internal_ptr].y,players[internal_ptr].theta);
			internal_ptr++;
		}
		else
		{
			internal_ptr=0;
		}
	}
	else
	{
		internal_ptr=0;
	}
	fprintf(stdout,"in checkexplode, area: %p, s: %i\n",area,s);
	return s;
}


but that code failed to. luckily the if statement check in rocket saves the program. I'm just baffled I can't write a simple function. Whatever. Thanks for being patient guys. I'd like to think I was having one of those days where you should've just stayed in bed but I seem to be having quite a lot of those...

You are passing the pointer by copy


Ok exposing ignorance here but... why is that a bad thing? I only want a shallow copy to save space. Is that stupid? Is that not what I'm doing?
Last edited on
1
2
3
	while(c->checkexplode(a,good))
	{
		if(a==0)
You pass the pointer by copy, but you expect to be modified by the function. Obviously that is not going to work
You could pass it by reference int collisionhandler::checkexplode(collarea *&area,int good)

¿What is the problem with your new code?
Last edited on
The code as it stands, technically works. But collisionhandler::checkexplode reads true and returns a null pointer before reading false, requiring me to use the if statement if(a==0) as a safety check. I don't understand why that happens.

I thought I would only need to pass a pointer by reference if I want to change where the pointer itself points to.

How my code works is collisionhandler is a global object that collects pointers to collarea objects used by both players and weapon classes for easier collision detection (and in this case detect how far away an enemy is to a rocket). a pointer passed by copy still allows me to access the original object stored in memory and simply use and reuse. collarea has a method set(float x, float y, float theta) that helps with collision detection on the screen.

I don't understand why I would need to create a copy of said object (or create a new one) if I can simply reset it by using that function. Afterall, collarea on initialization needs to be set by passing a pointer of a bitmap to set collision points.

EDIT: OK I see where you're going with this. I pass in a pointer by copy the best I can hope for is to dereference it (which I can't) and make it equal to collarea object whereas with a double pointer I can avoid all that and pass the address. bulb just went on. That was the issue! Thanks ne555, I don't care what they say you're a good guy.
Last edited on
Topic archived. No new replies allowed.