Question

the title is a bit too general. Let me get to it straight. I have an application with a large number of potential structures, called Player. So I thought, lets make an array of pointers to Player, due to the fact that usually you wont need all the players and you will safe up dynamic memory compared to directly allocating an array with the maximum size, so (C++):

Player* a[max];
for loop

     a[i] = new Player

end

vs

Player* a;
a = new Player[max]

The first example is what I use in a function. Every time you call that function, it allocates the next pointer in the array. Everything works, but the strange thing is that sometimes one of the pointers seems to lose its reference to the heap memory. When I have 2 players and I allocate 10, it works, but after a while (several frames guaranteed) it displays "-1.#J" as the value of a float in that Player structure, which I suppose means that it is an undefined value. I could not post the entire code (would be too much), and I checked for all other possible bugs, but I could not find one. I start presuming that it has to do with the fact that I allocate the memory using new in a function in a different .ccp file (so different obj file). Could this be the case? And can you use array of pointers for this situation?

I could be horrible be wrong with my ideas about memory saving using this method. Please give me advice on how to do it properly if so.

Thanks.

(EDIT)

the code for spawning the player (I dont know how to do the layout, sorry for that):

    void SpawnPlayer(float xpos, float ypos, float angle, unsigned short ammo[WP_TOTAL], unsigned char weapon)
{
    player[game.players] = new Player;
    // Properties
    player[game.players]->pos[0] = xpos;
    player[game.players]->pos[1] = ypos;
    player[game.players]->angle = angle;
    player[game.players]->dir[0] = 0.0f;
    player[game.players]->dir[1] = 0.0f;
    player[game.players]->speed = PL_SPEED;
    player[game.players]->health = PL_HEALTH;
    player[game.players]->shoot = true;
    player[game.players]->shootwait = 0;
    unsigned char i;
    CLoops(i, 0, WP_TOTAL)
    {
        player[game.players]->ammo[i] = ammo[i];
    }
    player[game.players]->weapon = weapon;
    // Model
    float cx = cos(angle) * PL_RADIUS, cy = sin(angle) * PL_RADIUS;
    player[game.players]->model.vertexcnt = 4;
    player[game.players]->model.vertex = new float[8];
    player[game.players]->model.vertex[0] = xpos - cx - cy ;player[game.players]->model.vertex[1] = ypos - cy + cx;
    player[game.players]->model.vertex[2] = xpos - cx + cy ;player[game.players]->model.vertex[3] = ypos - cy - cx;
    player[game.players]->model.vertex[4] = xpos + cx - cy ;player[game.players]->model.vertex[5] = ypos + cy + cx;
    player[game.players]->model.vertex[6] = xpos + cx  + cy ;player[game.players]->model.vertex[7] = ypos + cy - cx;    
    player[game.players]->model.texcoord = new float[8];
    player[game.players]->model.texcoord[0] = 0.0f;player[game.players]->model.texcoord[1] = 0.0f;
    player[game.players]->model.texcoord[2] = 1.0f;player[game.players]->model.texcoord[3] = 0.0f;
    player[game.players]->model.texcoord[4] = 0.0f;player[game.players]->model.texcoord[5] = 1.0f;
    player[game.players]->model.texcoord[6] = 1.0f;player[game.players]->model.texcoord[7] = 1.0f;
    core.CCreateModel(player[game.players]->model, CMODEL_TRISTRIPS, CMODEL_DYNAMIC);
    // AI
    player[game.players]->target = PL_MAXCNT;
    player[game.players]->move = 0;
    // Add global counter
    game.players++;
}
Was it helpful?

Solution

Why not use a vector of Player object pointers?

std::vector<Player*> vec;

Then, when you want to allocate a new player, do:

vec.push_back(new Player);

Here, the problem with your code may be that there is a chance of any Player stored in an array getting over written. But, using vector, you can use const iterators and hence, make sure that they are not getting changed when you don't want them to.

Also, you don't have to worry about allocating a constant size.

Also, if your Player object contains any member pointers, make sure that you don't pass any Player object by value or do assignment using them. If that is the case, you might want to write your own copy constructor and assignment operator so that shallow copy does not occur.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top