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


Groups > comp.lang.c > #148766

Re: for-loop mutates to an endless-loop

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: for-loop mutates to an endless-loop
Date 2020-01-04 19:28 +0100
Organization A noiseless patient Spider
Message-ID <quqlg0$mc0$1@dont-email.me> (permalink)
References <h78q9lF3mhkU1@mid.individual.net> <qunfda$gce$1@dont-email.me> <h7bogeFmh2bU1@mid.individual.net> <87d0bznqh9.fsf@bsb.me.uk> <h7c15uFobghU1@mid.individual.net>

Show all headers | View raw


On 04/01/2020 18:43, Walter H. wrote:
> On 04.01.2020 17:20, Ben Bacarisse wrote:
>> "Walter H."<Walter.H-Nntp@mathemainzel.info>  writes:
>>
>> Most (all?) 64-bit Linux systems have 64-bit long.
> 
> I see, and this fact reduces the portability to a minimum;
> 64-bit Windows systems have 32-bit long.

I think you have that backwards - the fact that MS decided to make 
"long" on 64-bit Windows merely 32-bit, when the rest of the 64-bit 
world had 64-bit long, reduced portability.

But it is still a very long way from being a "minimum" of portability.

The C standard says that "long int" must be a /minimum/ of 32-bit, but 
places no upper limits.  It also says "long long int" must be a minimum 
of 64-bit, and I have not heard of any C99 implementations with anything 
other than 64-bit for "long long".

When you want portability of exact sizes, use the <stdint.h> types - 
that is what they are for.

> 
>>>> If it's important to you that
>>>> it be an exactly 32-bit int, use int32_t.
>>>
>>> mostly there are other types either #define or typedef's like
>>> DWORD, BYTE, WORD, ...
>>
>> What do you mean by mostly?
> 
> most times

That is not any better.  On most systems, these types are not defined at 
all.  And on systems where they are defined, "WORD" might reasonably be 
16-bit or 32-bit.  (It could be anything else, but those are the common 
choices.)  The term "byte" is defined in the C standards as the minimum 
size of data that can be addressed - on some devices, this is bigger 
than 8-bit (I've used C on devices with 16-bit "bytes" and 32-bit 
"bytes").  In common usage, "byte" is always 8 bits - but that might not 
apply in the context of C programming.

I have also see types with those kinds of names as both signed integer 
types and unsigned integer types.

So while those names might be reasonable enough in a very specific 
context - such as non-portable Windows programming - they are hopeless 
for general coding or portable coding.

> 
> programmes are not only standard C, they use APIs of 3rd party
> like Win32 API, ...
> 

Of course - most C code is at least somewhat limited in its portability, 
and there is not problem with that (as long as you are aware of the 
fact).  But you should not use type names, or make assumptions, that 
pointlessly limit portability and make code harder to understand for others.

>> Those are provided by non-standard header files.
> of course;
> if we were using only standard header files there wouldn't be any 
> programme having a graphical interface ...
> just to think about this;
> 
>> From what you wrote below, I think you mean this is a mistake in C.
> yes
> 
>>>> The condition expression for that loop is "i32Iter<= 2147483647". A C
>>>> for loop terminates when the specified condition becomes false. For 
>>>> what
>>>> value of i32iter do you expect that condition to be false?
>>>
>>> already understood; in other words: the language that is nearest to
>>> the system can't handle the most primitive 'structures'; why?
>>
>> It can.  Several ways to do this have been posted.
> 
> yes but they look weired as I found one myself at the OP;
> 
> this one looks weired too
> [code]
> #include <stdio.h>
> #define ANYVALUE 0x0815
> int main( int argc, char* argv[ ] )
> {
>      short int nIter = ANYVALUE;
>      long lSum = 0;
>      while ( ++nIter != ANYVALUE )
>          lSum += nIter;
>      printf( "sum: %ld\n", lSum );
>      return 0;
> }
> [/code]

