|
[Sponsors] |
September 9, 2002, 17:48 |
dynamic memory allocation in C++
|
#1 |
Guest
Posts: n/a
|
Sorry, this is not a cfd question, but I know there are lots of people now using C++ in cfd programming.
Is there any way of dynamic memory allocation for a 2 or 3 d matrix in C++? I'm not looking for allocating the memory by defining a matrix as an array (1 d matrix), but something keeping the matrix as it is. Thanks. - mm |
|
September 10, 2002, 05:57 |
Re: dynamic memory allocation in C++
|
#2 |
Guest
Posts: n/a
|
There is no direct support for 2d or 3d matrices in either bare C++ or it's STL (standard template library).
This is one of the reasons you'll get loads of hits if you do a web search for "C++ matrix class". Trouble is, most of them aren't quite what you want (too much or too little functionality) and people generally want cash for them. |
|
September 10, 2002, 08:10 |
Re: dynamic memory allocation in C++
|
#3 |
Guest
Posts: n/a
|
Correcting myself slightly...
There is no direct support for dynamically allocatable and resizeable 2d and 3d matrices in C++. |
|
September 10, 2002, 17:09 |
Re: dynamic memory allocation in C++
|
#4 |
Guest
Posts: n/a
|
Any matrix, whether 2-D, 3-D, 4-D, or higher, is ultimately stored in RAM as a 1-D structure. Computer memory is 1-D. Only through the sophisticated use of multiple pointers can you create a higher-dimensional data structure.
|
|
September 11, 2002, 02:33 |
Re: dynamic memory allocation in C++
|
#5 |
Guest
Posts: n/a
|
In C++ you can try the following:
void main() { double **mat2,***mat3; int i,j,k,n1,n2,n3; // Allocation of a 2 and 3 dimensional matrix n1 = n2 = n3 = 10; mat2 = new double*[n1]; mat3 = new double**[n1]; for (i=0; i<<n1>n1; i++) { mat2[i] = new double[n2]; mat3[i] = new double*[n2]; for (j=0; j<<n2>n2; j++) mat3[i][j] = new double[n3]; } // Access to matrix elements for (i=0; i<<n1>n1; i++) for (j=0; j<<n2>n2; j++) { mat2[i][j] = 1; for (k=0; k<<n3>n3; k++) mat3[i][j][k] = 2; } // Deallocation for (i=0; i<<n1>n1; i++) { for (j=0; j<<n2>n2; j++) delete [] mat3[i][j]; delete [] mat3[i]; delete [] mat2[i]; } delete [] mat2; delete [] mat3; } Best regards Markus |
|
September 11, 2002, 10:08 |
Re: dynamic memory allocation in C++
|
#6 |
Guest
Posts: n/a
|
Thank you very much, Markus. It works perfectly. -- Malik
|
|
September 11, 2002, 22:26 |
Re: dynamic memory allocation in C++
|
#7 |
Guest
Posts: n/a
|
I met the same problem. But mine is more difficult. In your code, the n1,n2,n3 is known by the compiler. What should I do when they are not const? For example, I read grid data from a file. But before that I don't know the number of the total vetex. Code read the size from the grid data file, maybe ni*nj*nk. Then I want to create a 3D array with size of ni*nj*nk. I tried in Visual C++ 6.0. But the operator "new" dosen't surport non-constant size.
|
|
September 12, 2002, 02:12 |
Re: dynamic memory allocation in C++
|
#8 |
Guest
Posts: n/a
|
I can't believe that the new operator does accept only constant integers as parameters. If I have to know the array sizes at compile time, I need no dynamic memory allocation at all and thus no new operator.
I do not know the Visual C++ compiler, but I use this kind of dynamic memory allocation for years with a lot of different C++ compilers on many different machines. By the way, in my program n1,n2, and n3 are not constant. They could be read in from a file. A constant n1 is defined in C++, e.g., by const int n1=10; Best regards Markus |
|
September 12, 2002, 03:15 |
Re: dynamic memory allocation in C++
|
#9 |
Guest
Posts: n/a
|
I've crack te problem. Just like the following: ----------------------
FILE * f; int i,j,k; float x,y,z; //point *points; f=fopen("grid.dat","r"); fscanf(f,"%d %d %d",&i,&j,&k); const int ni=i; const int nj=j; const int nk=k; printf("%d,%d,%d\n",ni,nj,nk); //read the size of grid point ***points = (point ***)new point[ni*nj*nk]; //create 3d array. point is a class for(k=0;k<nk;k++) for(j=0;j<nj;j++) for(i=0;i<ni;i++) { fscanf(f," %f %f %f",&x,&y,&z); ........... ---------------------- |
|
September 12, 2002, 03:24 |
Re: dynamic memory allocation in C++
|
#10 |
Guest
Posts: n/a
|
these code is not needed. ---------------- fscanf(f,"%d %d %d",&i,&j,&k);
const int ni=i; const int nj=j; const int nk=k; ---------------- new operater does surport non-constant size just use -------------------- int ni,nj,nk; fscanf(f,"%d %d %d",&ni,&nj,&nk); |
|
September 12, 2002, 03:34 |
Re: dynamic memory allocation in C++
|
#11 |
Guest
Posts: n/a
|
sorry
there are still some problems. |
|
September 12, 2002, 05:24 |
Re: dynamic memory allocation in C++
|
#12 |
Guest
Posts: n/a
|
This is just simple C pointer/array equivalence stuff. You'd get much more help by posting to comp.lang.c. Just don't mention you're using a C++ compiler.
If you want to make use of C++ features, make yourself a Matrix class and use STL vectors in it. Then you can do nice overloads to make your code look like your equations. |
|
September 12, 2002, 05:53 |
Re: dynamic memory allocation in C++
|
#13 |
Guest
Posts: n/a
|
BTW, the relevent comp.lang.c FAQ page can be found online at:
http://www.eskimo.com/~scs/C-faq/s6.html |
|
September 12, 2002, 07:52 |
Re: dynamic memory allocation in C++
|
#14 |
Guest
Posts: n/a
|
Thanks. Now I've known how to do it.
point *** points = new point**[ni]; for (i=0;i<ni;i++) { points[i] = new point*[nj]; for (j=0;j<nj;j++) points[i][j] = new point[nk]; } |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
about numeca harmonic method Memory allocation failed | uqzcpo | Fidelity CFD | 2 | October 7, 2011 23:10 |
Problems with dynamic memory allocation | Syed | Siemens | 0 | July 23, 2006 13:02 |
Memory allocation problem | shhe | CFX | 4 | January 7, 2004 00:00 |
memory allocation | strudl | CFX | 1 | October 5, 2003 11:26 |
Dynamical memory allocation for multiblock? | peter.zhao | Main CFD Forum | 2 | May 20, 2002 05:58 |