Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I want to declare an array z[m][n], m=100,n is an input variable from the dialog box, I use this method:
C++
double **z = new double *[m];
for (i=0;i<m;i++)
{
   z[i]=new double [n];
}
///////////
///////////

for (i=0;i<m;i++)
{
   delete []z[i];
}
delete []z;

is there a better way to declare such an array?
thank you.
Posted
Comments
ThatsAlok 27-Jun-12 2:55am    
Looking something like multidimensional array!
better try vector or list to achieve what actually you looking for.

if you programming in MFC, still use stl as they are more powerful then MFC container classes

what can be better?
another way can be
//i am not saying this is a better way
double *z=new double[m * n]; //then accessing will be tricky
for (int i=0;i<m;i++);
{
 for (int j=0;j<n;j++);
 {
  desired_value=z[i*m+j];
 }
}
delete[] z;//at least in delete you wont have to loop through;
 
Share this answer
 
v3
Comments
Angela2012 26-Jun-12 23:04pm    
In solution 1,z is one dimension,Thank you all the same.
Albert Holguin 26-Jun-12 23:11pm    
Its a trick... to store a multi-dimensional array in a one dimensional array. Not necessary but it's an option.
Albert Holguin 26-Jun-12 23:13pm    
+5, good option...
Mohibur Rashid 26-Jun-12 23:17pm    
Thank you :)
Albert Holguin 26-Jun-12 23:31pm    
Forgot to mention this option would also be significantly faster than the OP's due to the single dynamic allocation versus the looped allocations. See my note to OP below.
You can always use one of the many container classes available (CArray, vector, array, etc...). Whether they are better options? ...depends on your use.

For example, std::vector tends to have better performance in the case when there are continual allocations... because it's been optimized for that (by pre-allocating memory). If your multi-dimensional array is allocated once and doesn't change much, the allocation performance difference is really negligible in comparison. Read up on your options and make up your own mind.

Good luck!

Some references:
http://www.cplusplus.com/reference/stl/vector/[^]
http://www.cplusplus.com/reference/stl/array/[^]
http://msdn.microsoft.com/en-us/library/4h2f09ct(v=VS.80).aspx[^]
 
Share this answer
 
Comments
[no name] 26-Jun-12 23:45pm    
If the dimensions are being input by the user then speed probably not critical. Usability may be enhanced by your suggestion though.
Albert Holguin 26-Jun-12 23:47pm    
Yeah, see my note above about speed v. readability of the code.
Angela2012 27-Jun-12 1:44am    
Thank you for your advice,it is very useful
Albert Holguin 27-Jun-12 9:19am    
You're certainly welcome. :)
// C++, MFC
You could also try to implement the following class :) :
C++
typedef CArray<double> CRowData;
typedef CArray<CRowData> CDblMtx;

class CDblMatrix : private CDblMtx
{
  CSize m_sizeDimensions;

public:
  CDblMatrix(int iRows, int iCols);
  virtual ~CDblMatrix();

  void SetSize(const CSize& cNewSize);
  const CSize& GetSize() const;

  void SetAt(int iRow, int iCol, const double& dVal);
  const double& GetAt(int iRow, int iCol) const;
};
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900