🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3966

Re: C++ syntactic trap

#3966
From: J. Kanze
Date: Tue, 09 Apr 1996 13:22
79 lines
3768 bytes
In article <4k3q4p$lkd@syn.cs.cornell.edu> vavasis@CS.Cornell.EDU
(Stephen Vavasis) writes:

|> I have just spent a long time tracking down a mysterious bug (the
|> heap-trashing variety of bug) in my program caused by a syntactic trap
|> in C++.  The troubling thing about this trap is that none of the unix
|> compilers I tried (gcc-2.7.2, Sun SC3.0.1, HP-UX cfront 3.0.3) issued
|> a warning about the mistake, even on the highest warning level.  Only
|> Visual C++4.0 observed that there might be a problem.  Here is an
|> example of the trap.  This program asks the user how many asterisks,
|> and then prints out that many asterisks.

|> #include <iostream.h>
|> int main() {
|>   cout << " How many *'s? ";
|>   int sz; cin >> sz;
|>   char* a = new char(sz + 1); // bug is here, but syntax is legal.
|>   for (int i = 0; i < sz; i++)
|>     a[i] = '*';
|>   a[sz] = 0;
|>   cout << a << endl;
|>   delete[] a;
|>   return 0;
|> }

|> (Does everyone see the error?  I did not, even after staring at my
|> code for a long time.  The point is that the marked statement is an
|> unintended cast from int to char because I used () instead of [].)

|> I would like to make a plea to the compiler-writers who read this
|> group: please issue warnings for syntactic trouble spots!  Implicit
|> type conversion probably creates other traps that I haven't thought
|> of.  C++ programmers like me need help from the compiler to navigate
|> the traps!

The problem is that most of the time, it is the initialization of a
single object (the form that you used) which is wanted.  In practice,
one simply doesn't allocate arrays.  Thus, your program would typically
be more like:

    int
    main()
    {
        int             results( EXIT_FAILURE ) ;
        cout << " How many *'s? " << flush ;
        int             sz ;
        cin >> sz ;
        if ( ! cin || sz < 0 )
            cerr << "Illegal input value or EOF" << endl ;
        else
        {
            cout << string( sz , '*" ) << endl ;
            result = EXIT_SUCCESS ;
        }
        return results ;
    }

Even if the purpose of the exercise is to use a loop to fill a vector,
the else branch of the if would be something like:

    {
        vector< char >      a( sz + 1 ) ;
        //  ...
    }

(Note that in this case, the input validation would have to reject the
value of INT_MAX, as well as negative values, in order to avoid a
possible overflow.)
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
                            -- Beratung in industrieller Datenverarbeitung
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <KANZE.96Apr9123729@gabi.gabi-soft.fr>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4k3q4p$lkd@syn.cs.cornell.edu>

Show Full Headers
Complete Article Headers
From: kanze@gabi-soft.fr (J. Kanze)
Message-ID: <KANZE.96Apr9123729@gabi.gabi-soft.fr>
X-Original-Date: 09 Apr 1996 10:37:29 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 09 Apr 96 13:22:25 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: C++ syntactic trap
Organization: GABI Software, Sarl.
References: <4k3q4p$lkd@syn.cs.cornell.edu>
In-Reply-To: vavasis@CS.Cornell.EDU's message of 06 Apr 1996 11:33:39 PST
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMWpkmOEDnX0m9pzZAQG+AgF+PNwlgMZRMfjM4OJ1rVKqLSsvZAYPJTII
 cRFVXAz4Mq8YOdHhraWJQysNe+/Fn2AP
 =MwjO