Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.c > #387291
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What is your opinion about unsigned int u = -2 ? |
| Date | 2024-08-02 19:40 -0700 |
| Organization | None to speak of |
| Message-ID | <87zfpue3bz.fsf@nosuchdomain.example.com> (permalink) |
| References | (8 earlier) <v8jvln$33atp$1@dont-email.me> <87h6c2fldh.fsf@nosuchdomain.example.com> <v8k21v$33nca$1@dont-email.me> <878qxefjk2.fsf@nosuchdomain.example.com> <v8k4i7$37tvs$1@dont-email.me> |
Thiago Adams <thiago.adams@gmail.com> writes:
[...]
> Here a sample with signed int that has a overflow warning.
>
>
> #include <stdio.h>
>
> int main()
> {
> constexpr int a = 2147483647;
> constexpr int b = 1;
> constexpr int c = a+b;
> }
>
> https://godbolt.org/z/ca31r8EMK
It's reasonable to warn about a+b, since it has undefined behavior.
In fact gcc warns about the expression a+b, since it has undefined
behavior, and issues a fatal error message about its use in a context
requiring a constant expression, since that's a constraint violation.
> I think both cases (overflow and wraparound) should have warnings.
You're free to think that, of course, but wraparound behavior is well
defined and unambiguous. I wouldn't mind an *optional* warning, but
plenty of programmers might deliberately write something like
const unsigned int max = -1;
with the reasonable expectation that it will set max to INT_MAX.
> Comparing with __builtin_add_overflow it also reports wraparound.
>
> #include <stdio.h>
>
> int main()
> {
> unsigned int r;
> if (__builtin_add_overflow(0,-1, &r) != 0)
> {
> printf("fail");
> }
> }
Of course __builtin_add_overflow is a non-standard gcc extension. The
documentation says:
-- Built-in Function: bool __builtin_add_overflow (TYPE1 a, TYPE2 b,
TYPE3 *res)
...
These built-in functions promote the first two operands into
infinite precision signed type and perform addition on those
promoted operands. The result is then cast to the type the third
pointer argument points to and stored there. If the stored result
is equal to the infinite precision result, the built-in functions
return 'false', otherwise they return 'true'. As the addition is
performed in infinite signed precision, these built-in functions
have fully defined behavior for all argument values.
It returns true if the result is equal to what would be computed in
infinite signed precision, so it treats both signed overflow and
unsigned wraparound as "overflow". It looks like a useful function, and
if you use it with an unsigned target, it's because you *want* to detect
wraparound.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-07-31 10:55 -0300
Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-07-31 20:29 +0100
Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 12:33 -0700
Re: What is your opinion about unsigned int u = -2 ? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-11 21:08 +0100
Re: What is your opinion about unsigned int u = -2 ? Richard Damon <richard@damon-family.org> - 2024-08-11 16:45 -0400
Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-11 16:53 -0400
Re: What is your opinion about unsigned int u = -2 ? Vir Campestris <vir.campestris@invalid.invalid> - 2024-08-12 11:47 +0100
Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 23:07 -0700
Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-31 17:17 -0400
Re: What is your opinion about unsigned int u = -2 ? Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-01 06:34 +0000
Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-01 12:02 +0100
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 10:36 -0300
Re: What is your opinion about unsigned int u = -2 ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-08-02 14:33 +0000
Re: What is your opinion about unsigned int u = -2 ? Bart <bc@freeuk.com> - 2024-08-02 15:48 +0100
Re: What is your opinion about unsigned int u = -2 ? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-02 16:17 +0100
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 15:39 -0300
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 15:58 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 11:48 -0700
Re: What is your opinion about unsigned int u = -2 ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-08-02 15:21 -0400
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:03 -0300
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:12 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 18:31 -0700
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:50 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:29 -0700
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 09:36 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:10 -0700
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 22:46 -0300
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 23:04 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 18:25 -0700
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 22:44 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:04 -0700
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-02 23:27 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-02 19:40 -0700
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:43 +0200
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 14:46 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:52 -0700
Re: What is your opinion about unsigned int u = -2 ? dave_thompson_2@comcast.net - 2024-08-25 16:52 -0400
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-25 14:28 -0700
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:34 +0200
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 14:51 -0300
Re: What is your opinion about unsigned int u = -2 ? Thiago Adams <thiago.adams@gmail.com> - 2024-08-03 15:30 -0300
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:46 -0700
Re: What is your opinion about unsigned int u = -2 ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-11 13:57 -0700
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-11 14:53 -0700
Re: What is your opinion about unsigned int u = -2 ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-08-03 16:19 -0700
Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 20:10 +0200
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-03 19:17 +0200
Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-04 20:29 +0200
Re: What is your opinion about unsigned int u = -2 ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-08-07 23:48 +0000
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-08 19:47 +0200
Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 20:08 +0200
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-09 20:19 +0200
Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-09 21:18 +0200
Re: What is your opinion about unsigned int u = -2 ? David Brown <david.brown@hesbynett.no> - 2024-08-09 21:40 +0200
Re: What is your opinion about unsigned int u = -2 ? Richard Damon <richard@damon-family.org> - 2024-08-09 21:16 -0400
Re: What is your opinion about unsigned int u = -2 ? Bonita Montero <Bonita.Montero@gmail.com> - 2024-08-04 18:16 +0200
csiph-web