Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #394643
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: signed vs unsigned and gcc -Wsign-conversion |
| Date | 2025-10-21 22:56 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <10d8s2q$6up0$1@dont-email.me> (permalink) |
| References | <10d5j0v$3kdmk$1@dont-email.me> <10d5thm$3ciil$1@dont-email.me> <20251020125218.835@kylheku.com> <10d7o2d$3riat$1@dont-email.me> <10d8kau$26bqh$3@dont-email.me> |
On 21/10/2025 20:44, James Kuyper wrote: > On 2025-10-21 06:42, David Brown wrote: >> On 20/10/2025 22:09, Kaz Kylheku wrote: >>> On 2025-10-20, David Brown <david.brown@hesbynett.no> wrote: >>>> On 20/10/2025 17:03, pozz wrote: > ...>> However using unsigned types due to them being safe is often poorly >>> considered because if something goes wrong contrary to the programmer's >>> intent, there likely will be undefined behavior somewhere. >> >> Exactly. Unsigned types are not somehow "safer" than signed types, just >> because signed types have UB on overflow. Don't overflow your signed >> types, then you have no UB. And if you overflow your unsigned types >> without that being an intentional and understood part of your code, you >> will at the very least get unexpected behaviour - a bug - and just like >> UB, there are no limits to how bad that can get. > > No, there are limits on unexpected behavior: being unexpected, you might > not know what they are, but it is still the case that the behavior > starts out with having nothing more than an expression with an > unexpected but valid value. That's pretty bad, and your code might make > it worse, for example by promoting the unexpected value into undefined > behavior. However, unless and until it actually does so, the behavior is > somewhat more restricted than UB. The effect of "unexpected behaviour" - something that has well-defined behaviour according to the C standard or the implementation, but was not what the programmer had intended or expected - is clear at the point it happens. Your unsigned arithmetic overflows in a defined and specified manner. But the knock-on effects are, in general, unpredictable - there are no specific limits for how bad things can get. It is not unlikely that you'll end up with "real" UB. In theory, real UB can lead to launching of nasal daemons, while unexpected behaviour, if it does not lead to real UB, cannot launch nasal daemons unless you have nasal daemon launch procedures in your program. In practice, real UB can more often lead to a quick crash and perhaps "nicer" bad behaviour (via OS memory protections and the like), while the unexpected behaviour can continue on, quietly causing future havoc and problems that are harder to find and debug. Either way, I think we can agree that bad things can happen!
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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