Multidimentional Arrays and Pointers

Started by
6 comments, last by Nitage 18 years, 6 months ago
Hello. How do i turn this: "int **ptr" to this: "int ptr[4][4]"? Thanks in advance.
This world is ruled by big furry CATS!! :)Meow! :)
Advertisement
Well, you could copy the data as in:

for (int i = 0; i < 4; ++i)  for (int j = 0; j < 4; ++j)    out[j] = in[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.
I was going more in the direction of "new" "delete".
This world is ruled by big furry CATS!! :)Meow! :)
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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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.
This world is ruled by big furry CATS!! :)Meow! :)
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 = 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];};

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!
This world is ruled by big furry CATS!! :)Meow! :)
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.

This topic is closed to new replies.

Advertisement