segfault on array?

OK so I'll get right to it:

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
unsigned long* collisionhandler::checkpellethit(int good, int *n)
{
	*n=0;
	unsigned long *dead = new unsigned long[weapons.size()];
	int q = weapons.size();
	for (int i=0;i<q;i++)
	{
		printf("in collisionhandler, checkpellethit\n");
		if(weapons[i].type==3)
		{
			int r = players.size();
			for(int j=0;j<r;j++)
			{
				if(players[j].good!=good)
				{
					playerarea[!good]->setarea(players[j].x,players[j].y,players[j].theta);
					shotguns[good]->setarea(weapons[i].x,weapons[i].y,weapons[i].theta);
					if (playerarea[!good]->didhit(*shotguns[good]))
					{
						dead[*n]=weapons[i].idd; // right here
						weapons.erase(weapons.begin()+i);
						//players.erase(players.begin()+j);
						player a = players[j];
						players.erase(players.begin()+j);
						a.hit=1;
						players.push_back(a);
						*n++;
					}
				}
			}
		}
	}
	return dead;
}


From terminal:
1
2
3
4
5
6
7
8
9
10
11
in collisionhandler, checkpellethit
in collisionhandler, checkpellethit
in collisionhandler, checkpellethit
in collisionhandler, checkpellethit

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000003fe45e200
[Switching to process 17350]
0x0000000100006b64 in collisionhandler::checkpellethit (this=0x10042fff0, good=0, n=0x1029dba80) at collisionhandler.cpp:273
273							dead[*n]=weapons[i].idd;


when my shotgun fires, there are around 60-70 pellets generated and supposed to show up on screen. I'm a little confused what would cause this, and terminal indicates its going through the loop at least 3 full times before something happens. Also how I have it set up player 1 is the only character on screen so technically the inner if statement should never read true.
Obfuscate it a little more, please
Checkout operator precedence

Learn to use a debugger
closed account (zb0S216C)
It looks like you're overstepping the boundaries of an array somewhere. Whenever you exceed the boundary of an array, the OS will react by sending a signal to your program, which indicates a read/write operation on memory that doesn't pertain your program. This includes read/writing to address 0x000... (considered the null address).

From what I can see, two thing's could be causing your problem:

• You're dereferencing n when NULL was given to it. I eliminated this due to the fact that you dereferenced n before the troublesome line.

• The int pointed-to by n contains an index which exceeds the largest index of dead.

The debugger is there for a reason.

Wazzak
*n++; executes as *(n++)

Whenever you exceed the boundary of an array, the OS will react by sending a signal to your program, which indicates a read/write operation on memory that doesn't pertain your program
No, accessing out of bounds is undefined behaviour. There are no checks made, and you could step over your own variables.
in case that you were directionating outside your program memory, the OS may react (protection)
Last edited on
ok yeah I introduced another variable to use instead of *n. Thanks ne555
Topic archived. No new replies allowed.