Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Martin Ward Newsgroups: comp.compilers Subject: Undefined behaviour, was: for or against equality Date: Fri, 7 Jan 2022 14:02:50 +0000 Organization: Compilers Central Lines: 26 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <22-01-028@comp.compilers> References: <17d70d74-1cf1-cc41-6b38-c0b307aeb35a@gkc.org.uk> <22-01-016@comp.compilers> <22-01-018@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="31121"; mail-complaints-to="abuse@iecc.com" Keywords: standards, semantics Posted-Date: 07 Jan 2022 20:25:43 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com In-Reply-To: <22-01-018@comp.compilers> Content-Language: en-GB Xref: csiph.com comp.compilers:2804 On 06/01/2022 08:11, David Brown wrote: > The trick is to memorize the/defined/ behaviours, and stick to them. Isn't the set of defined behaviours bigger than the set of undefined behaviours? How do you know what is defined if you don't know what is undefined? For example, a = b + c is precisely defined in C and C++ for floating point variables, but the result can be "undefined behaviour" for ordinary 32 bit signed integer values. If you want to stick to defined behaviours then you need to add extra code. For example, CERT recommends: if (((si_b > 0) && (si_a > (INT_MAX - si_b))) || ((si_b < 0) && (si_a < (INT_MIN - si_b)))) { /* Handle error */ } else { sum = si_a + si_b; } -- Martin Dr Martin Ward | Email: martin@gkc.org.uk | http://www.gkc.org.uk G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4