Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing 2D/3D arrays to functions in C++ 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
I want to define an 2D/3D arrays in a C++ program and pass it to a function like:

array(dim1,dim2,dim3)

function(float array,int dim1,int dim2,int dim3){

float tmp = new [dim1*dim2*dim3] ?

for (i=0;i<dim1) {
for (j=0;j<dim2) {
for (k=0;k<dim3) {

tmp(i,j,k) = array(i,j,k)...
}
}
}

}

How can I declare those arrays and how can I pass them to a function What is the best thing to do? Declare pointers?

Thanks!
 
one way is to do it as

float*** array = new float[dim1*dim2*dim3];

then you can pass it to the function as a float***

An other way is to malloc but that is not really necessary if you can use new and delete. Also, if you want to declare it as a normal array you will need to specify all but one of the dimentions in the argument and declaration.

I would go with the &quot;new&quot; appraoch

Matt
 
Thanks for your answer! I actually have to convert the following f90 function in C++, what is the best way to do?


subroutine Display_3D(field,mesh1,mesh2,mesh3,dim1,dim2)

include &quot;common_graphics.h&quot;
include &quot;xdisplay.h&quot;

integer dim1,dim2,maxdim,istepi,istepj
real Points(3,dim1,dim2)
real ColorP(4,dim1,dim2)
real Normal(3,dim1,dim2)
real tmp_Points(3,dim1**2)
real tmp_ColorP(4,dim1**2)
real tmp_Normal(3,dim1**2)
integer eee(2,9)
real V1(3),V2(3),dst

real field(dim1,dim2)
real mesh1(dim1,dim2),mesh2(dim1,dim2),mesh3(dim1,dim2)

integer np,id1,id2,in1,in2,in3,in4
real maxf,minf,vc,dl
integer i,j,k,d,last1,last2,istep,jstep
real val,Ptmin1,Ptmax1,Ptmin2,Ptmax2,Ptmax3,Ptmin3

c write(*,*)'dim1,dim2',dim1,dim2


c initializations
Points=0.0
ColorP=0.0
Normal=0.0
tmp_Points=0.0
tmp_ColorP=0.0
tmp_Normal=0.0
eee=0.0
V1=0.0
V2=0.0

c sets the min and max of the field to display (between -1 and 1)

maxf = max(abs(mindisp),abs(maxdisp))
minf = -max(abs(mindisp),abs(maxdisp))

c sets the min and max of the mesh
Ptmax1=maxx
Ptmax2=maxy
Ptmax3=maxz
Ptmin1=minx
Ptmin2=miny
Ptmin3=minz


dl = max(Ptmax1-Ptmin1,Ptmax2-Ptmin2,Ptmax3-Ptmin3)

c normalize points and normals and initialize colors
do j=1,dim2
do i=1,dim1

c for a non uniform grid
Points(1,i,j) = ((mesh1(i,j)-Ptmin1)/dl)*2.0-1.0
Points(2,i,j) = ((mesh2(i,j)-Ptmin2)/dl)*2.0-1.0
Points(3,i,j) = ((mesh3(i,j)-Ptmin3)/dl)*2.0-1.0

Normal(1,i,j) = 0.0
Normal(2,i,j) = 0.0
Normal(3,i,j) = 1.0

ColorP(alpha,i,j) = 1.0
ColorP(red,i,j) = 0.0
ColorP(green,i,j) = 0.0
ColorP(blue,i,j) = 0.0
enddo
enddo

...
 
2D/3D arrays are difficult unless the procedure using them is passed the exact dimensions. The easiest way around this is to treat them as 1-D arrays and do the arithmetic accoridingly.
eg
real Points(3,dim1,dim2)
real mesh1(dim1,dim2)
Points(1,i,j) = ((mesh1(i,j)-Ptmin1)/dl)*2.0-1.0

would be

#define POINTS(x,y,z) Points[((x * dim1) + y) * dim2 + z]
float* Points = new float[3 * dim1 * dim2];
#define MESH1(x,y) mesh1[(x * dim2) + y];
float* Mesh1 = new float[dim1 * dim2];

POINTS(1,i,j) = ((MESH1(i,j) - Ptmin1)/d1)*2.0-1.0;
 
Thanks xwb,

This looks like what I need, is there a way to make the define more generic something like templates?

Fabien
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top