#### Archived

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

# variable sized multidimension array in C++ not C

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

## Recommended Posts

i need to know how to allocate the memory for the following situation:
class fftile{
public:
long srcx;
long srcy;
long width;
long height;
};
class ffmap{
public:
fftile **map;
void fillmap(long sizex,long sizey);
};

void ffmap::fillmap(long sizex,long sizey){
//allocate the memory here
}

i have constructors to free the memory too, so if you can show code to free the memory as well, that will be awesome map will be a 2 dimensional array: ie: map[x][y] I need to allocate the memory, according to sizex, which is the number of tiles going accross, and according to sizey, which is the number of tiles going down. Also, if sizex was 5, there would be 5 tiles 0 through 4. We are starting with 0 cuz its an array. Thanks for any help, p.s. Ive read a tutorial on how to do this, but it was in C, and the code is incorrect in the first place. I looked in MSDN but its lack of information about the new operator confused me. --Fireking Owner/Leader Genetics 3rd Dimension Development [edited by - fireking on September 13, 2002 11:40:32 PM]

##### Share on other sites
A basic way of doing this is:

  void ffmap::fillmap(long sizex, long sizey){   map = new fftile*[sizex];   for (long i = 0; i < sizex; i++)       map[i] = new fftile(/* ... */);}ffmap::~ffmap(){    // for this you need to store sizex    // somewhere as a data member    for (long i = 0; i < sizex; i++)        delete [] map[i];    delete [] map;}

A safer way is:

  #include <vector>class ffmap{public:    vector< vector<fftile> > map;    void fillmap(long sizex, long sizey);}void ffmap::fillmap(long sizex, long sizey){    map.resize(sizex);    for (map::iterator i = map.begin(); i != map.end(); ++i)        i->resize(sizey);}

You don''t need a destructor in the latter case as the vector destructors are used.

##### Share on other sites
we wouldnt need to store sizex, we can use ubound + 1 cant we?

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
also, this is a multidimension array, correct me if im wrong, but when i looked at your code it didnt look like it was multidimensionable(woah what a word).

I eventually want to be able to access it like so

map[6][8].Whatever

where 6 is the x, and 8 is the y

i will need to be able to loop through it too

long ix,iy;for(ix=0; ix < maxtilex; ix++){    for(iy=0; iy < maxtiley; iy++){        map[ix][iy].whatever    }}

--Fireking

Genetics 3rd Dimension Development

[edited by - fireking on September 13, 2002 12:25:22 AM]

##### Share on other sites
quote:

we wouldnt need to store sizex, we can use ubound + 1 cant we?

ubound? I don''t know what you''re referring to.

quote:

also, this is a multidimension array, correct me if im wrong, but when i looked at your code it didnt look like it was multidimensionable(woah what a word).

The [] symbols are just an operator. array is equivalent to *(array+i). array[i][j] is equivalent to *(*(array+i)+j). Anyway, long story short, you can use what I showed as array[i][j]. For the vector example the same is true, although if you want bounds checking you can use array.at(i).at(j). It will throw an exception if you index out of bounds. Indexing out of bounds in the unsafe way won''t always give a segmentation fault mind you. That occurs only if you access something outside of your address space.

##### Share on other sites
Some of the test in the above post got garbled. Let me restate.

  array[i]

is equivalent to

  *(array+i)

##### Share on other sites
yippy now i get an assertion failed when program starts, hooray

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
quote:
Original post by Anonymous Poster
A basic way of doing this is:

  void ffmap::fillmap(long sizex, long sizey){   map = new fftile*[sizex];   for (long i = 0; i < sizex/*did you mean to put this here?*/; i++)       map[i] = new fftile(/* ... */);}ffmap::~ffmap(){    // for this you need to store sizex    // somewhere as a data member    for (long i = 0; i < sizex; i++)        delete [] map[i];    delete [] map;}

I have a problem with this code. and i would rather use this than the vector, by the way.

--Fireking

Genetics 3rd Dimension Development

[edited by - fireking on September 13, 2002 12:44:33 AM]

[edited by - fireking on September 13, 2002 12:45:13 AM]

##### Share on other sites
whats the problem with that code?
as it all looks sane enuff to me

##### Share on other sites
it doesnt even use sizey! so apparently its not doing SOMETHING right

--Fireking

Genetics 3rd Dimension Development

• 16
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999749
×