Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22223 > unrolled thread

What a stupid gcc!

Started byzgene <nospam@nospam.com>
First post2012-06-13 21:41 +0000
Last post2012-06-14 23:21 +0000
Articles 20 on this page of 97 — 29 participants

Back to article view | Back to comp.lang.c


Contents

  What a stupid gcc! zgene <nospam@nospam.com> - 2012-06-13 21:41 +0000
    Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-13 18:01 -0400
      Re: What a stupid gcc! Noob <root@127.0.0.1> - 2012-06-14 13:00 +0200
        Re: What a stupid gcc! Nomen Nescio <nobody@dizum.com> - 2012-06-14 14:33 +0200
          Re: What a stupid gcc! Rui Maciel <rui.maciel@gmail.com> - 2012-06-14 14:46 +0100
          Re: What a stupid gcc! Noob <root@127.0.0.1> - 2012-06-14 18:19 +0200
    Re: What a stupid gcc! Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-13 18:06 -0400
    Re: What a stupid gcc! jt@toerring.de (Jens Thoms Toerring) - 2012-06-13 22:19 +0000
      Re: What a stupid gcc! Rui Maciel <rui.maciel@gmail.com> - 2012-06-14 09:47 +0100
        Re: What a stupid gcc! Rui Maciel <rui.maciel@gmail.com> - 2012-06-14 14:37 +0100
    Re: What a stupid gcc! Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-06-13 17:05 -0600
    Re: What a stupid gcc! Nick Bowler <nbowler@draconx.ca> - 2012-06-14 00:09 +0000
    Re: What a stupid gcc! Varun Tewari <tewari.varun@gmail.com> - 2012-06-14 03:14 -0700
      Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-14 06:56 -0400
      Re: What a stupid gcc! Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-06-17 07:18 +0000
        Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-17 11:47 +0100
          Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-17 09:31 -0400
            Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-18 12:34 +0100
              Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-18 08:18 -0400
                Re: What a stupid gcc! gwowen <gwowen@gmail.com> - 2012-06-18 05:42 -0700
                  Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-18 09:39 -0400
                Re: What a stupid gcc! Rich Webb <bbew.ar@mapson.nozirev.ten> - 2012-06-18 09:06 -0400
                  Re: What a stupid gcc! ImpalerCore <jadill33@gmail.com> - 2012-06-18 07:18 -0700
              Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-19 00:13 -0700
                Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 11:28 +0100
                  Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 07:30 -0400
                  Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-20 00:16 -0700
          Re: What a stupid gcc! Rui Maciel <rui.maciel@gmail.com> - 2012-06-17 15:52 +0100
          Re: What a stupid gcc! Noob <root@localhost> - 2012-06-18 09:07 +0200
          Re: What a stupid gcc! Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-06-18 09:03 +0000
          Re: What a stupid gcc! Andrew Smallshaw <andrews@sdf.lonestar.org> - 2012-06-18 16:02 +0000
            Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-18 19:40 +0100
              Re: What a stupid gcc! Noob <root@localhost> - 2012-06-18 23:52 +0200
              Re: What a stupid gcc! Ike Naar <ike@iceland.freeshell.org> - 2012-06-18 23:09 +0000
                Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 01:30 +0100
                  Re: What a stupid gcc! Ian Collins <ian-news@hotmail.com> - 2012-06-19 13:03 +1200
                    Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 06:34 -0400
                  Re: What a stupid gcc! Ike Naar <ike@sverige.freeshell.org> - 2012-06-19 08:43 +0000
                    Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 11:00 +0100
                      Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 07:05 -0400
                      Re: What a stupid gcc! Ian Collins <ian-news@hotmail.com> - 2012-06-19 23:21 +1200
                        Re: What a stupid gcc! Ian Collins <ian-news@hotmail.com> - 2012-06-19 23:22 +1200
                      Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-19 13:45 +0100
                        Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 15:23 +0100
                          Re: What a stupid gcc! Ike Naar <ike@iceland.freeshell.org> - 2012-06-19 16:14 +0000
                            Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 18:01 +0100
                              Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 13:59 -0400
                              Re: What a stupid gcc! Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-06-19 15:29 -0600
                          Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-19 18:22 +0100
                            Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-19 21:13 +0100
                              Re: What a stupid gcc! Lew Pitcher <lpitcher@teksavvy.com> - 2012-06-19 16:27 -0400
                              Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 16:38 -0400
                                Re: What a stupid gcc! Kenneth Brody <kenbrody@spamcop.net> - 2012-06-20 09:47 -0400
                              Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-19 22:44 +0100
                                Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-21 01:39 -0700
                                  Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-21 10:55 +0100
                                    Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-21 04:11 -0700
                                      Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-21 12:36 +0100
                                        Re: What a stupid gcc! David Brown <david@westcontrol.removethisbit.com> - 2012-06-21 15:33 +0200
                                    Re: What a stupid gcc! Andrew Smallshaw <andrews@sdf.lonestar.org> - 2012-06-21 14:15 +0000
                              Re: What a stupid gcc! Kenneth Brody <kenbrody@spamcop.net> - 2012-06-20 09:37 -0400
                                Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-20 14:54 +0100
                                  Re: What a stupid gcc! Kenneth Brody <kenbrody@spamcop.net> - 2012-06-20 18:19 -0400
                                    Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-21 00:13 +0100
                                      Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 02:09 +0100
                                        Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-21 02:05 -0700
                                        Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-21 11:49 +0100
                                          Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 14:24 +0100
                                          Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-22 00:38 -0700
                                            Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-22 13:37 +0100
                                      Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-21 01:57 -0700
                                        Re: What a stupid gcc! "BartC" <bc@freeuk.com> - 2012-06-21 11:46 +0100
                                      Re: What a stupid gcc! Kenneth Brody <kenbrody@spamcop.net> - 2012-06-21 14:37 -0400
                                        Re: What a stupid gcc! Andrew Smallshaw <andrews@sdf.lonestar.org> - 2012-06-21 19:38 +0000
                                  Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-21 01:41 -0700
                                    Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-21 12:28 +0100
                Re: What a stupid gcc! Kaz Kylheku <kaz@kylheku.com> - 2012-06-19 15:05 +0000
          Re: What a stupid gcc! Ian Collins <ian-news@hotmail.com> - 2012-06-19 09:37 +1200
            Re: What a stupid gcc! ImpalerCore <jadill33@gmail.com> - 2012-06-18 15:18 -0700
            Re: What a stupid gcc! Paul J Gans <gansno@panix.com> - 2012-06-18 23:21 +0000
              Re: What a stupid gcc! Ian Collins <ian-news@hotmail.com> - 2012-06-19 11:26 +1200
                Re: What a stupid gcc! Paul J Gans <gansno@panix.com> - 2012-06-19 01:46 +0000
                  Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 07:19 -0400
                  Re: What a stupid gcc! Stephen Sprunk <stephen@sprunk.org> - 2012-06-19 08:01 -0500
                  Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 09:25 -0400
                    Re: What a stupid gcc! Les Cargill <lcargill99@comcast.com> - 2012-06-19 12:57 -0500
                      Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-19 14:59 -0400
              Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-19 01:36 +0100
                Re: What a stupid gcc! Paul J Gans <gansno@panix.com> - 2012-06-19 01:48 +0000
            Re: What a stupid gcc! Les Cargill <lcargill99@comcast.com> - 2012-06-18 19:50 -0500
            Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-19 00:19 -0700
              Re: What a stupid gcc! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-19 12:56 +0100
                Re: What a stupid gcc! nick_keighley_nospam@hotmail.com - 2012-06-20 00:26 -0700
                  Re: What a stupid gcc! James Kuyper <jameskuyper@verizon.net> - 2012-06-20 06:31 -0400
        Re: What a stupid gcc! Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-06-18 00:18 -0600
          Re: What a stupid gcc! Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-06-18 09:15 +0000
    Re: What a stupid gcc! Kaz Kylheku <kaz@kylheku.com> - 2012-06-14 23:21 +0000

