Click on image to see enlargment

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


1    #include <stdio.h>
3    const char *buf = "showers";
5    void h( const char *&q )
6        { q = buf; }
8    int main()
9        {
10       char *p;
12       h( p );
13       printf( "%s\n", buf );
14       p[0] = 'f';   p[1] = 'l';
15       printf( "%s\n", buf );
16       return 0;
17       }

This program converts "showers" into "flowers" which might be nice but since "showers" was initially const how was const integrity shattered?

bug1561.cpp lint Output

--- Module:   bug1561.cpp
    h( p );
bug1561.cpp(12) : Warning 1561: Reference initialization causes loss of
    const/volatile integrity (arg. no. 1)

Reference Manual Explanation

1561  Reference initialization causes loss of const/volatile integrity (Context)  -- 
      A reference initialization is resulting in a capability gain that can cause a 
      loss of const or volatile integrity.

      Typically the message is given on initializing a non-const reference with a 
      const.  For example:

                void f( int &x );
                const int n = 0;


      Here, function f() could assign a value to its argument and thereby modify n, 
      which is declared to be const.

      The message can also be issued when a pointer is initialized.  Consider the 
      following example.

                void h( const int *&q );
                int *p;


      It might seem that passing a regular (i.e., non-const) pointer to a const int * 
      could cause no harm.  That would be correct if it were not for the reference.  
      If function h() were to assign a pointer to const to its parameter q then upon 
      return from the call, p could be used to modify const data.

      There are many subtle cases that can boggle the mind.  See the commentary to Message 605.

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

Previous Bug - Bug #795 - March 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.