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


Groups > comp.lang.c > #107356

Re: for-loop scopes

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c
Subject Re: for-loop scopes
Date 2017-03-28 19:15 -0700
Organization A noiseless patient Spider
Message-ID <obf57d$r48$1@dont-email.me> (permalink)
References <ob0drl$agb$1@dont-email.me>

Show all headers | View raw


On 3/23/2017 5:11 AM, BartC wrote:
> This line in the 'struct padding' thread:
>
>     for(long i = 0; i<N; i++)
>
> prompted me to do more tests:
>
>   {
>     double i;                 // scope 1
>     for (int i=0; i<10; ++i)  // scope 2
>         i;                    // scope 2
>         i;                    // scope 1, deliberate misalignment
>
> Is is correct that the 'int i' inside the for-loop header creates a new
> scope for the following single statement, even without the existence of
> a formal {...} block?
>
> I'm just asking because I'm finding it hard to believe that those last
> two final 'i' terms switch types with merely an intervening ';'.
>

Yes it does. Actually, it creates two: one for the whole 'for' statement 
and another, nested one for the loop body. This is related to one of the 
changes in C99.

In C89/90 the only way to create a local scope was to use an explicit 
pair of '{ ... }'. There is simply no reason to create any "implicit" 
local scopes in C89/90. (Primarily because declarations in C are not 
statements, and therefore "unenclosed" object definitions cannot be used 
as a body of 'for', 'while', 'switch', branches of 'if' etc.). Since 
naked object definition cannot be used as a body of 'for', there's no 
issue of limiting its lifetime, there's no need for implicit blocks. End 
of story.

So, in C89/90 your 'for' does not create a local scope for its body. 
This can be verified (and/or exploited) by the following code

     int i;
     for (i = 0; i < 10; ++i)
       sizeof(enum { A });

     i = A;
     /* OK, sees 'A' declared inside 'for' */

     if (sizeof(enum { B }) > 0)
       sizeof(enum { C });
     else
       sizeof(enum { D });

     i = A + B + C + D;
     /* OK, sees 'B', 'C' and 'D' declared inside 'if' */

This code will happily compile in C89/90 mode, i.e. fresh enum constant 
declarations introduced inside the body of 'for', condition and branches 
of 'if' are visible after those statements.

In C99 declarations are not a statements either, but C99 introduced 
_compound literals_. And compound literals brought with them the same 
lifetime issues object definitions would, since compound literals are 
basically definitions of nameless objects. E.g. in this code

     int *p;
     for (i = 0; i < 10; ++i)
       p = (int []) { rand(); }

Should `p` remain valid after the loop? If yes, then this cycle should 
generally spawn 10 compound literals, all of which should remain alive 
till the end of the enclosing block.

C99 decided that this is not how it should work. Instead, lifetime of 
compound literals created in such contexts is limited (in C++ fashion) 
to the confines of an _implicit_ block that now encloses the loop body 
and another implicit block that encloses the whole loop. Similar 
implicit blocks now surround each branch of 'if' and enclose the whole 
'if' as well.

The above code sample with enum declarations made inside 'sizeof' no 
longer compiles in C99.

-- 
Best regards,
Andrey Tarasevich

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 12:11 +0000
  Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 14:14 +0100
    Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 13:45 +0000
      Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 15:48 +0100
      Re: for-loop scopes Robert Wessel <robertwessel2@yahoo.com> - 2017-03-23 23:01 -0500
        Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-24 10:27 +0000
          Re: for-loop scopes jameskuyper@verizon.net - 2017-03-24 10:43 -0700
          Re: for-loop scopes Robert Wessel <robertwessel2@yahoo.com> - 2017-03-26 21:57 -0500
  Re: for-loop scopes Keith Thompson <kst-u@mib.org> - 2017-03-23 08:37 -0700
  Re: for-loop scopes Ike Naar <ike@iceland.freeshell.org> - 2017-03-23 16:31 +0000
  Re: for-loop scopes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-23 10:23 -0700
    Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 18:07 +0000
      Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 20:05 +0100
      Re: for-loop scopes jameskuyper@verizon.net - 2017-03-23 12:33 -0700
      Re: for-loop scopes John Bode <jfbode1029@gmail.com> - 2017-03-24 11:35 -0700
  Re: for-loop scopes Noob <root@127.0.0.1> - 2017-03-23 21:06 +0100
  Re: for-loop scopes Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-03-28 19:15 -0700
    Re: for-loop scopes Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-03-28 19:19 -0700
    Re: for-loop scopes supercat@casperkitty.com - 2017-03-29 13:14 -0700

csiph-web