This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/LAUNCHXL-F28379D: Errors passing a pointer to a structure as a function parameter

Part Number: LAUNCHXL-F28379D
Other Parts Discussed in Thread: C2000WARE

Tool/software: TI C/C++ Compiler

My problem is I am passing a pointer to a structure as a function parameter.  The error occurs when I have passed a pointer to a structure to the function several times, then I change the pointer to another structure on the next function call.  The address that is received in the function as it executes is incorrect.  The received address is different than the address that was passed when the function was. called (the correct address).

Here is an illustration of the problem in the debugger.  Here is the address that is being passed to the function:

And this is what the function receives:

These two views are separated by one press of the F5 "step into" key.  Needless to say, when this function runs it makes a hash of things, writing into the wrong addresses.

The structure looks like this:

/ Define Sweep structures
struct Sweep {
        float value;
        float first;
        float last;
        float delta;
        Uint16 i;
        Uint16 n;
};

struct Sweep VoutSweep;
struct Sweep VinSweep;
struct Sweep VcoSweep;

The function:

int SweepStep( struct Sweep *sweep, int Reset ) {
    int carry1 = 0;
    if (Reset){
        sweep->value = sweep->first;
        sweep->i = 0;
        if (sweep->delta == 0) sweep->delta = 1;            // no divide by zero
        sweep->n = (int)((sweep->last - sweep->first)/sweep->delta + .5);
    } else if (sweep->i==sweep->n){
        carry1 = 1;
        sweep->value = sweep->first;
        sweep->i = 0;
    } else {
        sweep->value += sweep->delta;
        sweep->i++;
    }
    return carry1;
}

The function calls:

           if (carry && vin_sweep_on){
                carry = SweepStep(&VinSweep, 0);    // when previous sweep reaches final value, increment next sweep
            }

            if (carry && vout_sweep_on){
                carry = SweepStep(&VoutSweep, 0);       // sweep returns 1 when it reaches its final value, 0 otherwise
            }

            if (carry && vco_sweep_on){
                carry = SweepStep(&VcoSweep, 0);    // when previous sweep reaches final value, increment next sweep
            }

The program runs through the sweep of SweepStep(&VinSweep, 0) until it wraps around and returns a 1. Then SweepStep(&VoutSweep, 0) runs, with a pointer to a different structure.
That is when the problem occurs.

Another feature of this problem is that it has been difficult to reproduce, as seemingly unrelated changes affect its occurrence. Initially I used the local "carry" variable in both the function making the call
(the B4 state machine function) and in the SweepStep function. I found that if I changed the name of the carry variable in either place, the problem went away. That didn't make sense because both are
local variables, but that was my observation. But now the problem has reoccurred as documented above, and the two variables have different names.

I am running C2000Ware_3.01.00.00 and CCS 9.2.0.00013

  • if (carry && vin_sweep_on){
         carry = SweepStep(&VinSweep, 0);    // THIS RUNS OK!
     }
    
     
     if (carry && vout_sweep_on){
         carry = SweepStep(&VoutSweep, 0);       // THE PROBLEM OCCURS HERE, after carry has been set by the previous SweepStep return.
     }
    
     

  • I tried sprinkling some "volatile" keywords around to see if that would help.

    
    
    / Define Sweep structures
    struct Sweep {
            float value;
            float first;
            float last;
            float delta;
            Uint16 i;
            Uint16 n;
    };
    
    volatile struct Sweep VoutSweep;
    volatile struct Sweep VinSweep;
    volatile struct Sweep VcoSweep;
    ...
    
    //----------------------------------------
    void B4(void) //  Run parameter sweeps
    //----------------------------------------
    {
        volatile int carry=1;
    
    ...
    
    int SweepStep(volatile struct Sweep *sweep, int Reset ) {
        volatile int carry1 = 0;
    

    Since the problem involved the "carry" variable and the "Sweep" structures, I put "volatile" on them. The problem did not reoccur immediately after trying this. But like I said earlier, this problem has come and gone with apparently unrelated changes, so I'm not confident I fixed anything.

  • Hi,

    Can you provide addresses for all the structures defined in the code i.e. VoutSweep, VinSweep and VcoSweep? I would suggest to check the logic in if blocks wherein SweepStep() function is getting called.  Also can you share the application code so that I can reproduce this issue in my setup?

    Thanks

    Vasudha