variable sized multidimension array in C++ not C

Started by
15 comments, last by fireking 21 years, 7 months ago

    // to createmap = new fftile*[sizex];for(int col = 0; i < sizex; i++)  map[col] = new fftile[sizey];// to deletefor(int col = 0; i < sizex; i++)  delete[] map[col];delete[] map;  


You'd still have to loop through all the tiles to initialize them since you can't call anything but a default constructor when you use new[]. But, you should be able to use map[x][y] to address a specific tile.

This is actually backwards from the way 'normal' arrays work however since they are in the form array[row][column] and not array[column][row], which is essentially what you're saying by having the x first. If that bothers you and you want this to act like a regular 2D array, then you need to swap sizey and sizex and write map[y][x].


[edited by - jaxson on September 13, 2002 12:57:15 AM]
Advertisement

  for (long i = 0; i < sizex/*did you mean to put this here?*/; i++)    map = new fftile(/* … */</font>);<br>  </pre></DIV><!–ENDSCRIPT–><br><br>The sizex is right, but it should be<br><br>new fftile[sizey];<br><br>not<br><br>new fftile(/* … */);<br><br>my mistake. I thought it was fftile ***map and got a little ahead of myself. <img src="wink.gif" width=15 height=15 align=middle>    
ok ok, so finally, here it is, and it works


  #include "ffclass.h"ffmap::ffmap(){	map=NULL;}ffmap::~ffmap(){	long i;	for(i=0;i<tilesizex;i++)		delete [] map[i];	delete [] map;}void ffmap::FillMap(long sizex,long sizey){	long i;	tilesizex=sizex;	tilesizey=sizey;	map = new fftile*[sizex];	for(i=0;i<sizex;i++)		map[i] = new fftile[sizey];}  


thakns a lot, i can use these concepts to do more memory management in my applications

--Fireking

Owner/Leader
Genetics 3rd Dimension Development
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Another way to do it would be:

  // assuming that you have members called width and heightvoid CMap::CreateNewMap(char *pMap, int SizeX, int SizeY){pMap = new char[SizeX * SizeY];Width = SizeX;Height = SizeY;}void SetCharInMap(char *Tile, int X, int Y){pMap[X + Y * Width] = Tile;}void CleanUpMap(char *pMap){delete [] pMap;}  


Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
"thakns a lot, i can use these concepts to do more memory management in my applications"

oh gee, doesn''t that sound fun? How about using vector? Or maybe writing your own templated 2d array class? Memory management should be avoided, not embraced
Here is an example which may clarify why vectors are preferred for exception safety reasons, and how to use them:

http://makeashorterlink.com/?P2ED368C1

David
Okay I know of the way to map a 2d array into a 1d one, but won''t that make it more difficult to allocate a large 2d array? If you break the 2d array up into an array of arrays the requirement of a large contiguous chunk of memory doesn''t exist, but if you map a 2d array into a 1d one, all of the memory used has to be contiguous. I could see this posing a problem for working with very large matrices.

This topic is closed to new replies.

Advertisement