Dynamic 2d arrays of objects allocate in desired index

Hi. I have this short game I am designing. It has an Organism class, an ant class and a bug class. Organism is parent. Other two inherits from parent.

I want to create a 20 x 20 grid of organism and initialize ant and bug so that bug can prey on ant.

I did this

Organism** organisms= new Organism* [20]

for(i=0;i<20;i++)
organisms[i]=new Organism[20];

Now every time I want to breed a new ant in an index say [15][12] how can I allocate such in organisms object?

Ant *ant= new Ant

organisms[15]= .....? I am stuck here
Last edited on
Double pointers are ugly, stick with 2D array syntax. If you want to initialize the 400 organisms to start with (maybe to null), use 2 for loops.

Is this what you were thinking of?

1
2
3
Organism *organisms[20][20]; 
Ant *ant = new Ant;
organisms[15][12] = ant;
Morning.
Are you writing this in C? Otherwise you could consider using 'cleaner' STL containers like vectors?
James2250 suggestion makes sense if you intend to use a fixed size grid; far safer than dynamic memory. And in real code you'd use std::vector or equivalent, as mutexe mentioned, if you required a variable sized grid.

But if you're doing this as an exercise on using pointers, etc...

organisms[15]= .....? I am stuck here

As you are using polymorphism I presume...

It has an Organism class, an ant class and a bug class. Organism is parent. Other two inherits from parent.

... you need your grid to store base class (Organism*) pointers rather than Organisms. So you need to use an extra *:

1
2
3
4
5
6
// create an array of Organism**s
Organism*** organisms = new Organism**[20](); // use value initialization

// for each element of this array create a child array of Organism*s
for(int i=0;i<20;i++)
    organisms[i] = new Organism*[20](); // // use value initialization 


so you end up with a "grid" of Organism* elements (not Organism instances like you had.)

The () after the [] used when new-ing the arrays tells the compiler to value initialize them (to fill them with zeros.) Without it the arrays would all be filled with whatever happens to be in the memory the allocator gives you (or sometimes a debug fill pattern, if you're using a debug allocator.)

In C++11 you can use {} rather than (), which is all C++03 understands.

(Aside: you can do the same for an array:

Organism *organisms[20][20] = {0}; // also works in C

Organism *organisms[20][20] = {}; // works in C++ )

or

Organism *organisms[20][20]{}; // works since C++11

And if an array -- or a normal variable -- is global or static then it's automatically zeroed anyway.)

You can then access them in the same ways as James2250's array based example.

organisms[15][12] = new Ant;

operator[] acting on organisms returns the Organism** for the row.

operator[] acting on the returned Organism** returns the Organism* pointing at the instance.

Of course, you've to free all the memory you allocated above!

Andy

PS Please use code tags!

Also see the article "How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

Last edited on
thanks! it is working now
Topic archived. No new replies allowed.