Dynamic Multi Dimensional arrays

Started by
11 comments, last by Sand_Hawk 21 years, 5 months ago
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
----------------(Inspired by Pouya)
Advertisement
Try this:

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

// Do stuff

delete [] Data;
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
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
----------------(Inspired by Pouya)
Your method is correct, your problem must be in the size of the arrays or the value of x or y.
"I seek knowledge and to help those who also seek it"
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]
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]
My stuff.Shameless promotion: FreePop: The GPL god-sim.
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
----------------(Inspired by Pouya)
Nope. That typedef will be fixed. Dynamic sized columns will have to be done the hard way.

Peace,
Brendon
My stuff.Shameless promotion: FreePop: The GPL god-sim.
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
----------------(Inspired by Pouya)

This topic is closed to new replies.

Advertisement