Jump to content
  • Advertisement

Archived

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

fireking

variable sized multidimension array in C++ not C

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest 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; 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 this post


Link to post
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

Owner/Leader
Genetics 3rd Dimension Development

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Some of the test in the above post got garbled. Let me restate.


  
array[i]


is equivalent to


  
*(array+i)

Share this post


Link to post
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

Owner/Leader
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 this post


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

--Fireking

Owner/Leader
Genetics 3rd Dimension Development

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!