Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug1571.cpp

1    #include <stdio.h>
2    #include <math.h>
3    struct complex { float x; float y; };
4    struct complex_polar { float radius; float angle; };
5    complex cx = { 5, 12 };
6    complex_polar &polarize( complex &c )
7        {
8        complex_polar cp = { sqrt( c.x * c.x + c.y * c.y ),
9            atan2( c.y, c.x ) };
10       return cp;
11       }
12   int main()
13       {
14       complex_polar &cp = polarize( cx );
15       printf( "complex number %g + %g i\n", cx.x, cx.y );
16       printf( "in polar coordinates has radius, angle = " );
17       printf( "%g, %g \n", cp.radius, cp.angle );
18       return 0;
19       }

Dimitri, a better mathematician than programmer, is gearing up to do some calculations using complex numbers with polar coordinates and is finding that this program is behaving differently on different machines and different compilers. Can you spot the problem?


bug1571.cpp lint Output

--- Module:   bug1571.cpp (C++)
                                                     _
    complex_polar cp = { sqrt( c.x * c.x + c.y * c.y ),
bug1571.cpp(8) : Info 747: Significant prototype coercion (arg. no. 1) float to double
bug1571.cpp(8) : Info 736: Loss of precision (initialization) (64 bits to 32 bits)
                  _
        atan2( c.y, c.x ) };
bug1571.cpp(9) : Info 747: Significant prototype coercion (arg. no. 1) float to double
bug1571.cpp(9) : Info 747: Significant prototype coercion (arg. no. 2) float to double
bug1571.cpp(9) : Info 736: Loss of precision (initialization) (64 bits to 32 bits)
             _
    return cp;
bug1571.cpp(10) : Warning 1571: Returning an auto variable 'cp' via a reference type

Reference Manual Explanation


1571   Returning an auto variable 'Symbol' via a reference type -- A function that is
       declared to return a reference is returning an auto variable (that is not itself a
       reference).  The auto variable is not guaranteed to exist beyond the lifetime of
       the function.  This can result in unreliable and unpredictable behavior.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #786 - February 2011

Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2011, Gimpel Software, All rights reserved.