Article View: comp.std.c++
Article #2433New treatment of 'for (int i ...'
From: Bob Kline
Date: Mon, 06 Nov 1995 05:02
Date: Mon, 06 Nov 1995 05:02
51 lines
2923 bytes
2923 bytes
I post this comment on the new treatment of the variable i in the for
loop 'for (int i ...)' not so much to say that I disagree with the
change (in theory, anyway, I think it was a correct decision) as a
warning about how much code this will break. I was apprehensive
from the outset when I first discovered the change, but the extent
of the problems to be faced as a result hadn't really hit home until
I undertook the task of building groff for Windows NT. It was a
real eye-opener to see how much reliance there was on the assumption
that the variable's lifetime continues until the end of the scop
enclosing the 'for' loop. The worst was tbl, whose source compiled
without error and linked fine, even produced reasonable-looking
output until you looked closely and found out spanned columns
weren't being handled properly. It took quite a bit of digging to
find the culprit. It looks a lot simpler in this boiled-down
version, but the original function was lengthy and convoluted
enough to make spotting the problem non-trivial:
...
int c = in.get();
...
while (...) {
for (int c = 0; c < ncolumns; c++) {
...
}
for (c = 0; c < ncolumns; c++) { // wrong c!
...
}
}
...
for (;;) {
if (c == tab_char || c == '\n') // oops! c isn't what it's
// supposed to be ...
I'd say it's hard to fault the authors of the original code, who
were just following the advice frequently given to C++ programmers
to delay creation of a variable until as close to its first use as
possible (of course, it would have helped if they had used a
different name for the second variable :->} ).
I expect this is just a small taste of similar fun to come.
--
/*----------------------------------------------------------------------*/
/* Bob Kline Stream International */
/* bob_kline@stream.com formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<47itjt$65m@lhc.nlm.nih.gov>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 06 Nov 95 05:02:02 GMT Approved: fjh@cs.mu.oz.au From: bkline@cortex.nlm.nih.gov (Bob Kline) Newsgroups: comp.std.c++ Subject: New treatment of 'for (int i ...' X-Original-Date: 5 Nov 1995 17:51:57 GMT Organization: National Library of Medicine Message-ID: <47itjt$65m@lhc.nlm.nih.gov> X-Newsreader: TIN [version 1.2 PL2] X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ2W+OEDnX0m9pzZAQHZiQF/YoghDkhJEa+NaeDe0OwzqBOWthO+6x0O HveAmZn8OXUtZXZ9QfoCkRfvP/3ZZEJc =WBNj