Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #519

Re: why do some writers declare and define variables separately

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newspump.sol.net!posts.news.megabitz.net!nnrp3-asbnva.megabitz.net!not-for-mail
From Jens Schmidt <Jens.Schmidt-HH@gmx.de>
Newsgroups comp.lang.c.moderated
Subject Re: why do some writers declare and define variables separately
Date Thu, 24 Oct 2013 20:10:45 -0500 (CDT)
Organization at home
Sender clcm@herd.plethora.net
Approved clc@plethora.net
Message-ID <clcm-20131024-0001@plethora.net> (permalink)
References <clcm-20130918-0002@plethora.net> <clcm-20131003-0002@plethora.net> <clcm-20131008-0006@plethora.net> <clcm-20131022-0001@plethora.net>
User-Agent KNode/4.11.2
Return-Path <mod-submit@uni-berlin.de>
X-Original-to clcm@plethora.net
Content-Type text/plain; charset="ISO-8859-1"
X-Originating-ip 130.133.4.5
Mime-Version 1.0
Delivered-To clcm@localhost.plethora.net
X-Orig-x-trace news.uni-berlin.de xMtwp2FBzez6wlamOGzRDQ7KT5Yykmb+FbEPALKt6UYg==
Content-Transfer-encoding 7Bit
Lines 48
NNTP-Posting-Date 25 Oct 2013 01:10:45 GMT
NNTP-Posting-Host 11481661.news.megabitz.net
X-Trace DXC=BRd0V9J>XRF<`QFh`C@4YM><6FU_Q:4mB^W\Y;gN2lOM;24OKP?1]dAnDRCehO`QFG7d@DE\31QYEe4?]5YdC<jCF7O[G=28e_EgE>Vg8R`oRB:39X^mjV3WC
X-Complaints-To abuse@megabitz.net
Xref csiph.com comp.lang.c.moderated:519

Show key headers only | View raw


ThosRTanner wrote:

> On Tuesday, October 8, 2013 10:38:14 PM UTC+1, James Kuyper wrote:
>> I prefer to leave variables uninitialized if I cannot initialize them
>> with the value they're supposed to have the next time their value is
>> read. That's because any decent compiler will give you a warning if its
>> analysis of the code flow suggests that there's a possibility of the
>> value of that variable being read before it gets written. Initializing a
>>  variable with a value that's not actually intended to be used turns off
>> that warning, because the compiler doesn't know that you don't intend it
>> to be used.
> 
> I have to say I prefer the other way round, and also to keep the
> declarations in as small a scope as possible (i.e. only in the block,
> rather than declaring every variable ever used at the top of the
> function). Given you have a decent compiler, it should be able to tell you
> that you have assigned a value to a variable without using it as well as
> reading before initialising.

Here is an example of an algorithm where you can neither declare the
variable in the innermost block where it is used nor intitialize with a
good value. I have yet to see a compiler which is able to detect that there
is no read-before-write here.

int f (int x, int *ax, int n)
{
    int i;

    assert (0 < x && x < n);
    for (j = 0; j < n; ++j) {
        if (j > x)
            g (i, ax[j]);
        if (j == x)
            i = h (ax[j]);
    }
}

The variable i must be declared before the loop, and inside the read
of i is statically before the write, but never dynamically before a
previous execution of the body did the write. Unfotunately I forgot
what the algorithm was good for. :-(
-- 
Greetings,
  Jens Schmidt
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

why do some writers declare and define variables separately yoodavid <odimegwudavid@yahoo.fr> - 2013-09-18 16:42 -0500
  Re: why do some writers declare and define variables separately mt <mahdert@gmail.com> - 2013-10-03 13:37 -0500
    Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-10-08 16:38 -0500
      Re: why do some writers declare and define variables separately ThosRTanner <ttanner2@bloomberg.net> - 2013-10-22 16:36 -0500
        Re: why do some writers declare and define variables separately Jens Schmidt <Jens.Schmidt-HH@gmx.de> - 2013-10-24 20:10 -0500
        Re: why do some writers declare and define variables separately Francis Glassborow <francis.glassborow@btinternet.com> - 2013-10-24 20:10 -0500
          Re: why do some writers declare and define variables separately Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2013-10-25 19:36 -0500

csiph-web