Computing.Net > Forums > Programming > Dynamicly-sized C++ 2D array

Dynamicly-sized C++ 2D array

Reply to Message Icon

Original Message
Name: GyroTech
Date: October 16, 2004 at 18:12:29 Pacific
Subject: Dynamicly-sized C++ 2D array
OS: Win2K Pro \ SuSE 9.1
CPU/Ram: AMD XP 3000+ \ 1Gb DDR333
Comment:

I'm writing a class in C++.
I need to create a member 2d array but the size is not known until the constructor is called...

Currently, in my header I have defined the array:
int* i2d;

and in the constructor it's initialised:
i2d = new int[width][height];

width and height are parameters passed into the constructor.

All I get with this is a compiler error 'error C2540: non-constant expression as array bound' using MSVC++6 (no MFC).
It works fine if I create it as:
i2d = new int[width * height];

which will give me the same number of elements, but I really need it to be 2d...

Any ideas??

TIA


Report Offensive Message For Removal

Response Number 1
Name: BlueRaja
Date: October 16, 2004 at 18:52:59 Pacific
Subject: Dynamicly-sized C++ 2D array
Reply: (edit)

uh...
Why can't you use the latter method? That's the way people usually create 2d arrays..
You could always create a macro:
#define MAX_WIDTH 32
#define I2DMACRO(w,h) w*MAX_WIDTH+h
...
i2d[I2DMACRO(width,height)]=12345;

AKhalifman@hotmail.com


Report Offensive Follow Up For Removal

Response Number 2
Name: egkenny
Date: October 16, 2004 at 23:54:53 Pacific
Subject: Dynamicly-sized C++ 2D array
Reply: (edit)

#include <iostream>
using namespace std;

class MyClass
{
public:
MyClass(int nRows, int nCols);
int getArray(int iRow, int iCol);
void setArray(int iRow, int iCol, int iVal);
private:
void initArray();
int **m_array;
int m_Rows;
int m_Cols;
};

MyClass::MyClass(int nRows, int nCols)
{
m_Rows = nRows;
m_Cols = nCols;
initArray();
}

void MyClass::initArray()
{
// dynamic allocation of 2-D arrays
int iRow;
m_array = (int **) malloc(m_Rows *sizeof(int));
for(iRow=0;iRow < m_Rows;iRow++)
m_array[iRow] = (int *) malloc(m_Cols*sizeof(int));
}

int MyClass::getArray(int iRow, int iCol)
{
if (iRow>0 && iRow<m_Rows && iCol>0 && iCol<m_Cols)
return m_array[iRow][iCol];
else
return 0;
}

void MyClass::setArray(int iRow, int iCol, int iVal)
{
if (iRow>0 && iRow<m_Rows && iCol>0 && iCol<m_Cols)
m_array[iRow][iCol] = iVal;
}

int main()
{
int nRows = 120;
int nCols = 30;
class MyClass A(nRows, nCols);
int result;

A.setArray(30, 20, 123);
result = A.getArray(30,20);
cout << result << endl;

A.setArray(nRows-1, nCols-1, 368);
result = A.getArray(nRows-1, nCols-1);
cout << result << endl;
return 0;
}


Report Offensive Follow Up For Removal

Response Number 3
Name: shantanu_gg
Date: November 17, 2004 at 10:04:38 Pacific
Subject: Dynamicly-sized C++ 2D array
Reply: (edit)

This code may be helpful for 2d array dynamic initialization in C++

int **a;
int rows=3,cols=4;//3X4 matrix
a=new int* [rows];
for(int i=0;i<rows;i++)
*(a+i)=new int[cols];

Happy Coding
Shantanu


Happy Coding
Shantanu


Report Offensive Follow Up For Removal

Response Number 4
Name: shantanu_gg
Date: November 17, 2004 at 10:05:02 Pacific
Subject: Dynamicly-sized C++ 2D array
Reply: (edit)

This code may be helpful for 2d array dynamic initialization in C++

int **a;
int rows=3,cols=4;//3X4 matrix
a=new int* [rows];
for(int i=0;i<rows;i++)
*(a+i)=new int[cols];


Happy Coding
Shantanu


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Dynamicly-sized C++ 2D array

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software