Click on image to see enlargment

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


1    #include <stdio.h>
2    #include <string.h>
4    char husband[] = "  Charlie";
5    char wife[] = "   Sandra";
7    char *trim( char *s )
8        {         // removing leading blanks
9        char *p = new char[strlen(s)+1];
10       strcpy( p, s );
11       while( *p == ' ' ) p++;
12       strcpy( s, p );
13       delete p;
14       return s;
15       }
17   int main()
18       {
19       printf( "...pleased to announce the wedding of"
20               " %s and %s\n", trim(husband), trim(wife) );
21       return 0;
22    }

Charlie and Sandra, both programmers, decided to program the printing of their wedding announcements but did not use PC-lint or FlexeLint. The program of course bombed. What's going wrong?

bug424.cpp lint Output

--- Module:   bug424.cpp (C++)
    delete p;
bug424.cpp(13) : Warning 424: Inappropriate deallocation (delete) for 'new[] or modified' data

Reference Manual Explanation

424    Inappropriate deallocation (Name1) for 'Name2' data.  -- This message indicates
       that a deallocation (free(), delete, or delete[]) as specified by Name1 is
       inappropriate for the data being freed.  [12, Item 5]

       The kind of data (specified by Name2) is one or more of:
       malloc, new, new[], static, auto, member, modified or constant.
       These have the meanings as described below:

       malloc    data is data obtained from a call to malloc, calloc or realloc.
       new and new[]  data is data derived from calls to new.
       static    data is either static data within a function or external data.
       auto      data is non-static data in a function.
       member    data is a component of a structure (and hence can't be independently freed).
       modified  data is the result of applying pointer arithmetic to some other pointer.  E.g.
                         p = malloc(100);
                         free( p+1 );    // warning
                 p+1 is considered modified.
       constant  data is the result of casting a constant to a pointer. E.g.
                         int *p = (int *) Ox80002;
                         free(p);        // warning

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

Previous Bug - Bug #1539 - May 2006

[ 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.