Archived

This topic is now archived and is closed to further replies.

Dynamic Pointer arrays

This topic is 5593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have been programming, `C'', as a hobby for several years now and I have recently started an isometric-tile-based war game. I have linked lists that contain structs(UNIT) for each combat unit on both sides, with a new root node for each team and I need what I like to call a `radar'' that is an array of pointers to UNIT structs, though it does not matter which team a UNIT is on, just if a UNIT is occupying any given coordinate. So, my question is that I do not work with a lot of pointers-to-pointers, but I need to dynamically allocate the `radar'' array at the beginning of each game, because the actual physical map that the game is played on can be different sizes. And, I think that this initialization is correct: UNIT **radar; A pointer to an array of pointers. Now, my problem is that I cannot quite get the syntax for the rest of the time I want to use the `radar.'' For example, I want to intialize the entire map to NULL: for( x = 0; x < size_x; x++ ) for( y = 0; y < size_y; y++ ) radar[x * size_x + size_y] = NULL; <-- Problem! How do you syntactically set up `radar'', a pointer to an array of pointers to UNITs, so I can change the value(address) of each coordinate in the map, i.e. point it to NULL for no unit, or to the address of the coord''s occupier. I would be much obliged for any help. Colin

Share this post


Link to post
Share on other sites
hmm first try to use a ZeroMemory instead of two for loops, it is much faster.
Try *radar[...] = ...; instead of radar[...] = ...;

Share this post


Link to post
Share on other sites
Look ... you forgot to allocate those pointer ...

UNIT **radar;
radar = new UINT*[size_x*size_y]; // ADDED

for( x = 0; x < size_x; x++ )
for( y = 0; y < size_y; y++ )
radar[x * size_x + size_y] = NULL; <-- Problem!


Remember to delete those afterward ...

delete[] radar;

If you already know the size of your pointer array, you better not allocate them dynamicaly ...

#define size_x 10
#define size_x 11

UINT* radar[size_x*size_y]

Anyhow it is up to you ;P




“ ZIM: But... Invader''s blood marches through my veins, like giant radioactive rubber pants! The pants command me! Do not ignore my veins! “

Share this post


Link to post
Share on other sites