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.

function return issue

I have a function of type integer like:

int function1(int X)

I am doing to the following thing:


if (function1-constant1>constant2)

{

xxxxxx

}

else

{

xxxxxxx

}

I found that the information the function returned inside the if statement is some sort of a random number - every time a different number which has nothing to do with the actual value the function returns. On the other hand if I save it to a variable before the if statement it is the actual value.

Is this a bug in the compiler or something? Because I have never in my life seen something like this.

  • Probably something rather than a compiler bug.

    However, more information is needed to even guess at a cause.  Best would be the smallest possible piece of code that reproduces the problem.

     

    Robert

  • I would like to suugest trying the following to check if the function returns the expected value.

    int function1(int X);

    int value;

    value = function1();

    if ((value-constant1)>constant2)

    {

    xxxxxx

    }

    else

    {

    xxxxxxx

    }

    Thanks and regards,

    Zhaohong

  • I tried that and I had no problem. However, when the function is located in the if statement I keep getting the random numbers. That is why I thought it is a compiler issue or something.

    Note that at this point I could just use this method. However, it would require defining numerous variables because I have at times 10-15 else if statements which all go through different functions...

  • Elad,

    I am not sure how you check the function return value within the if statement. It can only be done in the assembly code level. Fro the the disassembly window, find the instruction for branching to the function. The return value is in Cortex M4 register R0. You can check this register fater the function returns.

    Thanks and regards,

    Zhaohong

  • I have always done that in on other compliers....


    Could you please be more specific as to where I can find specific instructions which explain how to check this register?

  • The debugger should have a view option to display the value of the CPU registers in a window.

    Thanks and regards,

    Zhaohong

  • Sorry I must have not understood you correctly.


    I am not in debugging mode. I am in running mode. I put a condition to check the value to the function as described above. I checked the value before the if statement by putting it into a variable. Then I tried to use it in the if statement - works. When I put the function return value directly into the if statement I get random numbers.

    Thus, if I am supposed to ask for a specific register or address instead of the function inside the if statement could you please explain to me how to do so?

  • This is my if statement:

    if (Trans(2)-10>22)
            {
                if ((g_ui32TickCount/100-DCFILL_timer>30) ||
                        (*DCFILL_ACSTATE==-1))
                {
                    DCFILL_flag=6;
                    DCFILL_timer=g_ui32TickCount/100;
                    Load_Vdc(8,0); // FCVAC
                    Load_Vdc(11,1); // FCACBAL
                    MODE=46;
                }
            }

    Trans is a function of integer type.

    What I find is that the if sometimes go in and sometimes out. Something like a random number. Note that when I do something like:

    A=Trans(2);

    if (A-10>22)
            {
                if ((g_ui32TickCount/100-DCFILL_timer>30) ||
                        (*DCFILL_ACSTATE==-1))
                {
                    DCFILL_flag=6;
                    DCFILL_timer=g_ui32TickCount/100;
                    Load_Vdc(8,0); // FCVAC
                    Load_Vdc(11,1); // FCACBAL
                    MODE=46;
                }
            }

    I have no such problem.

  • Can you try the following?

    if ((Trans(2)-10)>22)

    Thanks and regards,

    Zhaohong

  • A start, but not enough detail yet.

    You will need to show enough to reproduce the problem, I think.

    Reduce it to as small a piece that you can but we probably need to see the whole of the offending code.

     

    Robert


  • Does this help?

    uint32_t Trans(uint32_t bitset)
    {
        uint32_t ulPres_Value_set;

        TransducerRead();

        switch(bitset)
        {
        case 1:
        ulPres_Value_set = ulPres_Value_1;
        break;
        case 2:
        ulPres_Value_set = ulPres_Value_2;
        break;
        case 3:
        ulPres_Value_set = ulPres_Value_3;
        break;
        case 4:
        ulPres_Value_set = ulPres_Value_4;
        break;
        }
        return ulPres_Value_set;
    }

  • That's some help.

    Do you notice what's missing from your switch?  Probably not the problem but should be addressed since you are having problems.

    There are a couple of things I think you need to do.  Keep reducing the size of the code to the smallest that will reproduce the problem and show us the file(s) that will cause it.  If at all possible it is very helpful if you can reduce it to a size we can compile.  If not we are likely only going to be able to take shots in the dark or suggest techniques to find the problem.

    Second, review the generated assembly for the code causing the issue.  If you are getting random values the result should be illuminating.

     

    Robert

  • You're missing the default case.

     

    As I said, that's probably not the cause of your problem but since you do have one you should add something in to catch invalid values.  It could display symptoms like you are seeing.  It only costs you code space and the time to set it up.  At this point only the former could be an issue (you have already spent too much time to consider the time to do it an issue) and even if you are tight for space it is worth sacrificing elsewhere temporarily if you need to.

     

    Robert