CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Very common mistakes with parameter passing

Some common misunderstandings about how parameters work often result in incorrect code
(even a number of textbooks make these mistakes!):

  • The order of evaluation of parameters is not defined in C. For example, in the code:

    
    int square( int a )
    {
        printf("calculating the square of %i\n", a);
        return a * a;
    }
    
    void sum( int x, int y )
    {
        printf("sum = %i\n", x + y );
    }
    
    ....
    
        ....
        sum( square(3), square(4) );
    

    are we hoping the output to be:

        calculating the square of 3     // the output on PowerPC Macs
        calculating the square of 4
        sum = 25
    
    or
    
        calculating the square of 4     // the output on Intel Macs
        calculating the square of 3
        sum = 25
    

    Do not assume that function parameters are evaluated left-to-right. The compiler will probably choose the order of evaluation which produces the most efficient code, and this will vary on different processor architectures.

    (A common mistake is to place auto-incrementing of variables in parameters.)

 


CITS2002 Systems Programming, Lecture 4, p7, 31st July 2024.