Click to See Complete Forum and Search --> : Dynamic Multidimensional Arrays
April 19th, 1999, 08:28 AM
Hallo Gurus
I've done the following. It works.
float *array = new float[m_numbRows];
What I like to do is the following:
float *array = new float[m_numbRows][m_numbRows];
But it doesn't work.
I've got the following help from a friend. We think there should be an easier way.
float** pvfArray;
.....later....
pvfArray = new float*[iNumRows];
for(int i = 0; i < iNumRows; i++)
pvfArray[i] = new float[iNumCols];
pvfArray[iR][iC] = 3.9;
Thanks for the help.
M
April 19th, 1999, 09:56 AM
Have you tried:
float *array = new float[m_numbCol*m_numbRows];
array[7][9] = 25; //or
array[(7*m_numbRows) + 9] = 25;
...or something similar?
meintjesb
April 20th, 1999, 01:45 AM
Thank you very much for your help.
I got everything to work, except for
array[7][9] = 25;
Thanks again for your help.
Regards
Meintjes
Yangghi Min
April 20th, 1999, 03:01 AM
When you are to allocate a multi-dimensional array with the new operator, you can do it
only if all dimensions except for the leftmost array dimension are constant expression.
Here are some examples:
#define NUM_ROWS 10
#define NUM_COLS 10
float (*array2)[NUM_ROWS];
float (*array3)[NUM_ROWS][NUM_COLS];
int first_rows;
// after somehow the value of the first_rows is determined.
array2 = new float [first_rows][NUM_ROWS];
array3 = new float [first_rows][NUM_ROWS][NUM_COLS];
// When the array buffers are no longer needed, you can use delete operator like the followings.
delete [] array2;
delete [] array3;
If you are to allocate a dynamic multidimensional array without any fixed dimension specifier,
the advice of your friend is a standard way of allocating a dynamic multidimensional array.
Regards,
Yangghi.
sally
April 20th, 1999, 09:56 PM
array[7][9] = 25; won't work
array is a single dimensional array SIMULATING a multi dimensional array
index = 7 * numRows + 9;
array[index] = 25;
Sally
April 20th, 1999, 09:56 PM
array[7][9] = 25; won't work
array is a single dimensional array SIMULATING a multi dimensional array
index = 7 * numRows + 9;
array[index] = 25;
April 25th, 1999, 01:42 PM
There's a forumla you can use on a raw block of memroy to treat it as a multi-dimensional array.
You can also take a raw block of memory and initialize it as a multi-dimensional array. Uses more memory but allows you to keep the array[][] schemantics.
And of course, the easiest route is to use STL.
I think someone already posted the formula for option one, so here's option two and three...
static const int ROWS = 10;
static const int COLS = 10;
int STORAGE = ( (ROWS * COLS) * (sizeof(float)/sizeof(DWORD)) ) + ROWS;
float** ppFloat = (float**)new DWORD[STORAGE];
::memset(ppFloat,0,STORAGE * sizeof(DWORD));
for(int index = 0;index < ROWS;++index)
{
ppFloat[index] = (float*)((DWORD*)ppFloat + ROWS) +
index * ((sizeof(float)/sizeof(DWORD)) * ROWS);
}
float val = 1.5;
for(int r = 0;r < ROWS;++r)
for(int c = 0;c < COLS;++c)
ppFloat[r][c] = val++;
for(r = 0;r < ROWS;++r)
for(int c = 0;c < COLS;++c)
std::cout << '[' << r << "][" << c << "]: "
<< ppFloat[r][c] << std::endl;
For STL it helps to create a custom function object to help initializing the array.
typedef std::vector<float> tFloatCol;
typedef std::vector<tFloatCol> t2dFloatVec;
class setdim
{
public:
setdim(const int dim = 1):
m_dim(dim)
{}
void operator()(tFloatCol& col)
{
col.resize(m_dim,0);
}
int m_dim;
};
Then using it is...
static const int ROWS = 10;
static const int COLS = 10;
t2dFloatVec vec;
vec.assign(ROWS);
std::for_each(vec.begin(),vec.end(),setdim(COLS));
float val = 1.5;
for(int r = 0;r < ROWS;++r)
for(int c = 0;c < COLS;++c)
vec[r][c] = val++;
for(r = 0;r < ROWS;++r)
for(int c = 0;c < COLS;++c)
std::cout << '[' << r << "][" << c << "]: " << vec[r][c] << std::endl;
Hope it helps...
Mike
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.