> Can somebody explain [to] me how can I use matrixes with dinamic
memory?
I use malloc and free (NOT new & delete).
I want to access to the matrix like Matrix[r][c]
but I don't know how to do that with pointers and malloc & free.
If I declare 4 static matrices of 576x720,
the program breaks down (How is it possible?)
So I must use dinamic memory (I suppose).
Use a class who's operator[] returns a pointer to the type.
Example:
template < class T>
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(const
dynamic_2d_array&src):m_row(src.m_row),m_col(src.m _col),
m_data((src.m_row!=0&&src.m_col!=0)?new T[src.m_row*src.m_col]:NULL){
for(int r=0;r<m_row;++r)for(int c=0;c<m_col;++c) (*this)[r][c] =
src[r][c];
}
~dynamic_2d_array(){if(m_data) delete []m_data;}
inline T* operator[](int i) {return (m_data + (m_col*i));}
inline T const*const operator[](int i) const {return (m_data +
(m_col*i));}
protected:
dynamic_2d_array& operator=(const dynamic_2d_array&);
private:
const int m_row;
const int m_col;
T* m_data;
};
For more information, check out the following link:
http://www.tek-tips.com/faqs.cfm?fid=5575