Thread: Return an array

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Return an array

    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

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    	float 			frame[FRAMESIZE];
    	float			*frmptr;
    	frmptr = (float*) malloc( FRAMESIZE * sizeof(float) );
    	frmptr = frame;
    Well, you're allocating memory and then you're making frmptr point to the frame array and completely losing the memory you allocated. Just use your frmptr pointer as the array and forget the frame array; you don't need it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    oh, ok i see now. thanks for the help

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    frmptr = (float*) malloc( FRAMESIZE * sizeof(float) );
    That code (above) will work, but have a look at the following, just so you know the preferred way of doing things in C..

    FAQ > Explanations of... > Casting malloc

    ~/

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should also free() the memory you allocate with malloc(). Not that it really matters in a program like that, but it's a good idea to get into the habit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM