CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   dynamic memory allocation in C++ (http://www.cfd-online.com/Forums/main/5135-dynamic-memory-allocation-c.html)

m. malik September 9, 2002 16:48

dynamic memory allocation in C++
 
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

Steve Amphlett September 10, 2002 04:57

Re: dynamic memory allocation in C++
 
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.

Steve Amphlett September 10, 2002 07:10

Re: dynamic memory allocation in C++
 
Correcting myself slightly...

There is no direct support for dynamically allocatable and resizeable 2d and 3d matrices in C++.

Axel Rohde September 10, 2002 16:09

Re: dynamic memory allocation in C++
 
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.

Markus Lummer September 11, 2002 01:33

Re: dynamic memory allocation in C++
 
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

m. malik September 11, 2002 09:08

Re: dynamic memory allocation in C++
 
Thank you very much, Markus. It works perfectly. -- Malik

Quain Tchew September 11, 2002 21:26

Re: dynamic memory allocation in C++
 
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.


Markus Lummer September 12, 2002 01:12

Re: dynamic memory allocation in C++
 
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

Quain Tchew September 12, 2002 02:15

Re: dynamic memory allocation in C++
 
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);

...........

----------------------

Quain Tchew September 12, 2002 02:24

Re: dynamic memory allocation in C++
 
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);

Quain Tchew September 12, 2002 02:34

Re: dynamic memory allocation in C++
 
sorry

there are still some problems.

Steve Amphlett September 12, 2002 04:24

Re: dynamic memory allocation in C++
 
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.

Steve Amphlett September 12, 2002 04:53

Re: dynamic memory allocation in C++
 
BTW, the relevent comp.lang.c FAQ page can be found online at:

http://www.eskimo.com/~scs/C-faq/s6.html

Quain Tchew September 12, 2002 06:52

Re: dynamic memory allocation in C++
 
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]; }


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