Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.unix.programmer,comp.lang.c Subject: Re: Odd compiler behaviour? Date: Wed, 02 Mar 2016 10:41:47 -0800 Organization: None to speak of Lines: 38 Message-ID: References: <20160301061135.783@kylheku.com> <87h9gqb0lj.fsf@mantic.terraraq.uk> <878u22aua9.fsf@mantic.terraraq.uk> <87wpplaojy.fsf@mantic.terraraq.uk> <87povdajp6.fsf@mantic.terraraq.uk> <871t7sam40.fsf@mantic.terraraq.uk> <87si0894om.fsf@mantic.terraraq.uk> <87mvqg92q1.fsf@mantic.terraraq.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="27149"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1912Cy82WBBsFZfwClo9x8u" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:t3elieMyyfj8glJY85aBzdXz6v4= sha1:eolE1fhYxblOJhuSsSsTpLjvy24= Xref: csiph.com comp.unix.programmer:8010 comp.lang.c:83013 Richard Kettlewell writes: > Barry Margolin writes: >> I was making a very broad generalization: the C language is designed to >> make it easy for the compiler to translate basic operations >> straightforwardly into simple machine codes. > > Unfortunately it was an inaccurate and misleading generalization. I disagree. I think it's an accurate generalization. As a simple example, given: int n; /* ... */ n ++; a compiler must generate code that increments n if n's value is less than INT_MAX, but has no requirements if n == INT_MAX (the behavior is undefined). A major purpose of leaving the behavior of INT_MAX + 1 undefined is to permit `n ++` to be implemented as a simple increment instruction. A compiler doesn't have to perform any analysis of what value n has before that statement, and it doesn't need to decide what to do in the one case where the behavior is undefined. Of course a compiler *can* do any arbitrarily complicated thing in the undefined case. It could generate code equivalent to: if (n < INT_MAX) n ++; else exit(42); But it doesn't have to. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"