Click on image to see enlargment

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

            bug866.cpp

1    #include <stdio.h>
2    char word[] = "rotator";
3    bool palindrome()
4        {
5        int i, n;
6        for( i = 0, n = sizeof(word-1) - 1;
7             i <= n; i++, n-- )
8            {
9            if( word[i] != word[n] ) return false;
10           }
11       return true;
12       }
13   int main()
14       {
15       printf( "%s is%s a palindrome\n",
16               word, palindrome() ? "" : " not" );
17       return 0;
18       }

Checking that a simple string will read the same forwards as backwards should be easy, but this program reports that "rotator" is not a palindrome. What's the problem?


bug866.cpp lint Output

--- Module:   bug866.cpp (C++)
                                     _
    for( i = 0, n = sizeof(word-1) - 1;
bug866.cpp(6) : Info 866: Unusual use of 'ptr-int' in argument to sizeof

Reference Manual Explanation


866    Unusual use of 'String' in argument to sizeof  -- An expression
       used as an argument to sizeof() counts as "unusual" if it is not
       a constant, a symbol, a function call, a member access, a
       subscript operation (with indices of zero or one), or a
       dereference of the result of a symbol, scoped symbol, array
       subscript operation, or function call.  Also, since unary '+'
       could legitimately be used to determine the size of a promoted
       expression, it does not fall under the category of "unusual".
       Example:

       char A[10];
       unsigned end = sizeof(A - 1);      // 866; Programmer probably meant
                                          //  'sizeof(A) - 1'
       size_of_promoted_char =
                          sizeof(+A[0]);  // '+' makes a difference here
       size_t s1 = sizeof( end+1 );       // 866: use +end to get promoted type
       size_t s2 = sizeof( +(end+1) );    // OK, we won't complain
       struct B *p;                       // B is some POD.
       B b1;

       memcpy( p, &b1, sizeof(&b1) );     // 866; intended to take sizeof(b1)

       size_t s3 = sizeof(A[0]);          // OK, get the size of an element.
       size_t s4 = sizeof(A[2]);          // 866; Not incorrect, but ...
                                          // unusual in a sizeof().


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


Previous Bug - Bug #592 - February 2010

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 2010, Gimpel Software, All rights reserved.