Sign in to follow this  
Ancient Spirit

Multidimentional Arrays and Pointers

Recommended Posts

ZQJ    496
Well, you could copy the data as in:


for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
out[i][j] = in[i][j];


But if you mean by pointer conversion you can't. An int** is a pointer to an array of int* (i.e. pointers), where an int[4][4] is just an array of 16 integers accessed as a 2D array, so their memory representation is just fundementally different.

Share this post


Link to post
Share on other sites
TDragon    679
Quote:
Original post by ZQJ
But if you mean by pointer conversion you can't. An int** is a pointer to an array of int* (i.e. pointers), where an int[4][4] is just an array of 16 integers accessed as a 2D array, so their memory representation is just fundementally different.


I'm not quite sure what you mean here, but it doesn't seem to be correct; C and C++ allow quite a bit of interchangeability between arrays and pointers.

I'm also not quite sure what the OP means, so I can't really answer the question. Perhaps a better explanation of the circumstances would help.

Share this post


Link to post
Share on other sites
Ancient Spirit    151

template <class Type, size_t Rows, size_t Cols>
class Matrix {
public:
Matrix() { InitMatrix(); }
Matrix(const Matrix &mat) { CopyMatrix(mat); }
Matrix(const Matrix **mat) { CopyMatrix(mat); }
~Matrix() { Destroy(); }
private:
void InitMatrix();
void CopyMatrix(Matrix &mat);
void CopyMatrix(Type **mat);
void Destroy();

Type **m_pMatrix;
};



I have this class, and i want to allocate the m_pMatrix to the size of the Rows and Cols. Hope it is good enought of an explanation.

Share this post


Link to post
Share on other sites
ZQJ    496
Quote:
Original post by TDragon
Quote:
Original post by ZQJ
But if you mean by pointer conversion you can't. An int** is a pointer to an array of int* (i.e. pointers), where an int[4][4] is just an array of 16 integers accessed as a 2D array, so their memory representation is just fundementally different.


I'm not quite sure what you mean here, but it doesn't seem to be correct; C and C++ allow quite a bit of interchangeability between arrays and pointers.

I'm also not quite sure what the OP means, so I can't really answer the question. Perhaps a better explanation of the circumstances would help.


Well an int[4][4] can be converted to an int*[4] (I'm not sure if I've got the syntax for that right). But the data is still 16 consecutive ints in memory. The data for an int** doesn't need to be consecutive so you can't do the conversion. In the other direction, int[4][4] contains no pointers so you can't convert it to an int**.

Anyway:


m_pMatrix = new Type* [Rows];
for (int i = 0; i < Rows; ++i)
m_pMatrix[i] = new Type [Cols];


You may want to swap Rows and Cols depending on how you store the matrix (GL or D3D style). But since the memory is always going to be allocated exactly once, why not just do this:


template<class Type, int Rows, int Cols>
class Matrix
{
...
Type m_Matrix[Rows][Cols];
};

Share this post


Link to post
Share on other sites
Ancient Spirit    151
The thing with m_mat[rows][cols] is the it is stored in the stack which has not so much space and could be filled easily if use some large matrices... on the other hand the **m_pMatrix is stored in the heap which has much more space.

Thanks alot!

Share this post


Link to post
Share on other sites
Nitage    1107
Don't declare m_pMatrix as a Type** - declare it as m_pMatrix[Rows][Cols]; seeing as they're both compiler time constants.

But if you're using it for graphics you should probably store your array as a TYPE[rows*cols]


Quote:

The thing with m_mat[rows][cols] is the it is stored in the stack


Only if your matrix itself is stored on the stack. If created on the heap, either by new or as a member of another class on the heap, then it will all go on the heap.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this