Link to home
Start Free TrialLog in
Avatar of gwaltersii
gwaltersii

asked on

Multi-Dimensional Dynamic Arrays

I know this is not possible through the traditional approach...
 array = new int[][];

I heard there were some work arounds to this issue. Can some shed some light. I would like some sample code that will do the following:

1. Declare and initialize the array.
2. Populate the array.
3. Extract data from the array.
4. Destroy the array properly.

I will start with 100 points. Will add more for thorough code. I do not need a history lesson in C++ compiler design. I just need a quick and dirty answer to the question. Thanks so much!
Avatar of ShankarRamaswamy
ShankarRamaswamy

if stack allocation then
Decl:-
int array[10];
Init:-
for(int i=0;i<10;++i)
array[i] = value;

extraction:-
 someval = array[3];
deletion:-
automatic ,

heap allocation:-
decl:-
int *array;
array = new int;
Init:-
for(int i=0;i<10;++i)
array[i] = value;

extraction:-
 someval = array[3];

deletion:-
delete []array;

ASKER CERTIFIED SOLUTION
Avatar of PMazur
PMazur

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gwaltersii

ASKER

The question is about dynamic
'Mulit-Dimensional' arrays. I want to have rows and columns in a dynamic array

Thanks : )
Here is a sample use of it:

Array2D<int> array2D (5, 10); // 5 rows, 10 elements in each

array2D[0][1] = 5;
int xVal = array2D[0][1];
Hi,

How about this:

Let N, M are dimensions of your array

#define N  10           // rows
#define M   2           // cols

....
array = new int[N*M];
....
// for accessing array

int *AccessCell(int rows, int cols, int row, int col, int *arr)
{
  if(col>=cols || row>=rows) return NULL;

  return &arr[row*M+col];
}

// for initializing [3][1]
*AccessCell(N, M, 3, 1, array) = 7;
....

// for extract [3][1]
int a = *AccessCell(N, M, 3, 1, array);

// for destroying
delete[] array;

//------------------------------------

You can encapsulate this functionality in a class.
The above comment was for Shankar..

PMazur.. thanks.
How is it called and used?  Do I use this to declare?

Array2D Array[x][y];
Sorry PMazur.. I should have waited for you to finish.. I will give that a shot. What should I do about destroying the object? Do I use the delete directive?
destructor takes care about deleting all elements.
In fact only requirements is that classes should provide default contructor. It is necessary to initialize the elements during creation.

In fact the functionality of that template is quite limited, but it isn't difficult to add some extra features such as assignment operator or copy constructor which will make it a little more flexible.
>> quick and dirty
Quick but not dirty:

#include <vector>

template<typename T>
class vector2D
{
  private:
    typedef vector<T> rowtype;
    vector<rowtype> array;

  public:
    // Add c'tors & d'tor,  depends on what you want
    // accessor (example)
    const T& GetElement(int row, int col)
    { return array[row][col]; }
    // add everything you need, depends on what you want
};
As if there aren't already enough answers ... for a quick and dirty dynamic "2-dimentional" array, I like an array of arrays:

1. Declare and initialize the array.

// Declare as an array of pointers
int** array;
array = new int*[numrows];

// Make each pointer element an array
for(int i=0; i<numrows; i++)
   array[i] = new int[numcols];

2. Populate the array.
  AND
3. Extract data from the array.

Works just like any 2D array.

4. Destroy the array properly.
for(int i=0; i<(sizeof(array)/sizeof(array[0]))
   delete[] (array[i]);
delete[] array;
Thanks.. with a little work I got yours to do the job!
Hi gwaltersii,

I am very new to C++(about 2 weeks) and I am curious to know how this t emplate works.

Do I put them in a seperate file or the same file as my int main() ?
Do I need to include any special header files ?

Is it possible to show me a copy of a working code so I can get an idea about this template ?

I will open another question to award the points if necessary.

Thanks and Regards
C++ Beginner
Avatar of Axter
Here's a 2 dimensional array class that is more efficient then the above Array2D class.

template < class T, int ROW_T = 0, int COL_T = 0 >
class dynamic_2d_array
{
public:
    dynamic_2d_array(int row, int col):m_row(row),m_col(col), m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
    dynamic_2d_array():m_row(ROW_T),m_col(COL_T), m_data(new T[ROW_T*COL_T])
      {if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] = {{x[0]}};}}
    ~dynamic_2d_array(){if(m_data) delete []m_data;}
    T* operator[](int i) {return (m_data + (m_col*i));}
    T const*const operator[](int i) const {return (m_data + (m_col*i));}
private:
    const int m_row;
    const int m_col;
    T* m_data;
};

For example usage, see the following link:
http://axter.com/faq/topic.asp?TOPIC_ID=60&FORUM_ID=4&CAT_ID=9
lion-g,

>>Do I put them in a seperate file or the same file as my int main() ?
If you need to use the template class in multiple source files (*.cpp), then you need to put it in a header file.

>>Do I need to include any special header files ?
For the example class I posted, you don't need any special headers.


>>Is it possible to show me a copy of a working code so I can get an idea about this template ?
The above link I posted, shows a working copy of the dynamic_2d_array class.
Hi Axter,

