This is a section of code from a simple game I'm making. This routines check each bullet or enemy in the array and removes them from the array if they are already past the screen. (This code uses SDL2, but I don't think it matters.)
void cleanEnemies(Enemy* a[], int* s)
{
Enemy* e;
for (int i = 0; i < *s; i++)
{
e = a[i];
if (e->getX() < -75)
{
e->~Enemy();
e = NULL;
for (int j = i; j < *s; j++)
{
a[j] = a[j + 1];
}
(*s)--;
}
}
printf("Enemy count: %i\n", *s);
}
void cleanMissiles(HomingMissile* a[], int* s)
{
HomingMissile* m;
for (int i = 0; i < *s; i++)
{
m = a[i];
if (m->getX() < -75)
{
m->~HomingMissile();
m = NULL;
for (int j = i; j < *s; j++)
{
a[j] = a[j + 1];
}
(*s)--;
}
}
printf("Missile count: %i\n", *s);
}
Running the program produces pointers that point to invalid memory locations. Can anyone please tell me what I'm doing wrong? Thanks.