dynamic memory allocation in C++

 Register Blogs Members List Search Today's Posts Mark Forums Read

 September 9, 2002, 16:48 dynamic memory allocation in C++ #1 m. malik 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, 04:57 Re: dynamic memory allocation in C++ #2 Steve Amphlett 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, 07:10 Re: dynamic memory allocation in C++ #3 Steve Amphlett 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, 16:09 Re: dynamic memory allocation in C++ #4 Axel Rohde 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, 01:33 Re: dynamic memory allocation in C++ #5 Markus Lummer 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; i++) { mat2[i] = new double[n2]; mat3[i] = new double*[n2]; for (j=0; j<n2; j++) mat3[i][j] = new double[n3]; } // Access to matrix elements for (i=0; i<n1; i++) for (j=0; j<n2; j++) { mat2[i][j] = 1; for (k=0; k<n3; k++) mat3[i][j][k] = 2; } // Deallocation for (i=0; i<n1; i++) { for (j=0; j<n2; j++) delete [] mat3[i][j]; delete [] mat3[i]; delete [] mat2[i]; } delete [] mat2; delete [] mat3; } Best regards Markus

 September 11, 2002, 09:08 Re: dynamic memory allocation in C++ #6 m. malik Guest   Posts: n/a Thank you very much, Markus. It works perfectly. -- Malik

 September 11, 2002, 21:26 Re: dynamic memory allocation in C++ #7 Quain Tchew 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, 01:12 Re: dynamic memory allocation in C++ #8 Markus Lummer 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, 02:15 Re: dynamic memory allocation in C++ #9 Quain Tchew 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

 September 12, 2002, 02:24 Re: dynamic memory allocation in C++ #10 Quain Tchew 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, 02:34 Re: dynamic memory allocation in C++ #11 Quain Tchew Guest   Posts: n/a sorry there are still some problems.

 September 12, 2002, 04:24 Re: dynamic memory allocation in C++ #12 Steve Amphlett 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, 04:53 Re: dynamic memory allocation in C++ #13 Steve Amphlett 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, 06:52 Re: dynamic memory allocation in C++ #14 Quain Tchew Guest   Posts: n/a Thanks. Now I've known how to do it. point *** points = new point**[ni]; for (i=0;i

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post uqzcpo NUMECA 2 October 7, 2011 22:10 Syed Siemens 0 July 23, 2006 12:02 shhe CFX 4 January 7, 2004 00:00 strudl CFX 1 October 5, 2003 10:26 peter.zhao Main CFD Forum 2 May 20, 2002 04:58

All times are GMT -4. The time now is 00:07.