Click here to Skip to main content
15,042,397 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:
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.
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++);
delete[] z;//at least in delete you wont have to loop through;
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.
Angela2012 26-Jun-12 23:26pm
I am too familiar with "z[i][j]",because it is widely used in math,I will get used to "z[i*m+j]", thank you for your help.
Albert Holguin 26-Jun-12 23:30pm
Forgot to mention... this option will be a lot faster performance-wise... dynamic heap allocations are relatively slow, so the more you have to do in a loop, the slower the loop... but it's all relative. Don't get caught up with optimization if it's a place where it doesn't really matter. Speed at the expense of readability is not necessarily a good trade.
Mohibur Rashid 26-Jun-12 23:38pm
when the length is always same then the above solution will do fine. but if data are variable length then loop through is a solution for memory usage. say if you want to create a huge table. fixed size array would be wastage.
Albert Holguin 26-Jun-12 23:49pm
I never said fixed... I said one allocation versus numerous allocations. It's still dynamic.
Mohibur Rashid 26-Jun-12 23:50pm
Angela2012 26-Jun-12 23:33pm
OK,thank you ,I learn a lot from you.
Mohibur Rashid 26-Jun-12 23:39pm
anytime anything
Malli_S 27-Jun-12 2:31am
delete z;


delete[] z;

as you are deleting an array.
Mohibur Rashid 27-Jun-12 3:02am
well, I am not a c++ programmer, I didn't know that, After your edit I checked. It seems that to delete an array i need to add [] at the end. Thanks :)
[no name] 27-Jun-12 3:43am
Removed sizeof(double) - data type double already.
Fixed i++ to j++
Mohibur Rashid 27-Jun-12 3:51am
point taken
Mohibur Rashid 27-Jun-12 3:54am
I am used to with malloc function
[no name] 27-Jun-12 4:04am
Tricked me for a moment.
Albert Holguin 27-Jun-12 9:21am
I noticed that too, but I think the point still came across. Good job fixing it up though.
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:[^][^][^]
[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 :) :
typedef CArray<double> CRowData;
typedef CArray<CRowData> CDblMtx;

class CDblMatrix : private CDblMtx
  CSize m_sizeDimensions;

  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;

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