Archived

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

clabinsky

Help! 2-dim array and new

Recommended Posts

How do I dynamically allocate a two dimensional array? Declare what in the private: section of the class
  
struct MAP
{
   MAP *lpNext;
   MAP *lpPrev;
   DWORD dwTerrain;
   DWORD dwIndex;
};
class CMap
{
    MAP    *m_aMap[][];?????
}
 AND

when allocating

    m_aMap= new MAP[][100]; ???

  
Please and thanks. I am actually a Swede, living in paradise.

Share this post


Link to post
Share on other sites
It should be OK with only the pointer like this

MAP *m_aMap;

Then you can allocate the memory with ney just as you did it!

m_aMap = new MAP[100][100];

Because in C++ pointers and indexed Arrays are pretty much the same thing you can adress it afterwards with something like this

m_aMap[70][20] = 15;

I hope that did help!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
>MAP *m_aMap = new MAP[100][100];

You can''t do that. Do this:

  
MAP **m_aMap = new MAP*[100];

for (int i = 0; i < 100; i++) {
m_aMap[i] = new MAP[100];
}


Or made it a one-dimensional array and when indexing into (i, j) have it index into i*numberOfRows+j.

Share this post


Link to post
Share on other sites
Sorry Master Jazzman but I tried that without success...

I´ll try the other suggestion later on, for now I keep it static but it has to be dynamic later on.

Thanks both of you.

Share this post


Link to post
Share on other sites
you could make it one-dimensional, and index it yourself:

// create it
int* tG = new int[nWidth * nHeight];
// use it
tG[x + y * nWidth] = p_def_alt;

you oughtta be able to figure out what those spare variables are for...

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites