Hi, I'm new to C but have some experience in java and I'm wanting to return an array. As I understand it so far you have to return a pointer instead and use that. But I get odd results as if it were pointing at the wrong memory location after I have returned the pointer.
Basically, it gets an array, processes it and then i want to return it.
Code:
float *frameBuffer( float buf[], int bufferSize )
{
int i, k=0;
float frame[FRAMESIZE];
float *frmptr;
frmptr = (float*) malloc( FRAMESIZE * sizeof(float) );
if (!frmptr) {
printf("memory error\n"); exit(1);
}
/*** SOME PROCESSING ON THE ARRAY FRAME USING BUF[] ***/
frmptr = frame;
for (i = 0; i < FRAMESIZE; ++i) {
printf("%d %g \n", i, frmptr[i]);
}
return frmptr;
}
So when i print out all the data from the frmptr in frameBuffer method everything is fine. Now when the method is called and assigns the pointer to another pointer and print out the data I get random values.
Code:
void ProcessAudio( APacket* pkt)
{
short *sbuf = ((AWaveData*)pkt->GetData())->data;
float buf[WAVEPACKETSIZE];
float frame[FRAMESIZE];
float *frmptr;
int i;
frmptr = (float*) malloc( FRAMESIZE * sizeof(float) );
if (!frmptr) {
printf("memory error\n"); exit(1);
}
for (i = 0; i < 5 ++i) {
frmptr = frameBuffer(inputFrameBuffer, INPUTBUFFERLENGTH);
for (i = 0; i < FRAMESIZE; i++) {
// frame[i] = frmptr[i];
// printf("%d %g\n", i, frame[i]);
printf("%d %g\n", i, frmptr[i]);
}
}
}
Even if i try copying the values from frmptr to frame (commented out) this still prints odd results. What is it that I'm doing wrong because this is driving me mad! heh.
Thanks
Steve