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


1     #include <stdio.h>
3     int *array(int n)
4         {
5         return new int(n);
6         }
8     int main()
9         {
10        int *p = array(10);
11        for( int i = 0; i < 10; i++ )
12            {
13            p[i] = 0;
14            }
15        printf( "%d\n", p[0] );
16        p = array(10);
17        printf( "%d\n", p[0] );
18        return 0;
19        }

This program is producing strange results. With one compiler I get a report that it has performed an illegal operation. With another compiler I get no output. What's going on?

bug1556.cpp lint Output

--- Module:   bug1556.cpp
    return new int(n);
bug1556.cpp(5) : Warning 1556: 'new Type(integer)' is suspicious

Reference Manual Explanation

1556  'new Type(integer)' is suspicious  -- A new expression had
      the form new T(Integer) where type T has no constructor.
      For example:

                new int(10);

      will draw this warning.  The expression allocates an area
      of storage large enough to hold one integer.  It then
      initializes that integer to the value 10.  Could this have
      been a botched attempt to allocate an array of 10
      integers?  Even if it was a deliberate attempt to allocate
      and initialize a single integer, a casual inspection of
      the code could easily lead a reader astray.

      The warning is only given when the type T has no
      constructor.  If T has a constructor then either a
      syntactic error will result because no constructor matches
      the argument or a match will be found.  In the latter case
      no warning will or should be issued.

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

Previous Bug - Bug #1554 - May 2003

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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