Article View: comp.std.c++
Article #3971Re: C++ syntactic trap
From: John R. Williams
Date: Tue, 09 Apr 1996 16:59
Date: Tue, 09 Apr 1996 16:59
55 lines
2994 bytes
2994 bytes
Stephen Vavasis (vavasis@CS.Cornell.EDU) wrote:
> #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!
At the risk of sounding insulting, I wonder if you have been using C++
long. This strikes being similar the the error in the latest
PC-Lint ads ("for (i = 0; i <= 10; i++)"): It looks like it would be easy
to make but does not occur often in practice because programmers who use
the language often are simply not accustomed to writing the incorrect
version and would never do it under normal circumstances.
I agree that it is uusual to allocate a single object of a built-in type
this way, but how do you propose to write this in such a way that such a
warning would not be generated? All I can think of is to warn when *any*
conversion occurs (this one looks pretty explicit to me), forcing you to
write something like this:
char *foo = new char(static_cast<char>(10));
(Actually this rule doesn't seem too bad if applied only to built-in
types...)
--
class JohnWilliams: public Student, public Programmer {
public:
char const *operator&() const { return "johnw@jove.acs.unt.edu"; }
char const *homepage () const { return "http://www.unt.edu/~johnw"; }
};
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4kcrhk$aj1@hermes.acs.unt.edu>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernReferences:
<4k3q4p$lkd@syn.cs.cornell.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: johnw@jove.acs.unt.edu (John R. Williams) Newsgroups: comp.std.c++ Subject: Re: C++ syntactic trap Date: 09 Apr 1996 09:59:37 PDT Organization: University of North Texas Approved: austern@isolde.mti.sgi.com Message-ID: <4kcrhk$aj1@hermes.acs.unt.edu> References: <4k3q4p$lkd@syn.cs.cornell.edu> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 9 Apr 1996 05:11:16 GMT X-Newsreader: TIN [version 1.2 PL2] X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMWqXeky4NqrwXLNJAQGQxQIAj6vfvv0PB/JKV0dnDHtzFdRYZUQwF6dP 8QfGUlZ5orSAVnUhHp6F45K9YhqzAiLyUc/+DQucugBNXehQwAxK9A== =/YFy Originator: austern@isolde.mti.sgi.com