Jump to content
  • Advertisement

Archived

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

Sand_Hawk

Dynamic Multi Dimensional arrays

This topic is 5703 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 am busy with an app that requires multi dimensional arrays, However, since they differ per object I create them dynamicly. This is the source I tried:
  
char *Data;
Data = new char[2, 10]; // This would create an array with 2 row and 10 chars per row

// do stuff

delete [] Data;
  
Why doesn''t this work? I didn''t test it on a VC6 compiler but on Borland but that shouldn''t be the problem. I never tried this source at home but can anyone tell me what I might do wrong? Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try this:

char *Data;
Data = new char[2][10];

// Do stuff

delete [] Data;

Share this post


Link to post
Share on other sites
Hi Sand_Hawk,

I don''t have a lot of experience with c++ but i think this is your solution:

#define length1 10
#define length2 2
char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


In c++, real multi dimensional arrays doesn''t exist. Instead you can use an array of array''s.

Glenn

Share this post


Link to post
Share on other sites
Thanks for all the help so far. I''ve used this method:


char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


I can write to the memory, but if I try to read it my programs crahes. I use Data[Y][X] to read 1 character.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
I must have posted this a million times over!


    
typedef float ARRAY[3]; // Just as an example.


ARRAY *TwoDArray;

TwoDArray = new ARRAY[3];

// And to delete...

delete [] TwoDArray;


This should create aand delete a 3x3 array.

Peace out,
Mathematix.
Q3A + UT1999 = UT2003
therefore UT2003 - Q3A = UT1999
and UT2003 - UT1999 = Q3A!

[edited by - Mathematix on November 7, 2002 5:18:54 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sand_Hawk
Thanks for all the help so far. I've used this method:


char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data = new char[length2];


I can write to the memory, but if I try to read it my programs crahes. I use Data[Y][X] to read 1 character.



Here's the problem. It should be:

  
char** data;

data = new char*[length1];
for (i = 0; i < length1; ++i)
data[i] = new char[length2];


Note that each element in data is now getting a 1d array, making a 1d array of 1d arrays: ie a 2d array.

Mathematix's idea looks pretty neat, though.

Peace,
Doc


[edited by - doc on November 7, 2002 6:40:41 PM]

[edited by - doc on November 7, 2002 6:42:02 PM]

Share this post


Link to post
Share on other sites
I noticed I made a mistake in pasting the code. I used that code. However, coming back to the typedef, can I use a typedef to when I want to create dynamic X and Y axis because now you set it to max 3 on the rows.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

Share this post


Link to post
Share on other sites
Nope. That typedef will be fixed. Dynamic sized columns will have to be done the hard way.

Peace,
Brendon

Share this post


Link to post
Share on other sites
It seems like I am going crazy. I ran the debugger over it and it should work. Here is the code:


  
int iIndex = 0;
ifstream FishFile(FileName, ios::binary);

// Load X and Y dimensions from the file

FishFile.read((char *)&DimX, sizeof(int));
FishFile.read((char *)&DimY, sizeof(int));

// Allocate dynamic array with size of the fish

FishData = new char*[DimY];
for (iIndex = 0; iIndex < DimY; iIndex++)
FishData[iIndex] = new char[DimX];

// Read the fish from file

FishFile.read((char *)FishData, sizeof(FishData));


It is for the Ascii fishtank contest. What is wrong with the allocation code? And also, can I read the array from file the way I do it?

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!