Page 2 of 5 — ← Prev page 1 [2] 3 4 5  Next page →


#22365

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-18 09:39 -0400
Message-ID<jrnb2e$f33$1@dont-email.me>
In reply to#22363
On 06/18/2012 08:42 AM, gwowen wrote:
> On Jun 18, 1:18�pm, James Kuyper <jameskuy...@verizon.net> wrote:
>> I've never seen a GUI editor (other than gvim, which doesn't really count)
>> where I could do all (or even most) of the things I can do with vi commands.
> 
> *cough*
> 
> M-x all-hail-emacs

You're right, of course, emacs is a GUI with powerful commands, and I
forgot about it.
I once spent an entire year using it as my main editor, partly because
it was one of the few available on the platform I was using at the time,
and vi wasn't, but also to give myself a decent chance to get used to
it. I never did. I no longer remember the details, but I remember having
a great deal of trouble remembering the syntax for the emacs equivalent
of vi's global search-and-replace:

	g/pattern/s/original/replacement/g

Whatever the actual syntax was, I found it counterintuitive (not that
I'd claim that the vi syntax is obvious - but I'm used to vi). What was
worse is that I remember having even more trouble locating help
explaining that syntax, and that's a problem with help system, not with
my memory.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#22364

FromRich Webb <bbew.ar@mapson.nozirev.ten>
Date2012-06-18 09:06 -0400
Message-ID<s79ut795nvvhjqf46onb0bt6qhqleln65m@4ax.com>
In reply to#22362
On Mon, 18 Jun 2012 08:18:02 -0400, James Kuyper
<jameskuyper@verizon.net> wrote:

