Thread: very strange passing of arguments

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    very strange passing of arguments

    Hi everyone,

    something very strange is happening in a very simple code;
    specifically, I have this function:

    Code:
    void flinspace(float vmin, float vmax, int n, float *VECT)
    {
    	//!!! VECT must be allocated starting from 1 for this function to work properely
    	
    	int i;
    	float dv;
    	
            // WARNING NOTE THIS PRINTING LINE!!!
    	//printf("vmin = %f  vmax =%f\n", vmin, vmax);
    	
            VECT[1] = vmin;
    	for (i=2; i<=n; i++){
    		dv = (vmax - vmin)/(n-1);
    		VECT[i] = VECT[i-1] + dv;
    	}
    
    return;
    }
    now, this function is called by a main function that passes the correct arguments to it as expected. However, here is the very strange behavior:

    although argument 1 and 2 (float vmin, float vmax) are not supposed to be modified within this function, once I comment the "printf" line above (see WARNING NOTE above), vmin and vmax ARE in fact modified randomly within this function, and are returned to the calling function with a different value from what they were assigned;
    on the other hand, when I UNcomment the print line, everythign stands still, and the only variable that is returned with a new value is the expected/wanted VECT.

    Is anyone able to help me understand how this is possible?
    I am on a Mac OSX 10.5 with GCC 4.0.1

    Thank you in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're trashing your memory somewhere else, and you're seeing the side effect of that here. Make a small program with just this function, fix your math again, and it will work fine.
    Code:
    void flinspace(float vmin, float vmax, int n, float *VECT)
    {
        int i;
        float dv;
    
        VECT[0] = (vmax-vmin);
        VECT[1] = VECT[0] + ((vmax - vmin) / 2);
    }
    ...
    float vector[2] = { 1.0, 2.0 }; /* whatever */
    ...
    flinspace( .75, .33, /* not used */ 0, vector );
    Something like that. I don't know what you're planning on passing, but I don't see that you really need a loop there. Anyway, you're probably trashing your memory some place else.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi, thanks for replying and trying to help.

    I indeed freed some memory from space that was not allcoated. I was allocating certain previous arrays with npts elements without noticing that npts was never assigned beforehand.

    Thank you again; It was too strange to be real!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 01:14 PM
  2. Passing arguments.
    By omnificient in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 02:41 PM
  3. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 08:10 AM