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


Groups > comp.lang.c > #394614

Re: signed vs unsigned and gcc -Wsign-conversion

From Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups comp.lang.c
Subject Re: signed vs unsigned and gcc -Wsign-conversion
Date 2025-10-21 01:45 +0000
Organization A noiseless patient Spider
Message-ID <20251020183259.925@kylheku.com> (permalink)
References <10d5j0v$3kdmk$1@dont-email.me> <87a51lp7lz.fsf@example.invalid> <87wm4pnmcw.fsf@example.invalid>

Show all headers | View raw


On 2025-10-21, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> [...]
>> The thing about unsigned types is that they have a discontinuity at
>> 0, which is much easier to run into than signed int's discontinuties
>> at INT_MIN and INT_MAX.  Subtraction in particular can easily yield
>> mathematically incorrect results for unsigned types (unless your
>> problem domain actuall calls for modular arithmetic).
>
> One specific footgun enabled by unsigned types involves loops that count
> down to zero.  This :
>
>     for (int i = N; i >= 0; i --) {
>         // ...
>     }
>
> is well behaved, but this :
>
>     for (size_t i = N; i >= 0; i --) {
>         // ...
>     }


We just have to translate the signed "i >= 0" into unsigned.

One way is to just directly translate the two's complement semantics
is doing, pretending that the high bit of the value is a sign bit:

  // if the two's-complement-like "sign bit" is zero ...

  (SIZE_MAX & (SIZE_MAX >> 1) & i) == 0

In a downard counting loop, we can just stop when we wrap around
to the highest value, so we get to use most of the range:

  for (size_t i = N; i != SIZE_MAX; --i) // or (size_t) -1

(Note: I like to write --i when it's downward, just as a style; it
comes from stacks:  stack[i++] = push; pop = stack[--i].)

The troublesome case is when N needs to start at SIZE_MAX!

But that troublesome case exists when counting upward also,
signed or unsigned.

Signed:

   // We must break the loop before undefined i++:

   for (int i = 0; i <= INT_MAX; i++)

   // Need a bottom-loop break on SIZE_MAX or else infinite loop:

   for (size_t i = 0; i <= SIZE_MAX; i++)

This is where BartC will chime in with how languages benefit from
built-in idioms for ranged loops that solve these problems under the
hood. It's a valid argument.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

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


Thread

signed vs unsigned and gcc -Wsign-conversion pozz <pozzugno@gmail.com> - 2025-10-20 17:03 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-20 17:38 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Michael S <already5chosen@yahoo.com> - 2025-10-20 19:43 +0300
    Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-20 19:07 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion scott@slp53.sl.home (Scott Lurndal) - 2025-10-20 18:01 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-10-21 04:27 +0200
      Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 09:13 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion BGB <cr88192@gmail.com> - 2025-10-20 17:44 -0500
    Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:36 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-20 23:52 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 16:58 -0700
  Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-20 20:03 +0200
    Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-20 20:09 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion rbowman <bowman@montana.com> - 2025-10-21 01:43 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 12:42 +0200
        Re: signed vs unsigned and gcc -Wsign-conversion James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-10-21 14:44 -0400
          Re: signed vs unsigned and gcc -Wsign-conversion David Brown <david.brown@hesbynett.no> - 2025-10-21 22:56 +0200
  Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 14:48 -0700
    Re: signed vs unsigned and gcc -Wsign-conversion Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-10-20 17:13 -0700
      Re: signed vs unsigned and gcc -Wsign-conversion Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-21 01:45 +0000
        Re: signed vs unsigned and gcc -Wsign-conversion antispam@fricas.org (Waldek Hebisch) - 2025-10-21 03:52 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:35 +0000
    Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-20 23:38 +0000
      Re: signed vs unsigned and gcc -Wsign-conversion Bonita Montero <Bonita.Montero@gmail.com> - 2025-10-21 09:57 +0200
        Re: signed vs unsigned and gcc -Wsign-conversion Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-10-21 19:45 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion antispam@fricas.org (Waldek Hebisch) - 2025-10-21 04:42 +0000
  Re: signed vs unsigned and gcc -Wsign-conversion Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-12-15 00:25 -0800

csiph-web