Thanks for the prompt reply!!

Here's my code, and it didn't seems to work !! don't know how as I am only a beginner.
By the way, I am using Dev C++ complier. Can the code works in this complier ?


#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <c:\my documents\dynamic_2d_array.h>

int main()
{

typedef char MYARRAY_T[3][20];

SomefFunction(MYARRAY_T);
Testdynamic_2d_array;

return 0;
}

void SomefFunction(MYARRAY_T &Src)
{    for (int i = 0;i < 3;++i)    {        
for (int i2 = 0;i2 < 20;++i2)        {            
std::cout << Src[i][i2] ;        }        
std::cout << std::endl;    }}

void Testdynamic_2d_array()
{    dynamic_2d_array < char > My_dynamic_2d_array(3, 20);        
MYARRAY_T TestData = {"hello world", "bla bla","more bla"};    
int i;    
for (i = 0;i < 3;++i)    {        
for (int i2 = 0;i2 < 20;++i2)        {            
My_dynamic_2d_array[i][i2] = TestData[i][i2];        }    }        
for (i = 0;i < 3;++i)    {        
for (int i2 = 0;i2 < 20;++i2)        {            
std::cout << My_dynamic_2d_array[i][i2] ;        }        
std::cout << std::endl;    }        
SomefFunction(TestData);    
SomefFunction(*((MYARRAY_T*)&My_dynamic_2d_array[0][0]));    
char *data1 = TestData[1];    
const char *data2 = My_dynamic_2d_array[1];        
std::cout << data1 << std::endl;    
std::cout << data2 << std::endl;}

How do i change it to accept double type ?

Thanks a lot
C++ Beginner
>>By the way, I am using Dev C++ complier. Can the code works in this complier ?
The Dev C++ compiler uses the GNU compiler, which is a very good compiler as far as being C++ compilant.
Yes, the code does work with the GNU compiler.

To make it use a double type, just change the type in the declaration.
dynamic_2d_array < double > My_dynamic_2d_array(3, 20);        

What exactly doesn't work?

I notice you have the functions after the main().
You should either put the functions before the main(), or put a forward function declartion for each function before the main()
Hi Axter,

That's fast ! Thanks for your response and patience.

Well, I still can't get the original code running, let alone changing it to DOUBLE !!!

I do have the following bugs from the complier :-

1)MYARRAY_T' was not declared in this scope

2)Src was not declared in this scope

3) variable or field `SomefFunction' declared

I think these are the few important ones, there are others which might be a conquence of the above.

here's my altered source code just for your reference:-

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <c:\my documents\fyp\dynamic_2d_array.h>
using namespace std;

void SomefFunction(MYARRAY_T &Src)
{
for (int i = 0;i < 3;++i)
    {for (int i2 = 0;i2 < 20;++i2)
        {std::cout << Src[i][i2];}        
                std::cout << std::endl;}}

void Testdynamic_2d_array()
{    dynamic_2d_array < char > My_dynamic_2d_array(3, 20);        
MYARRAY_T TestData = {"hello world", "bla bla","more bla"};    
int i;    
for (i = 0;i < 3;++i)    
{for (int i2 = 0;i2 < 20;++i2)        
{My_dynamic_2d_array[i][i2] = TestData[i][i2];}}        
for (i = 0;i < 3;++i)    
{for (int i2 = 0;i2 < 20;++i2)
{std::cout << My_dynamic_2d_array[i][i2];}        
std::cout << std::endl;}        
SomefFunction(TestData);    
SomefFunction(*((MYARRAY_T*)&My_dynamic_2d_array[0][0]));    
char *data1 = TestData[1];    
const char *data2 = My_dynamic_2d_array[1];        
std::cout << data1 << std::endl;    
std::cout << data2 << std::endl;}

int main()
{
void SomefFunction(char, char);
void Testdynamic_2d_array();


typedef char MYARRAY_T[3][20];

SomefFunction(MYARRAY_T);
Testdynamic_2d_array();

return 0;
}


Thanks a lot
C++ Beginner


Hi Axter,

I have solved some part of the error and define the variable as global so there's no variable error.
But,

43 C:\DEV-CPP\include\dynamic_2d_array.h:30
[Warning] no newline at end of file

I got this error on the header file I think, please enlighten me.

Thanks and Regards
C++ Beginner


>>I got this error on the header file I think, please enlighten me.
That's not an error.  That's a warning.
You can still compile with a warning.  I'm not sure why you got the warning.  I would have to see the complete header.
Hi there,

Thanks for the reply.

Here's my real error. Forgive me for being such a beginner

39 C:\My Documents\dynamic2dtemplate.cpp
parse error before `)' token

For your Info :-

Here's line 39 within my code:-

SomefFunction(MYARRAY_T);

From my understanding, here's how I activate the function.

Do enlighten me in this area

Have a nice weekend !!

Thanks a lot
C++ Beginner




Hi Axter,

Here's the points for you.

https://www.experts-exchange.com/questions/20724404/Points-for-Axter.html

I will add more if there's a need !

Thanks a lot
C++ Beginner