Click on image to see enlargment

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

            bug785.cpp

1    #include <stdio.h>
2
3    int numbits(unsigned word)  // returns the number of 1 bits in a word
4        {
5        int n = 0;
6        int density[16] = { 0,1,2,2,1,2,2,3,1,2,2,3,2,3,4 };
7
8        while( word > 0 )
9            {
10           n += density[ word & 0xF ];
11           word >>= 4;
12           }
13       return n;
14       }
15
16   int main()
17       {
18       printf( "%d\n", numbits( 0x18F ) );
19       return 0;
20       }

The programmer expected numbits to report that there are six 1's in the integer 0x18F. Instead two different compilers returned a value of 2. What's the problem?


bug785.cpp lint Output

--- Module: bug785.cpp
_
int density[16] = { 0,1,2,2,1,2,2,3,1,2,2,3,2,3,4 };
bug785.cpp(6) : Info 785: Too few initializers for aggregate 'density'

Reference Manual Explanation

 
785   Too few initializers for aggregate  -- The number of initializers in a 
      brace-enclosed initializer was less than the number of items in the 
      aggregate.  Default initialization is taken.  An exception is made with 
      the initializer {0}.  This is given a separate message number in the 
      Elective Note category ( 943).  It is normally considered to be simply a 
      stylized way of initializing all members to 0.

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

Previous Bug - Bug #1752 - August 2004

[ new ] 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 2006, Gimpel Software, All rights reserved.