Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic allocation

Started by Floru Mar 17, 2000 at 8:27 AM 7 replies 550+ views
Original Post
Floru
Floru
Hi! Could someone give me a example code for c++ which allocates two-dimensional array. The array would contain classes. Here is code which I made but it didn''t work: pointer to the array: // CCluster is a class CCluster ***m_pCluster; Memory allocation: { m_pCluster = (CCluster ***) new CCluster(); // Allocate memory for the X elements for(int x = 0; x < maxX; x++) { m_pCluster[x] = (CCluster **) new CCluster(); // Allocate memory for the Y elements for(int y = 0; y < maxY; y++) { m_pCluster[x][y] = (CCluster *) new CCluster(); } } } It works for example 25*25 array but if it gets bigger the program terminates. I use WinNT does it matter? Any help would be appreciated. Floru
kressilac
kressilac
Take you for loops and embed them into eachother. Use the x,y pairs in your new statement. Something like this.


for(int x = 0; x < maxX; x++)
{
for(int y = 0; y < maxY; y++)
{
m_pCluster[x][y] = (CCluster *) new CCluster();
}
}



What you were doing before was effectively allocating memory for all x''s in the first y. With y being undefined in the first for loop I am not sure how the compiler would like that or the exe would like it. I am guessing it would be defaulted to y = 0. Anyway if you embed the for loops both x and y will be defined for any give usage of new, allowing the array to be properly built.

Hope this helps.
Kressilac


Derek Licciardi (Kressilac)Elysian Productions Inc.
kressilac
kressilac
Oh also be sure you have enough memory available. If you allocate a 25*25 array of classes 1k in size you will use 625k memory. Make the class 10,000 in size(lots of strings or included classes) and your now at 6.25 megs of memory and you haven''t counted the rest of your code or data. This could be the problem but I don''t know enough about your class to say so for sure.

Kressilac
Derek Licciardi (Kressilac)Elysian Productions Inc.
Timdog
Timdog
Try this:

CCluster *m_pCluster[][];

m_pCluster = malloc(((int)sizeof(CCluster *)) * maxX * maxY);

for (x =0; xfor (y =0; ym_pCluster[x][y] = new CCluster;

null_pointer
null_pointer
kressilac: I can allocate the arrays and use them properly. This may seem stupid, but how do I delete them? Here's the code I'm using:


for( int x=0; x < m_nWidth; x++ )
{
for( int y=0; y < m_nHeight; y++ )
{
delete (m_pTiles[x][y]);
// m_pTiles[x][y] = NULL;
}
}


As you can see, I commented out the line that made the pointer NULL, which triggered an access violation when I delete the stuff. The access violation occurs when x=1 and y=0 (and every time y resets to 0).

Thanks!


- null_pointer



Edited by - null_pointer on 3/17/00 2:08:48 PM
kressilac
kressilac
It could possibly be in the definition of the array. Define your array something like this. If you need to pass this into a function then use a double pointer. You should be able to delete like you were trying to using this type of a declaration.

CCluster *m_pCluster[MAX_X][MAX_Y];

Kressilac

ps Usually GPFs are cause by accessing memory that is undefined, unitialized, or invalid. Look at where the array of pointers is defined, how it is used, check if it is freed in any way before you get to the delete code, and finally check the scope of all references to it. This should lead you to the bug your experiencing in the loop. I have a feeling it was planted elsewhere in the code and not in that loop.
Derek Licciardi (Kressilac)Elysian Productions Inc.
null_pointer
null_pointer
kressilac: It was defined using the code from your first reply, to Floru -- the only thing I changed was to make it use my class type!

Here's how I allocate the array:


// Create a new array of tile and object pointers
m_pTiles = (Tile***) malloc(dwTiles * (sizeof( Tile* )));

// Fill the array with new tile objects
for( int x=0; x < m_nWidth; x++ )
for( int y=0; y < m_nHeight; y++ )
m_pTiles[x][y] = new Tile(x, y, this);



And here's how I delete it:


for( int x=0; x < m_nWidth; x++ )
{
for( int y=0; y < m_nHeight; y++ )
{
delete m_pTiles[x][y]; // crashes here, when x=1 and y=0
m_pTiles[x][y] = NULL;
}
}

free(m_pTiles);
m_pTiles = NULL;



Syntax for multi-dimensional arrays is terrible!

Thanks!


- null_pointer


Edited by - null_pointer on 3/17/00 5:06:45 PM

Edited by - null_pointer on 3/17/00 5:09:19 PM
kressilac
kressilac
I must be missing something here. The for loop you used should work at deallocation. The only reason I can think of that would explain why it doesn''t work would be related to the triple pointer. If you have an array of x,y pointers you should be able to iterate through the array in two dimensions and delete each class attached to a pointer.

Try deleting one from somewhere in the middle. Try setting one at x,y 7x7 and then deleting the same. At this point I am not sure I can be of more help without sitting down and coding it up myself. Unfortunately I do not have the time.

Sorry I couldn''t be more help.
Kressilac
Derek Licciardi (Kressilac)Elysian Productions Inc.
null_pointer
null_pointer
kressilac: Since it was a different question, I decided to put it in a new post, called "Multidimensional Arrays." It's already been answered, but the answer was quite different from what I expected! You might want to check it out


- null_pointer


Edited by - null_pointer on 3/21/00 7:00:55 AM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.