java to C++ , pointer conversion??

C

Cormac O'Donnell

hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}
 
B

Barry White

Hi Cormac,

I'm no expert in C/C++ but I've used it a bit, so hopefully someone else
can correct this if necessary or expand on it.

As far as I know when you create an array in C an amount of memory is
allocated for that array and you are returned a poiter to the first
element (there is no array object). When you increment the pointer by 1,
it then points to the next element in the array. You have to dereference
the pointer if you want to increment the value, but can't remember how
to do that!

In C:

float* s = new float[10];
s += 5; // s points to index 5

In Java:

float[] s = new float[10];
int sIndex = 0;
sIndex += 5; // sIndex can be used to access index 5


So I think a1 is equivelant to my sIndex, s is left so as to always have
a reference to the first elemnt of the array.

Barry
 
D

Dimitri Maziuk

Cormac O'Donnell sez:
nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}

a1 is incremented by 128 and is pointing to s[0], s[128], s[256]...
a2 is incremented by 25: u[0], u[25], u[50]...
FBFilter() presumably uses 128 elements of s[] and 50 elements of
u[], hence passing the pointers rather than single element.

To do this in java, e.g. make a1 and a2 int indexes and also pass
s[] and u[] to FBFilter():
FBFilter( s, 0, u, 0 )
FBFilter( s, 128, u, 25 )
FBFilter( s, 256, u, 50 )
....

Or make s[] and u[] class member variables so that FBFilter() can
get to them.

Dima
 
D

Dale King

Cormac O'Donnell said:
hi,

I have a piece of code implementing the FFT algorithm in c++ and i
have to convert it into java. i am getting stuck trying to understand
these pointers. Anyone who has experience in java and C++, could you
please explain the code below , esp the lines a1+=nFFT2( i know it
increments the a1 variable/pointer by nFFT2 but how does this give the
methos FBFilter new elements of each array). Anyone who could
translate this to java would be a star!!!

Cormac.

nFrames = 249;
nFFT2 = 128;
nChn =25;

float* s=new float[nFrames*nFFT2]; // FFT
float* u=new float[nFrames*nChn]; // FB

// s and u have operations done on them here

a1=s;
float* a2=u;
for(int i=0;i<nFrames;i++) {
FBFilter(a1,a2);
a1+=nFFT2;
a2+=nChn;
}

Here is a java equivalent. Java does not have pointers like C. You cannot
use a single value to reference part of an array. But I think we can get
what you want using multiple dimensions on the arrays, but that depends on
the code in FBFilter. I am assuming here that FBFilter is only looking at
nFFT2 values of the a1 array and nChn values of the a2 array. If not, then
this doesn't work, because that is all I am passing to FBFilter.

float[][] s = new float[nFrames][nFFT2]; // FFT
float[][] u = new float[nFrames][nChn]; // FB

// s and u have operations done on them here

float[][] a1=s;
float[][] a2=u;

for( int i = 0; i < a1.length; i++ )
{
FBFilter( a1[ i ], a2[ i ] );
}

Note that if my assumptions are correct about how FBFilter is working there
is no reason that the C code shouldn't have been written pretty much the
same way using multi-dimensional arrays instead of raw pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top