# Multidimentional Arrays and Pointers

## Recommended Posts

Hello. How do i turn this: "int **ptr" to this: "int ptr[4][4]"? Thanks in advance.

##### Share on other sites
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 on other sites
I was going more in the direction of "new" "delete".

##### Share on other sites
Quote:
 Original post by ZQJBut 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 on other sites
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 on other sites
Quote:
Original post by TDragon
Quote:
 Original post by ZQJBut 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 on other sites
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 on other sites
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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628379
• Total Posts
2982344

• 10
• 9
• 15
• 24
• 11