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

            bug682.cpp

1     #include <stdio.h>
2
3     int an[4] = { 1, 2, 3, 4 };
4
5     int sum( int a[4] )
6         {
7         int limit = sizeof(a) / sizeof(int);
8         int result = 0;
9         for( int i = 0; i < limit; i++ )
10            result += a[i];
11        return result;
12        }
13
14    int main()
15        {
16        printf( "%d\n", sum( an ) );
17        return 0;
18        }

The programmer expected that summing the elements of the array would produce the value 10. Instead he got something much smaller. What did he do wrong?


bug682.cpp lint Output

--- Module:   bug682.cpp
                        _
    int limit = sizeof(a) / sizeof(int);
bug682.cpp(7) : Warning 682: sizeof applied to a parameter 'a' whose type is a
    sized array
    _
    }
bug682.cpp(12) : Info 818: Pointer parameter 'a' (line 5) could be declared as
    pointing to const

Reference Manual Explanation

 
682   sizeof applied to a parameter 'Symbol' whose type is a
      sized array  -- If a parameter is typed as an array it is
      silently promoted to pointer.  Taking the size of such an
      array will actually yield the size of a pointer.
      Consider, for example:

                unsigned f( char a[100] ) { return sizeof(a); }

      Here it looks as though function f() will return the value
      100 but it will actually return the size of a pointer,
      which is usually 4.

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

Previous Bug - Bug #681 - May 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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