1. Two dimentional array

Is it possible to make a two dimetional dynamic array?
It probably is, but I cant get it to work

2. example:
Code:
```int i;
int sizeX = 10, sizeY = 20;
int **my2DArray;
my2DArray = malloc(sizeX * sizeof(*my2DArray));
// OR: my2DArray = malloc(sizeX * sizeof(*int));
for (i = 0; i < sizeX; i++)
{
my2DArray[i] = malloc(sizeY * sizeof(**my2DArray));
// OR: my2DArray[i] = malloc(sizeY * sizeof(int));
}```

3. >Is it possible to make a two dimetional dynamic array?
Code:
`vector<vector<int> > V(rows, cols);`

4. that should be:
Code:
`vector<vector<int> > V(rows, vector<int>(cols));`
I wouldn't use malloc in C++, and I wouldn't use new either because a container like vector is much better than dynamic allocation.

For one thing, whether you use malloc or new, KONI's example doesn't include the deletion code. The vector example is all you need for creation and cleanup.

5. Considering this is C++ new and delete are more used, but vectors are the way to go, really.

Code:
```int **Allocate_2D_Array( int x, int y )
{
int **Array = new int *[y];

for ( int i=0; i<y; i++ )
Array[i] = new int [x];

return Array;
}

void Dealocate_2d_array ( int **Array, int y )
{
for ( int i=0; i<y; i++ )
delete []Array[i];

delete []Array;
}

int main( void )
{
int x, y;

// Get your user defined array dimensions
cout<< "Enter X: ";
cin >> x;

cout<< "Enter Y: ";
cin >> y;

// Allocate memory
int **p_2d_array = Allocate_2D_Array( x, y );

// Fill array with variables
for ( int j=0; j<y; j++ )
for ( int i=0; i<x; i++ )
p_2d_array[j][i] = i+j;

// print out array
for ( int j=0; j<y; j++ )
{
for ( int i=0; i<x; i++ )
cout<< p_2d_array[j][i] << " ";

cout<< '\n';
}

// delete allocated memory
Dealocate_2d_array( p_2d_array, y );

return 0;
}```
Should work OK if you need to use new/delete. I prefer to make functions to allocate and deallocate the memory cause it makes main() ... nicer.

6. Unless you are forced to do so because you are a student, I cannot think of any reason why you would pick that version over a vector. You are really just reinventing what a vector does for you, except your own version won't work as well.

7. Originally Posted by Daved
that should be:
Code:
`vector<vector<int> > V(rows, vector<int>(cols));`
|
Are you sure Daved? I used to not think what I posted worked, but then I saw someone post that example, tried it, and changed my mind. Maybe it's not conformant to the standard though.

8. I can't say that I'm sure. At first I thought it was wrong because using that form on a one dimensional vector would create a vector of rows ints initialized to cols. However, that cannot happen here since it is declared as a 2-D vector. So what it will try to do is create rows vector<int> objects constructed with cols.

My guess is that it will work if the single int constructor for vector<T> is not explicit, and it will fail if it is. I'm not sure it either way is required by the standard, but I'll check if I have a chance.

BTW, I do get an error in VC++ 7.1 indicating that an int cannot be converted to a const vector<int>& because, "Constructor for class 'std::vector<_Ty>' is declared 'explicit'".

9. >BTW, I do get an error in VC++ 7.1 indicating that an int cannot be converted to a const vector<int>&
Interesting. It compiles with Dev-C++ (g++).

10. My copy of the standard indicates it should be explicit (23.2.4.1), so I suspect gcc is wrong in this case.

Interestingly, in this this link Stroustrup indicates that this exact example was the catalyst for the rule in C++ allowing constructors to be declared explicit: http://www.research.att.com/~bs/dne_errata.html (scroll to pg80 for the second printing).

11. Thanks to all of you for your inputs. I think this forum is the one where most people get the most replies to there question.

I need the two dimentional array for holding the color of a computer generated picture so that I can pass it to my saveFileFunction (Work in progres).

Thanks again

12. >> You are really just reinventing what a vector does for you

Technically it's the other way around isn't it

13. You are really just doing what a vector reinvents for you ?

I'm not very good at logical inverses