>My favorite editor (vi) is fairly low tech, and available just about
>everywhere I need it; you may be less lucky. I don't think anyone should
>settle for using a editor on a modern (in this case, modern means post
>1980's) system that's not capable of backwards search.
>
>Note: I would not recommend vi for new users; the learning curve is
>quite steep, GUI editors are much easier to use, particularly if built
>into an IDE. However, I've already learned it, and once you've learned
>them, the power of vi's commands can be quite addictive. I've never seen
>a GUI editor (other than gvim, which doesn't really count) where I could
>do all (or even most) of the things I can do with vi commands.

As a long-time vi user (from 'way back in the dark ages) and current
gvim-er, I agree of course. However, Notepad++ (aka npp) is FOSS and
also worth having around. It's pretty decent, as editors go. The visual
diff is quite handy. Windows only, though. http://notepad-plus-plus.org/

And, oh yeah, it will do a backwards search...   ;-)

-- 
Rich Webb     Norfolk, VA

[toc] | [prev] | [next] | [standalone]


#22366

FromImpalerCore <jadill33@gmail.com>
Date2012-06-18 07:18 -0700
Message-ID<f1590012-6848-4729-82ff-6ce5417a15c3@j9g2000vbk.googlegroups.com>
In reply to#22364
On Jun 18, 9:06 am, Rich Webb <bbew...@mapson.nozirev.ten> wrote:
> On Mon, 18 Jun 2012 08:18:02 -0400, James Kuyper
>
> <jameskuy...@verizon.net> wrote:
> >My favorite editor (vi) is fairly low tech, and available just about
> >everywhere I need it; you may be less lucky. I don't think anyone should
> >settle for using a editor on a modern (in this case, modern means post
> >1980's) system that's not capable of backwards search.
>
> >Note: I would not recommend vi for new users; the learning curve is
> >quite steep, GUI editors are much easier to use, particularly if built
> >into an IDE. However, I've already learned it, and once you've learned
> >them, the power of vi's commands can be quite addictive. I've never seen
> >a GUI editor (other than gvim, which doesn't really count) where I could
> >do all (or even most) of the things I can do with vi commands.
>
> As a long-time vi user (from 'way back in the dark ages) and current
> gvim-er, I agree of course. However, Notepad++ (aka npp) is FOSS and
> also worth having around. It's pretty decent, as editors go. The visual
> diff is quite handy. Windows only, though.http://notepad-plus-plus.org/

And thank the good people that created the vi plugin for Visual
Studio.  It makes development in the IDE tolerable.

[toc] | [prev] | [next] | [standalone]


#22392

Fromnick_keighley_nospam@hotmail.com
Date2012-06-19 00:13 -0700
Message-ID<6d12a8eb-ffa1-497b-a2cc-1c4de9f15dd9@googlegroups.com>
In reply to#22359
On Monday, June 18, 2012 12:34:50 PM UTC+1, Bart wrote:
> "James Kuyper" <jameskuyper@verizon.net> wrote in message
> news:jrkm86$43k$1@dont-email.me...
> > On 06/17/2012 06:47 AM, BartC wrote:
> >> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
> >> news:slrnjtr16f.rk7.grahn+nntp@frailea.sa.invalid...

<snip>

> That means looking at every line of code before that point, for a variable
> name that be similar to many others. Declarations at the start of a function
> are easier for low-tech editors (even with one that can't switch quickly 
> between two locations, probably you can run two instances of it).

low tech?! what are you using SOS?

[toc] | [prev] | [next] | [standalone]


#22397

From"BartC" <bc@freeuk.com>
Date2012-06-19 11:28 +0100
Message-ID<jrpk8n$9dj$1@dont-email.me>
In reply to#22392

<nick_keighley_nospam@hotmail.com> wrote in message
news:6d12a8eb-ffa1-497b-a2cc-1c4de9f15dd9@googlegroups.com...
> On Monday, June 18, 2012 12:34:50 PM UTC+1, Bart wrote:
>> "James Kuyper" <jameskuyper@verizon.net> wrote in message
>> news:jrkm86$43k$1@dont-email.me...
>> > On 06/17/2012 06:47 AM, BartC wrote:
>> >> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
>> >> news:slrnjtr16f.rk7.grahn+nntp@frailea.sa.invalid...
>
> <snip>
>
>> That means looking at every line of code before that point, for a
>> variable
>> name that be similar to many others. Declarations at the start of a
>> function
>> are easier for low-tech editors (even with one that can't switch quickly
>> between two locations, probably you can run two instances of it).
>
> low tech?! what are you using SOS?

SOS (if you're talking about the old DEC editor) wasn't so bad for editing
on a paper terminal. (It was sheer luxury compared with one or two others.)

And you could instantly print a hard copy of your function (actually you had
to), and could use that for reference.

But no that's not what I use now.

You can use smart editors, but that can be taken to extremes. It is
desirable for the source code of a language to be stored in a simple,
accessible format, not dependent on clever interpretive tools. For example,
can you print off a copy of a function, direct from a file, and
read it?

Having in-place and block-scope declarations, just crosses that line,
because it can make reading a paper (or dumb) copy more difficult.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#22403

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-19 07:30 -0400
Message-ID<jrpns2$t7j$1@dont-email.me>
In reply to#22397
On 06/19/2012 06:28 AM, BartC wrote:
...
> Having in-place and block-scope declarations, just crosses that line,
> because it can make reading a paper (or dumb) copy more difficult.

I find it easier to find declarations when they're declared closer to
the point of use, not harder. Your point about scanning every line is
more relevant to paper print-outs than for files viewed on the computer,
but it also applies, with equal strength (which isn't very much) when
searching for the start of the function. And when the declarations are
found only at the start of the function, and I have to search through a
larger number of lines, on average, to get there.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#22427

Fromnick_keighley_nospam@hotmail.com
Date2012-06-20 00:16 -0700
Message-ID<c3eef879-ce09-46c4-8987-4b23a08996b0@googlegroups.com>
In reply to#22397
On Tuesday, June 19, 2012 11:28:35 AM UTC+1, Bart wrote:
> <nick_keighley_nospam@hotmail.com> wrote in message
> news:6d12a8eb-ffa1-497b-a2cc-1c4de9f15dd9@googlegroups.com...
> > On Monday, June 18, 2012 12:34:50 PM UTC+1, Bart wrote:

<snip>

> >> That means looking at every line of code before that point, for a
> >> variable name that be similar to many others. Declarations at the 
> >> start of a function are easier for low-tech editors (even with one 
> >> that can't switch quickly between two locations, probably you can 
> >> run two instances of it).
> >
> > low tech?! what are you using SOS?
> 
> SOS (if you're talking about the old DEC editor)

I was. My memory doesn't go far back enough to remmber what it could and couldn't do- but it was the most primitive editor I could think of. 
Oh I've used batch command line editors. They were painful.

> wasn't so bad for editing
> on a paper terminal. (It was sheer luxury compared with one or two others.)
> 
> And you could instantly print a hard copy of your function (actually you had
> to), and could use that for reference.
> 
> But no that's not what I use now.
> 
> You can use smart editors, but that can be taken to extremes.

the editor I normally use does syntax hi-lighting but otherwise I 
wouldn't call it sophisticated. I use Visual Studio and taht can leap 
straight to declations/definitions but taht's merely handy not essential.

> It is
> desirable for the source code of a language to be stored in a simple,
> accessible format, not dependent on clever interpretive tools. 

yes. Since I store source code in text files I'd say I met that 
criterion. Declaring stuff near where it's used is *not* "dependent 
on clever interpretive tools"

> For example,
> can you print off a copy of a function, direct from a file, and
> read it?

well I'd have to select it. I don't do this very often unless the function 
is badly written (I have one that takes up 13 pages). Normally I don't 
need hard copy.

> Having in-place and block-scope declarations, just crosses that line,
> because it can make reading a paper (or dumb) copy more difficult.

Not significantly in my experience. Refactor your code so each function 
fits on one page.

[toc] | [prev] | [next] | [standalone]


#22335

FromRui Maciel <rui.maciel@gmail.com>
Date2012-06-17 15:52 +0100
Message-ID<jrkqva$rqr$1@speranza.aioe.org>
In reply to#22331
BartC wrote:

> But what happens when the code defines a variable two pages into a
> function, but you happen to be reading some code using that same variable
> four pages into the function! Where do you go to find the definition? At
> the start of a function is usually a good place! You don't always know
> that a variable is only used once.
> 
> And with blocks having their own scope (?) the same name can be used more
> than once with different definitions; even more confusing.

Why not let the text editor handle this?  Wouldn't a simple backward search 
for a identifier be enough to handle both these cases?  Some text editors 
even provide a way to run backward searches for the keyword currently under 
the text cursor.  For example, with vim it's only a matter of placing the 
cursor over an identifier and hitting #.


Rui Maciel

[toc] | [prev] | [next] | [standalone]


#22347

FromNoob <root@localhost>
Date2012-06-18 09:07 +0200
Message-ID<4fded39f$0$21914$426a74cc@news.free.fr>
In reply to#22331
BartC wrote:

> But what happens when the code defines a variable two pages into a function,
> but you happen to be reading some code using that same variable four pages 
> into the function! Where do you go to find the definition? At the start of a 
> function is usually a good place! You don't always know that a variable is 
> only used once.

Functions should NEVER be 5 pages long.

> And with blocks having their own scope (?) the same name can be used more
> than once with different definitions; even more confusing.

Wow, I have a solution for that one! Don't reuse variable names
within a function.

And let me blow your mind, within an inner block, you can "shadow"
definitions from outer blocks (declare a new variable with the same
name). And that's bad practice too.

These are QoP (Quality of Programmer) issues.

If one is actively trying to write obfuscated code, nothing
can be done to educate them, except for the clue bat.

cf. also http://www.ioccc.org/

[toc] | [prev] | [next] | [standalone]


#22353

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-06-18 09:03 +0000
Message-ID<slrnjttrnf.rk7.grahn+nntp@frailea.sa.invalid>
In reply to#22331
On Sun, 2012-06-17, BartC wrote:
>
>
> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
> news:slrnjtr16f.rk7.grahn+nntp@frailea.sa.invalid...
>> On Thu, 2012-06-14, Varun Tewari wrote:
>>> Its simply not stupid, its a simple fact the C++/Java allow
>>> decalaration anywhere in the code, but C wants them the first thing
>>> when you code. That's all
>>
>> You could say that "declarations only at the start of block" was a
>> stupid design ... I'm very happy this got changed, and it should
>> probably have happened earlier, when 'const' and such things were
>> introduced.
>>
>> Now all that remains is to educate the users.  Most code I read
>> doesn't even use the C89 "at the start of block" rule. It lumps all
>> the variables at the start of the function -- even those that are only
>> used and only make sense inside some loop, two pages down ...
>
> But what happens when the code defines a variable two pages into a function,
> but you happen to be reading some code using that same variable four pages 
> into the function!

Then it's refactoring time. But IME the usual case is that a few
variables need function scope and the rest need only a quite narrow
scope down inside some loop or if ... else.

I'm not a fan of huge functions (who is?) but with proper variable
scope (and the things that follow: being able to apply 'const' and
initialize them at the declaration) you can make them much more
pleasant to work with.  Breaking down the huge function into smaller
ones is a much bigger effort/risk, especially when maintaining old
code.

/Jorgen

PS. I also agree with what some other posters wrote.

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#22368

FromAndrew Smallshaw <andrews@sdf.lonestar.org>
Date2012-06-18 16:02 +0000
Message-ID<slrn3vfsjtuk8o.oe6.andrews@sdf.lonestar.org>
In reply to#22331
On 2012-06-17, BartC <bc@freeuk.com> wrote:
>
> But what happens when the code defines a variable two pages into a function,
> but you happen to be reading some code using that same variable four pages 
> into the function! Where do you go to find the definition? At the start of a 
> function is usually a good place! You don't always know that a variable is 
> only used once.
>
> And with blocks having their own scope (?) the same name can be used more
> than once with different definitions; even more confusing.

That's precisely the whole point: if you declare a variable inside
a block you DO KNOW that it is only used there and if the identifier
appears elsewhere it must be referring to something else.  There
are many times you want a local variable for a short amount of time
and that's it.  If you declare the variable in the appropriate
block you don't need to mentally keep track of the variable either
before or after that block.  Fewer variables to keep track of
generally means fewer corner cases, fewer uninitialised or outdated
variables, and fewer dangling pointers, whether they are still
relevant or not.

One fairly common idiom that comes to mind would be along the lines of:

if (some_condition()) {
    int retval = operation(object);
    free(object);
    return retval;
}

Why have an extra variable hanging around a whole function for
something so trivial?  What is more, a variable like that is easily
identified by the compiler as being short lived and can be optimised
as such - put into a register or whatever.  It knows that it doesn't
need to preserve its value because you happened to re-use the same
variable for something else later in the function.

-- 
Andrew Smallshaw
andrews@sdf.lonestar.org

[toc] | [prev] | [next] | [standalone]


#22370

From"BartC" <bc@freeuk.com>
Date2012-06-18 19:40 +0100
Message-ID<jrnsqm$4me$1@dont-email.me>
In reply to#22368
"Andrew Smallshaw" <andrews@sdf.lonestar.org> wrote in message 
news:slrn3vfsjtuk8o.oe6.andrews@sdf.lonestar.org...
> On 2012-06-17, BartC <bc@freeuk.com> wrote:
>>
>> But what happens when the code defines a variable two pages into a 
>> function,
>> but you happen to be reading some code using that same variable four 
>> pages
>> into the function! Where do you go to find the definition? At the start 
>> of a
>> function is usually a good place! You don't always know that a variable 
>> is
>> only used once.

> One fairly common idiom that comes to mind would be along the lines of:
>
> if (some_condition()) {
>    int retval = operation(object);
>    free(object);
>    return retval;
> }

In practice, you would define one 'retval' or 'temp' variable at the start, 
and reuse it whenever the same construct occurs.

In the same way I might define int i,j,k; in preparation for any for-loops 
there might be. Then there's one symbol saved in the for-loop construct, 
which in C already needs a lot of syntax (a minimum of 13 symbols), which 
would otherwise contribute little to a reader of the code (you can see 
instantly that each for-loop uses an int loop index, but that's not exactly 
news).

> Why have an extra variable hanging around a whole function for
> something so trivial?  What is more, a variable like that is easily
> identified by the compiler as being short lived and can be optimised
> as such - put into a register or whatever.  It knows that it doesn't
> need to preserve its value because you happened to re-use the same
> variable for something else later in the function.

That's a good point. Although you are then starting to do the compiler's 
job. (Keeping creating new variables throughout the code is a bit like how 
SSA works, where a new, unique variable is created for each assignment.)

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#22375

FromNoob <root@localhost>
Date2012-06-18 23:52 +0200
Message-ID<4fdfa316$0$21936$426a74cc@news.free.fr>
In reply to#22370
BartC wrote:

> In the same way I might define int i,j,k; in preparation for any for-loops 
> there might be. Then there's one symbol saved in the for-loop construct, 
> which in C already needs a lot of syntax (a minimum of 13 symbols), which 
> would otherwise contribute little to a reader of the code (you can see 
> instantly that each for-loop uses an int loop index, but that's not exactly 
> news).

In C99, one would (should) write:

  for (int i = 0; i < N1; ++i)
    for (int j = 0; j < N2; ++j)
      for (int k = 0; k < N3; ++k)
        do_whatever_with(i, j, k);

[toc] | [prev] | [next] | [standalone]


#22378

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-18 23:09 +0000
Message-ID<slrn3vfsjtvd9m.rhm.ike@iceland.freeshell.org>
In reply to#22370
On 2012-06-18, BartC <bc@freeuk.com> wrote:
> "Andrew Smallshaw" <andrews@sdf.lonestar.org> wrote in message 
>> if (some_condition()) {
>>    int retval = operation(object);
>>    free(object);
>>    return retval;
>> }
>
> In practice, you would define one 'retval' or 'temp' variable at the start, 
> and reuse it whenever the same construct occurs.
>
> In the same way I might define int i,j,k; in preparation for any for-loops 
> there might be. Then there's one symbol saved in the for-loop construct, 
> which in C already needs a lot of syntax (a minimum of 13 symbols), which 
> would otherwise contribute little to a reader of the code (you can see 
> instantly that each for-loop uses an int loop index, but that's not exactly 
> news).

Why don't you go one step further and declare
retval, temp, i, j, and k as global variables?

[toc] | [prev] | [next] | [standalone]


#22383

From"BartC" <bc@freeuk.com>
Date2012-06-19 01:30 +0100
Message-ID<jroh87$71q$1@dont-email.me>
In reply to#22378

"Ike Naar" <ike@iceland.freeshell.org> wrote in message 
news:slrn3vfsjtvd9m.rhm.ike@iceland.freeshell.org...
> On 2012-06-18, BartC <bc@freeuk.com> wrote:

>> In the same way I might define int i,j,k; in preparation for any 
>> for-loops
>> there might be.

> Why don't you go one step further and declare
> retval, temp, i, j, and k as global variables?

Because it won't work?

Exactly the same 'i' could be in use simultaneously in different functions.

That's unlikely to be the case with a single local 'i' in a function.

But you can also go too far the other way, and end up with this (modified 
from Noob's example):

 for (int i = 0; i < N1; ++i)
    for (int i = 0; i < N2; ++i)
      for (int i = 0; i < N3; ++i)
        do_whatever_with(i, i, i);    /* ? */

BTW, when using a debugger on such a program (where there are several local 
variables with the same name), how are they identified? 'The 'i' in the 2nd 
block of that 3rd 'if' of that fourth loop'? (I'm implementing this sort of 
stuff at the moment, and this anonymity bothers me.)

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#22387

FromIan Collins <ian-news@hotmail.com>
Date2012-06-19 13:03 +1200
Message-ID<a4a1ecF7l4U3@mid.individual.net>
In reply to#22383
On 06/19/12 12:30 PM, BartC wrote:
>
>
> "Ike Naar"<ike@iceland.freeshell.org>  wrote in message
> news:slrn3vfsjtvd9m.rhm.ike@iceland.freeshell.org...
>> On 2012-06-18, BartC<bc@freeuk.com>  wrote:
>
>>> In the same way I might define int i,j,k; in preparation for any
>>> for-loops
>>> there might be.
>
>> Why don't you go one step further and declare
>> retval, temp, i, j, and k as global variables?
>
> Because it won't work?
>
> Exactly the same 'i' could be in use simultaneously in different functions.
>
> That's unlikely to be the case with a single local 'i' in a function.
>
> But you can also go too far the other way, and end up with this (modified
> from Noob's example):
>
>   for (int i = 0; i<  N1; ++i)
>      for (int i = 0; i<  N2; ++i)
>        for (int i = 0; i<  N3; ++i)
>          do_whatever_with(i, i, i);    /* ? */

Why would anyone do something so foolish?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#22398

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-19 06:34 -0400
Message-ID<jrpkjl$bdn$1@dont-email.me>
In reply to#22387
On 06/18/2012 09:03 PM, Ian Collins wrote:
> On 06/19/12 12:30 PM, BartC wrote:
...
>> But you can also go too far the other way, and end up with this (modified
>> from Noob's example):
>>
>>   for (int i = 0; i<  N1; ++i)
>>      for (int i = 0; i<  N2; ++i)
>>        for (int i = 0; i<  N3; ++i)
>>          do_whatever_with(i, i, i);    /* ? */
> 
> Why would anyone do something so foolish?

There's no upper limit on human stupidity. However, I think that only a
badly-written code generation program is likely to produce code
containing that particular error.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#22395

FromIke Naar <ike@sverige.freeshell.org>
Date2012-06-19 08:43 +0000
Message-ID<slrn3vfsju0eu7.9cu.ike@sverige.freeshell.org>
In reply to#22383
On 2012-06-19, BartC <bc@freeuk.com> wrote:
> "Ike Naar" <ike@iceland.freeshell.org> wrote in message 
> news:slrn3vfsjtvd9m.rhm.ike@iceland.freeshell.org...
>> On 2012-06-18, BartC <bc@freeuk.com> wrote:
>
>>> In the same way I might define int i,j,k; in preparation for any 
>>> for-loops
>>> there might be.
>
>> Why don't you go one step further and declare
>> retval, temp, i, j, and k as global variables?
>
> Because it won't work?
>
> Exactly the same 'i' could be in use simultaneously in different functions.
>
> That's unlikely to be the case with a single local 'i' in a function.
>
> But you can also go too far the other way, and end up with this (modified 
> from Noob's example):
>
>  for (int i = 0; i < N1; ++i)
>     for (int i = 0; i < N2; ++i)
>       for (int i = 0; i < N3; ++i)
>         do_whatever_with(i, i, i);    /* ? */

Yes, that's gone too far the other way and it won't work.
In the inner loop the outer i's are hidden by the inner i.

But this is not a very good argument against declaring variables
close to where they are used. If you would declare i at the start
of the function, it would be a mess as well:

  int i;
  /* ... */
  for (i = 0; i < N1; ++i)
    for (i = 0; i < N2; ++i)
      for (i = 0; i < N3; ++i)
        do_whatever_with(i, i, i);    /* ? */

The mess has nothing to do with the placement of the declarations,
it's a mess because, in one context, one identifier is used for
three purposes.

In

  int i, j, k;
  /* ... */
  for (i = 0; i < N1; ++i)
    for (j = 0; j < N2; ++j)
      for (k = 0; k < N3; ++k)
        do_whatever_with(i, j, k);

vs.

  for (int i = 0; i < N1; ++i)
    for (int j = 0; j < N2; ++j)
      for (int k = 0; k < N3; ++k)
        do_whatever_with(i, j, k);

I don't see why the first variant would be an improvement
over the second one.

[toc] | [prev] | [next] | [standalone]


#22396

From"BartC" <bc@freeuk.com>
Date2012-06-19 11:00 +0100
Message-ID<jrpino$1lj$1@dont-email.me>
In reply to#22395
"Ike Naar" <ike@sverige.freeshell.org> wrote in message
news:slrn3vfsju0eu7.9cu.ike@sverige.freeshell.org...

> In
>
>  int i, j, k;
>  /* ... */
>  for (i = 0; i < N1; ++i)
>    for (j = 0; j < N2; ++j)
>      for (k = 0; k < N3; ++k)
>        do_whatever_with(i, j, k);
>
> vs.
>
>  for (int i = 0; i < N1; ++i)
>    for (int j = 0; j < N2; ++j)
>      for (int k = 0; k < N3; ++k)
>        do_whatever_with(i, j, k);
>
> I don't see why the first variant would be an improvement
> over the second one.

(1) Imagine this 3-loop construction occurs half-a-dozen times in the
function

(2) You would only need one 'int' declaration in the first variant, rather
than 18.

(3) The extra clutter in the second, is only telling you what you've already
guessed,  that these loop indices are all ints.

(4) But if you *did*, at some point, need to change the loop indices from
int to something else, you've got eighteen times as much editing to do.

(5) This would be bad practice, but if you ever found it necessary to goto
from one loop body to another, the loop indices would be undefined.

(6) If you wanted to break out of any of the loops, and needed to refer to
the value of the loop variable outside, it would either not exist, or be
confused with a more global version.

(7) If ever needed to refer to one of the variables, either in a debugger,
or on the phone to somebody, or in a note or email or comment, or even in a
printf caption, then you've got a problem.

Admittedly for the single bit of code in your example, it's not much of an
issue. But for the more general case of having declarations scattered
everywhere, identically-named variables shadowing each other in nested
scopes, identically-named variables from different scopes getting mixed up
(and what *does* happen when you goto from from block to another?), then
I've already pointed out some problems elsewhere.

-- 
Bartc
 

[toc] | [prev] | [next] | [standalone]


#22399

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-19 07:05 -0400
Message-ID<jrpmd5$kiv$1@dont-email.me>
In reply to#22396
On 06/19/2012 06:00 AM, BartC wrote:
> "Ike Naar" <ike@sverige.freeshell.org> wrote in message
> news:slrn3vfsju0eu7.9cu.ike@sverige.freeshell.org...
> 
>> In
>>
>>  int i, j, k;
>>  /* ... */
>>  for (i = 0; i < N1; ++i)
>>    for (j = 0; j < N2; ++j)
>>      for (k = 0; k < N3; ++k)
>>        do_whatever_with(i, j, k);
>>
>> vs.
>>
>>  for (int i = 0; i < N1; ++i)
>>    for (int j = 0; j < N2; ++j)
>>      for (int k = 0; k < N3; ++k)
>>        do_whatever_with(i, j, k);
>>
>> I don't see why the first variant would be an improvement
>> over the second one.
> 
> (1) Imagine this 3-loop construction occurs half-a-dozen times in the
> function
> 
> (2) You would only need one 'int' declaration in the first variant, rather
> than 18.
> 
> (3) The extra clutter in the second, is only telling you what you've already
> guessed,  that these loop indices are all ints.

No, it's also telling you that each loop index will be used, with it's
particular meaning, within a restricted scope.
> 
> (4) But if you *did*, at some point, need to change the loop indices from
> int to something else, you've got eighteen times as much editing to do.

No, in general, if you change the type of one of the loop indexes, you
will NOT need to change all of the other seventeen. That's the reason
why separating them is both possible and desirable.

> (5) This would be bad practice, but if you ever found it necessary to goto
> from one loop body to another, the loop indices would be undefined.

Yes, and a diagnostic is mandatory when that occurs. Making that
diagnostic mandatory is one of the purposes of writing code this way. If
you're changing a variable from having a meaning that's restricted to
one limited scope, into a variable with a wider scope, you should move
the point of declaration to reflect that fact. This is not the kind of
change that should be easy to make without thinking about the consequences.

> (6) If you wanted to break out of any of the loops, and needed to refer to
> the value of the loop variable outside, it would either not exist, or be
> confused with a more global version.

As with point 5, requiring you to move the declaration in order to avoid
the otherwise mandatory diagnostic is precisely the point of writing
code this way. You should have to invest at least a little bit of time
thinking about it whenever you decide to widen the scope of a variable.
When you always declare them with the widest possible scope, it does
make things easier, but in particular it makes it easier to commit
design errors, such as using the same variable name for two unrelated
purposes.

> (7) If ever needed to refer to one of the variables, either in a debugger,
> or on the phone to somebody, or in a note or email or comment, or even in a
> printf caption, then you've got a problem.

No, in general that's not a problem; the context will usually make it
clear which variable you're referring to; if you're referring to it out
of the proper context, that's the problem, not the fact that scope has
been deliberately limited. If the proper context is bigger than the
scope a variable has been given, then the scope is too small, and it's
declaration should be moved to the innermost block that contains all of
the contexts where it is needed; but only the innermost one, not to any
block farther out.

> Admittedly for the single bit of code in your example, it's not much of an
> issue. But for the more general case of having declarations scattered
> everywhere, identically-named variables shadowing each other in nested
> scopes,

Shadowing is something to be avoided in general; by giving each variable
the smallest scope consistent with its intended use, you actually reduce
the chance that another variable with the same name will shadow it (or,
more seriously, be accidentally merged with it by using a single
declaration for both uses).

> identically-named variables from different scopes getting mixed up

Defining variables with the smallest scope consistent with their
intended use prevents mixing them up; declaring them with a wider scope
than they need creates more opportunities for mixing them up.

> (and what *does* happen when you goto from from block to another?),

If the second block is not nested inside the first one, then variables
declared in the first block will no longer be visible. If that's a
problem for some particular variable, then that variable has NOT been
declared with the smallest possible scope consistent with its intended
use; it needs to be moved up to a higher scope that includes both
blocks. The point is, it should be moved up only to the smallest scope
that includes both blocks.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


Page 2 of 5 — ← Prev page 1 [2] 3 4 5  Next page →

Back to top | Article view | comp.lang.c


csiph-web