That is not a good way to write the code - precisely because relying on 
wrapping (or "overrun") is obfuscated, as well as 
implementation-dependent.  (And if the platform has "int" and "short 
int" the same size, it is undefined behaviour.)

> and the result of 'sum: -34837' needs an explanation;
> to rely on an overrun is a bit obfuscated, I'd say;

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


Thread

for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-03 13:27 +0100
  Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-03 14:01 +0100
  Re: for-loop mutates to an endless-loop Dan Purgert <dan@djph.net> - 2020-01-03 13:18 +0000
    Re: for-loop mutates to an endless-loop James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-03 08:28 -0500
      Re: for-loop mutates to an endless-loop Dan Purgert <dan@djph.net> - 2020-01-03 13:33 +0000
        Re: for-loop mutates to an endless-loop James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-03 21:10 -0500
    Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-03 15:34 +0100
  Re: for-loop mutates to an endless-loop James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-03 08:26 -0500
    Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-04 16:15 +0100
      Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-04 17:17 +0100
      Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 16:20 +0000
        Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-04 18:43 +0100
          Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-04 19:28 +0100
            Re: for-loop mutates to an endless-loop Bart <bc@freeuk.com> - 2020-01-04 18:59 +0000
              Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-05 14:24 +0100
            Re: for-loop mutates to an endless-loop Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-04 13:47 -0800
              Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-05 14:26 +0100
              Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-06 20:31 +0100
          Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 22:25 +0000
      Re: for-loop mutates to an endless-loop Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-04 14:08 -0800
      Re: for-loop mutates to an endless-loop James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-04 21:32 -0500
        Re: for-loop mutates to an endless-loop scott@slp53.sl.home (Scott Lurndal) - 2020-01-05 21:48 +0000
      Re: for-loop mutates to an endless-loop James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-04 21:43 -0500
  Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 14:47 +0100
    Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 14:39 +0000
      Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 15:42 +0100
        Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 15:07 +0000
          Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 16:18 +0100
            Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-03 16:47 +0100
              Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 20:41 +0000
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 01:05 +0100
            Re: for-loop mutates to an endless-loop gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-03 18:30 +0000
        Re: for-loop mutates to an endless-loop Robert Wessel <robertwessel2@yahoo.com> - 2020-01-04 00:23 -0600
          Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 08:10 +0100
            Re: for-loop mutates to an endless-loop scott@slp53.sl.home (Scott Lurndal) - 2020-01-04 16:41 +0000
              Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 17:44 +0100
          Re: for-loop mutates to an endless-loop Bart <bc@freeuk.com> - 2020-01-04 11:15 +0000
            Re: for-loop mutates to an endless-loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-04 12:33 +0000
              Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-04 15:59 +0100
              Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 16:01 +0100
                Re: for-loop mutates to an endless-loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-04 16:04 +0000
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 17:06 +0100
                Re: for-loop mutates to an endless-loop scott@slp53.sl.home (Scott Lurndal) - 2020-01-04 16:42 +0000
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 17:44 +0100
            Re: for-loop mutates to an endless-loop Robert Wessel <robertwessel2@yahoo.com> - 2020-01-04 17:55 -0600
              Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-06 20:43 +0100
                Re: for-loop mutates to an endless-loop scott@slp53.sl.home (Scott Lurndal) - 2020-01-06 19:50 +0000
                Re: for-loop mutates to an endless-loop Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-01-06 15:37 -0500
                Re: for-loop mutates to an endless-loop scott@slp53.sl.home (Scott Lurndal) - 2020-01-06 21:59 +0000
                Re: for-loop mutates to an endless-loop Robert Wessel <robertwessel2@yahoo.com> - 2020-01-06 17:33 -0600
      Re: for-loop mutates to an endless-loop gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-03 18:31 +0000
        Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 01:06 +0100
          Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 01:38 +0000
            Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 08:10 +0100
              Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 16:39 +0000
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 17:41 +0100
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-04 17:49 +0100
                Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 22:49 +0000
                Re: for-loop mutates to an endless-loop Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-05 03:44 +0100
            Re: for-loop mutates to an endless-loop David Brown <david.brown@hesbynett.no> - 2020-01-04 13:09 +0100
  Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 14:28 +0000
    Re: for-loop mutates to an endless-loop jak <nospam@please.ty> - 2020-01-05 00:40 +0100
    Re: for-loop mutates to an endless-loop "Walter H." <Walter.H-Nntp@mathemainzel.info> - 2020-01-06 20:50 +0100
      Re: for-loop mutates to an endless-loop Manfred <noname@add.invalid> - 2020-01-06 21:28 +0100
    Re: for-loop mutates to an endless-loop Manfred <noname@add.invalid> - 2020-01-06 21:02 +0100
    Re: for-loop mutates to an endless-loop Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-11 10:01 -0800
  Re: for-loop mutates to an endless-loop Richard Damon <Richard@Damon-Family.org> - 2020-01-04 16:21 -0500
    Re: for-loop mutates to an endless-loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-04 22:17 +0000

csiph-web