PRB: Using float/double Function as Subscript Causes Bad Code

ID Number: Q68921

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax buglist7.00

Summary:

SYMPTOMS

Multiple calls to float functions in the same logical statement may

cause the floating-point accumulator to be overwritten if the

result of one of the calls is used as a subscript. This problem may

occur with the Microsoft C Compiler versions 6.0, 6.0a, and 6.0ax,

but does not occur when the /qc (quick compile) option is

specified.

CAUSE

This problem occurs because of the C convention for returning

floating point values. To ensure that this problem never occurs,

you need to change the usual order of evaluation rules. One way to

resolve this problem is to use the PASCAL calling convention.

STATUS

Microsoft has confirmed this to be a problem in C versions 6.0,

6.0a, and 6.0ax. We are researching this problem and will post new

information here as it becomes available.

More Information:

The sample code below illustrates this problem. Function f_one()

returns 0.0 in the floating-point accumulator (__fac). However, the

floating-point accumulator is not saved before f_two() is called. The

function f_two() returns 12.0 in __fac, which overwrites the 0.0

returned by f_one(). As a result, array[12] is changed, instead of

array[0]. As a workaround, the function's return value may be stored

in a temporary variable, or the two functions may be declared as

_pascal.

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

float f_one(void);

float f_two(void);

float fval[20];

void main(void)

{

int i;

// This doesn't work.

fval[(int)f_one()] = f_two();

printf("fval[0] (should equal 12.0) = %f\n", fval[0]);

// This works.

i = (int)f_one();

fval[i] = f_two();

printf("fval[0] (should equal 12.0) = %f\n", fval[0]);

}

float f_one()

{

return((float)0.0);

}

float f_two()

{

return((float)12.0);

}

Additional reference words: 6.00 6.00a 6.00ax