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

            bug684.cpp

1     #include <stdio.h>
2     int x[10] = { 4, 6, 8, 6, 4, 3, 5, 9, 2, 8 };
3     void swap( int &n, int &m ) { int k = n; n = m; m = k; }
4     void sort( int **p, const int a[10] )
5         {
6         int b[10];
7         int i, j;
8         for( i = 0; i < 10; i++ ) b[i] = a[i];
9         for( i = 0; i < 10; i++ )
10            for( j = i+1; j < 10; j++ )
11                if( b[i] > b[j] ) swap( b[i], b[j] );
12        *p = b;
13        }
14    int main()
15        {
16        int i, *y;
17        sort( &y, x );
18        for( i = 0; i < 10; i++ ) printf( "%d\n", y[i] );
19        return 0;
20        }

Our programmer wrote a sort function to sort an array without modifying the original. But something's wrong. The first element is right but not the other 9. What's going on?


bug684.cpp lint Output

--- Module:   bug684.cpp
          _
    *p = b;
bug684.cpp(12) : Warning 684: Passing address of auto variable 'b' into caller
    space

Reference Manual Explanation

 
684   Passing address of auto variable 'Symbol' into caller
      space  -- The address of an auto variable was passed via
      assignment into a location specified by the caller to the
      function.  For example:

                void f( int *a[] )
                   {
                   int n;
                   a[1] = &n;
                   }

      Here the address of an auto variable (n) is being passed
      into the second element of the array passed to the
      function f.  This looks suspicious because upon return the
      array will contain a pointer to a variable whose lifetime
      is over.  It is possible that this is benign since it
      could be that the caller to f() is merely passing in a
      working space to be discarded upon return.  If this is the
      case, you can suppress the message for function f() using
      the option

                -efunc(684,f)

      See also Warning  604.

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

Previous Bug - Bug #682 - June 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2003, Gimpel Software