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


Groups > comp.lang.c > #29337

Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)
Date 2012-12-16 14:51 -0500
Organization A noiseless patient Spider
Message-ID <kal8nm$oe2$1@dont-email.me> (permalink)
References (5 earlier) <0.732b2ccaffb639498a0c.20121215221017GMT.87a9tfdl12.fsf@bsb.me.uk> <kaitq5$n2t$1@dont-email.me> <0.4e0124e757bed0241853.20121216012421GMT.874njmeqm2.fsf@bsb.me.uk> <kajsmp$2a4$1@dont-email.me> <0.f5d24705a97d28e4e8d3.20121216131132GMT.87sj76cfaz.fsf@bsb.me.uk>

Show all headers | View raw


On 12/16/2012 08:11, Ben Bacarisse wrote:
> Shao Miller <sha0.miller@gmail.com> writes:
>
>> On 12/15/2012 20:24, Ben Bacarisse wrote:
>>> Shao Miller <sha0.miller@gmail.com> writes:
>>>
>>>> On 12/15/2012 17:10, Ben Bacarisse wrote:
>>>>> Shao Miller <sha0.miller@gmail.com> writes:
>>>>>
>>>>>> On 12/15/2012 15:47, Ben Bacarisse wrote:
>>>>>>> glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
>>>>>>>
>>>>>>>> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>>>>>>>
>>>>>>>> (snip)
>>>>>>>>
>>>>>>>>> How odd.  What's the point of have both i and i2?  If either gets
>>>>>>>>> altered in the "some code" part then there is an argument for not
>>>>>>>>> re-writing it as a "for" loop.  Generally, readers will expect the
>>>>>>>>> variables mentioned in a "for" loops to be left alone in the body.  This
>>>>>>>>> is not rule of the language but it's a good rule of thumb.
>>>>>>>>
>>>>>>>> I have used "for" loops where I needed "continue" to restart
>>>>>>>> the loop without incrementing the index.
>>>>>>>>
>>>>>>>> Otherwise, I agree.
>>>>>>>
>>>>>>> So when you want that behaviour you'd write:
>>>>>>>
>>>>>>>       for (<initialise>; <condition>;) {
>>>>>>>         <code that uses continue>
>>>>>>>         <increment>
>>>>>>>       }
>>>>>>>
>>>>>>> rather than
>>>>>>>
>>>>>>>       <initialise>
>>>>>>>       while (<condition>) {
>>>>>>>         <code that uses continue>
>>>>>>>         <increment>
>>>>>>>       }
>>>>>>>
>>>>>>> ?  I don't think I would (and I could make hand-waving arguments about
>>>>>>> why I wouldn't) but it's really just a matter to taste.
>>>>>>>
>>>>>>
>>>>>> It can also be a matter of lifetime (for C >= C99), which could
>>>>>> potentially impact memory usage.  By using declarations (but not
>>>>>> compound literals) in the 'for', such objects needn't've storage for
>>>>>> the enclosing block.
>>>>>
>>>>> That does not match my understanding.  Can you provide supporting
>>>>> evidence?
>>>>>
>>>>> Putting a declaration in a for statement (as opposed to just before a
>>>>> while) alters the scope the scope of the name but does not, I think,
>>>>> have any effect on the lifetime of the declared object.
>>>>
>>>> Sure thing.  C99 6.8.5.3p1 with 6.2.4p6.
>>>
>>> These support exactly what I said.  I don't see any support there for
>>> the change in the lifetime that you suggested.
>>>
>>
>> With _much_ apology, I've assumed that N1256.PDF's 6.2.4p6 is C99's.
>> I am referring to C99's VLAs, which obviously introduce a number of
>> quirks (relative to C89/C90).  One of those quirks is that their
>> lifetime and scope are fairly closely tied, and it's relevant for a
>> for' loop's clause-1.  (_Why_ one would declare a VLA in a 'for' loop
>> is a matter of practicality, but _that_ one can is a matter of fact.)
>>
>> Or, I could be missing something.
>
> The "for" and the "while" versions are the same with respect to
> lifetime, with or without VLAs.  The loop pattern you commented on had
> <initialise> but you decided to make a point about the fact that C99
> also permits a declaration there.  That's fine, but your point is still
> wrong.  You must then compare
>
>         for (<declaration>; <condition>;) {
>           <code that uses continue>
>           <increment>
>         }
>
> with
>         <declaration>;
>         while (<condition>) {
>           <code that uses continue>
>           <increment>
>         }
>
> which do not differ significantly in the required lifetime of the
> declared object.
>

How do you "undeclare" the identifier(s) in the 'while' version, after 
the 'while' statement, so as to free up storage?  Do you _need_ to free 
the storage for the identifier(s) in the 'for' version?

It's also a matter of namespace pollution.  Please consider:

   #include <stddef.h>
   #define Countof(array) (sizeof (array) / sizeof *(array))

   int finally(size_t * arr) {
       (void) arr;
       return 0;
     }

   int main(void) {
       int n = 42;
       int i = 13;

       for (size_t i = 0, arr[n]; i < Countof(arr) || finally(arr); ++i)
         arr[i] = i;

       int x = i;
       (void) x;

       return 0;
     }

What value does 'x' land with?  Is the storage for 'arr' required by the 
time 'x' is initialized?  It seems to me that with a 'while', you'd need 
to do some curly brace wrapping.

- Shao Miller

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


Thread

comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Test <test@.nil.invalid.com> - 2012-12-15 15:47 +0200
  Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 14:05 +0000
    Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 20:26 +0000
      Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 20:47 +0000
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Les Cargill <lcargill99@comcast.com> - 2012-12-15 15:34 -0600
          Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-12-16 03:23 -0800
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-15 16:37 -0500
          Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-15 22:10 +0000
            Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-15 17:32 -0500
              Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-15 15:43 -0800
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 02:15 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-16 14:21 -0800
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 18:34 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 01:15 -0800
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-17 10:30 -0800
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 16:07 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 08:13 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-17 10:33 -0800
              Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 01:24 +0000
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 02:19 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 13:11 +0000
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Shao Miller <sha0.miller@gmail.com> - 2012-12-16 14:51 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-16 21:16 +0000
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 00:55 -0800
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-17 15:08 +0000
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 10:40 -0800
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-12-15 22:09 +0000
  Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-15 06:11 -0800
    Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Les Cargill <lcargill99@comcast.com> - 2012-12-15 13:26 -0600
  Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-15 09:24 -0500
    Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-15 12:21 -0800
      Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-15 16:42 -0500
      Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Philip Lantz <prl@canterey.us> - 2012-12-16 00:09 -0800
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-16 08:46 -0500
          Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-16 14:29 -0800
  Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-16 10:30 -0500
  Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-17 12:42 +0200
    Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-17 23:07 +0200
      Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 19:31 -0500
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 01:32 +0000
          Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-17 20:53 -0500
            Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 02:29 +0000
              Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-18 05:48 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-18 11:26 +0000
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-20 16:20 -0500
                Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Keith Thompson <kst-u@mib.org> - 2012-12-18 03:44 -0800
            Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) James Kuyper <jameskuyper@verizon.net> - 2012-12-17 22:13 -0500
              Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-18 05:40 -0500
        Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-20 15:14 +0200
          Re: comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) pete <pfiland@mindspring.com> - 2012-12-20 16:31 -0500

csiph-web