0

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++;
}
5
  • 3
    Any problem with the memory is always in the code (your). Please show the code. Commented Dec 24, 2013 at 9:13
  • Do you allocate the memory once at the beginning of the app, or reallocate it in runtime? If the latter, your all players could just break. And what's your reason not to use std::vector? Commented Dec 24, 2013 at 9:15
  • " 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", this has nothing to do with memory corruption. There are various possibilities, you could be deallocating some objects and reusing them, you could be accessing non existing array indices, you may not be assigning pointers properly, but as I said before nobody can help you without knowing what you have written. Commented Dec 24, 2013 at 9:26
  • game.players is assured to be 0 at beginning of each game, before spawning the players. Commented Dec 24, 2013 at 9:39
  • Using arrays manualy like you do is doing c in c++. You should use what c++ provide. Learn about the STL Commented Dec 24, 2013 at 9:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

RAEG I just wasted 2 days because I managed to be so stupid to overlook a division by zero hole in my code. I thank you all for you efforts, I knew C++ couldn't be wrong ;p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.