Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #127166
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Opinion sought: Is this strictly conforming C? |
| Date | 2018-02-28 22:31 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <p7773v$gbe$1@dont-email.me> (permalink) |
| References | (9 earlier) <aa4lC.185862$qv.84846@fx33.iad> <p73452$fje$1@dont-email.me> <6c87947f-bcf1-49c1-a860-051ebd622d62@googlegroups.com> <p75sge$jt3$1@dont-email.me> <cf3e3d61-8a05-41a2-a8eb-14c7853e4604@googlegroups.com> |
On 28/02/18 18:03, supercat@casperkitty.com wrote:
> On Wednesday, February 28, 2018 at 3:24:58 AM UTC-6, David Brown wrote:
>> On 27/02/18 17:48, supercat@casperkitty.com wrote:
>>> 2. The authors of gcc have taken the attitude that any program that relies
>>> upon behaviors that aren't explicitly guaranteed should be considered
>>> "broken", but its documentation fails to actually define all the behaviors
>>> which gcc seems to be intended to provide. For example, gcc's
>>> documentation states that it doesn't presently exploit c99's new
>>> signed-left-shift freedoms for the purpose of optimization, but it does
>>> not actually specify the behavior. Likewise with -fno-strict-aliasing.
>>>
>>
>> The authors of gcc take the attitude that if behaviour is defined in the
>> C standards, then it is defined in their compiler. If it is /not/
>> defined in the C standards (or other target-specific or OS-specific
>> standards), then they will sometimes give you a defined behaviour -
>> perhaps with a flag to control it. This is a /bonus/. Sure, they could
>> have given clearer documentation about some of their flags - but it is
>> better just to write code that doesn't need such non-C behaviour. Or
>> you can use the flags, making reasonable assumptions about how they work
>> (since the gcc developers are reasonable people), and test your code
>> thoroughly.
>
> The documentation for gcc frequently goes out of its way to suggest that
> certain actions would have defined behavior, at least when using certain
> combinations of flags, but fails to actually say what the behavior actually
> is. If the authors took the attitude that they'll avoid breaking any
> constructs which documentation implies that gcc would support even if it
> doesn't give a bullet-proof promise, such a distinction wouldn't matter.
> But if the gcc's authors are going to insist that only things which are
> actually guaranteed can be expected to work, that's less cool.
Why don't you ask the gcc authors? They follow the mailing lists, and
try to be helpful. Of course, it would help if you ask in a concise and
polite manner.
>
>> Anyway, who /cares/ about behaviour of signed integer shifts with
>> negative numbers, and other such cases? Use unsigned data when you need
>> to do bit manipulation.
>
> Anyone who has existing code which might use such things would care.
People who used such code care mainly about it working on the particular
platform and compiler they are using. After all, the great majority of
C code is only ever compiled for a single target, using one compiler -
or at most within a small group of targets. Portable code is a minority
(an important minority, of course).
> My
> bigger point is that this was a construct which was unambiguously defined
> in useful fashion on every common platform.
No, it was never defined in any portable way. Some compilers might have
defined it in a particular way, but certainly nothing you could rely on
for different compilers or different targets.
> The definition was less than
> ideal on some obscure platforms [none of which have ever, so far as I can
> tell, received C99 implementations], but there was no plausible reason why
> the behavior should not have remained the same on common platforms, save
> only for the fact that it was simpler for the Standard to say that the
> behavior is Undefined on all platforms, than to recognize a category of
> platforms for which the Standard would describe the behavior, and a
> category of (as it happens, non-existent) platforms for which it would not.
There you go again - thinking that because /you/ can't think of a reason
for something, it must not be the case, or that the standards committee
thought intended something other than what they wrote. It does not
/matter/ what you think, or what "plausible reasons" you can imagine, or
fail to imagine. It does not /matter/ how you think C used to work, or
how you think some compilers work, or how you think some platforms work.
It does not /matter/ what platforms /you/ think are obscure or
outdated. It certainly does not matter what you think the Standards
committees "intended" or did not intend.
What matters is what the standards (the C standards, together with any
other /relevant/ standards or documents for the platform) /say/. And
when they say something has undefined behaviour, compilers are under no
obligation to give any definition to it. When the standards fail to
give a defined behaviour for something, the behaviour is again
undefined. It does not matter if this lack of definition is because of
some "obscure" platforms, for optimisation purposes, for compiler
simplicity, or an accidental omission in the text. It really is that
simple.
And whether you like it or not, a number of people on the C standards
committees have been compiler users or compiler writers for some
/really/ obscure platforms. The kind of devices with 18-bit chars, or
sizeof(int) == 3, or no stack.
>
>>> struct s1 {int x; };
>>> struct s2 {int x; };
>>> union s1s2 {struct s1 v1; struct s2 v2;};
>>>
>>> int fetch_s1_x(struct s1 *p)
>>> {
>>> // Note: complete definition of union s1s12 is visible here!
>>> return p->x;
>>> }
>>> void store_s2_x(struct s2 *p, int v)
>>> {
>>> p->x=v;
>>> }
>>> int test(union s1s2 *up1, union s1s2 *up2)
>>> {
>>> if (fetch_s1_x(&up1->v1))
>>> store_s2_x(&up2->v2,1);
>>> return fetch_s1_x(&up1->v1);
>>> }
>
>> It might be even better if you simply don't write such daft code.
>
> Most practical code would work "by happenstance" even with gcc. On the
> other hand, a compiler which can't process the above code in accordance
> with what the Standard actually says should not be trusted to reliably
> process practical code either.
Yes, clearly gcc is not a tool to rely on for compiling C. The whole of
the programming world should throw it out immediately.
<snip>
If you think that gcc is wrong, file a bug. Smarter minds than yours
will look at it and compare it to the standards, and see if the compiler
needs to be changed.
>
>>> Isn't one of the main purposes of having a portable language to allow
>>> programmers to write code that others will be able to use on a variety of
>>> implementations *without the original programmer having to know about all
>>> the implementations in questions*?
>>
>> Yes indeed. But you are not talking about writing in C, the
>> standardised portable language. You are talking about writing in weird
>> imaginary dialects of C based on what you think people wrote in books
>> long before the standards were written, and based on what you imagine
>> people to have intended to write (because you believe that the C
>> standards committee had clear plans in their head, but repeatedly wrote
>> something different in every version of the standards, while
>> telepathically transmitting the rules of "true C" to you alone).
>
> What fraction of the tasks that can be performed using C can be performed
> in practical fashion on 100% of the platforms that run C?
>
> What fraction could be done practically on most such platforms, but not
> 100%?
And what use would making up such numbers be?
<snip>
>
>> If you want to use additional definitions beyond those given in standard
>> C, you have to read the documentation. If you want to use extra
>> compiler features to get particular effects not described in the
>> standard language, you need to read the documentation.
>
> How should one read the documentation for compilers that one doesn't know
> about, but with which people one doesn't know about might want to use
> the code?
Read what you want. Then either write C code that works, or write
jumbled nonsense and blame the tools.
>
>> And since gcc is far and away the most common C compiler in use, on the
>> widest range of targets, then something can only be a "relatively common
>> feature" if it is implemented that way in gcc. Like it or not, gcc is
>> the nearest there is to a "de facto" implementation standard for C (and
>> C++, Fortran and Ada).
>
> With optimizations disabled, it is semantically decent but produces rather
> inefficient code. It would be much more useful for many purposes if it
> had options to enable some relatively simple and safe optimizations without
> enabling more aggressive ones. Given that more aggressive optimizations
> require complicated analysis that slows down build times, I'd suggest that
> simpler optimizations would be in pretty much every way better except in
> those parts of the code where the savings in execution time would outweigh
> the increase in build time.
>
And I'd suggest that if you spent a fraction of the time you waste on
your suggestions, assumptions, and nonsensical arguments on learning to
write C correctly, and learning to live with the tools that actually
exist, then you'd get on far better. Alternatively, if you don't like
the way gcc does something, then make the effort to change it.
Meanwhile, other people will successfully (for the most part) write
correct and efficient code using gcc and other compilers.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Opinion sought: Is this strictly conforming C? lee@ansync.com - 2018-02-15 17:20 -0800
Re: Opinion sought: Is this strictly conforming C? James Kuyper <jameskuyper@verizon.net> - 2018-02-15 22:15 -0500
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-16 08:44 -0800
Re: Opinion sought: Is this strictly conforming C? jameskuyper@verizon.net - 2018-02-16 11:47 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-16 13:00 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 12:25 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 13:21 -0800
Re: Opinion sought: Is this strictly conforming C? Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2018-02-19 15:26 -0600
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 13:59 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 14:38 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 15:21 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 15:59 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-19 17:11 -0800
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-20 09:17 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 07:58 -0800
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-20 18:21 +0100
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-20 09:25 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 11:52 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-20 22:27 -0500
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-20 23:07 -0800
Re: Opinion sought: Is this strictly conforming C? Manfred <noname@invalid.add> - 2018-02-21 14:07 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 09:02 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 09:50 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 12:46 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 13:09 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 15:15 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 15:51 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 17:06 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-21 17:46 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:18 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 22:14 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 11:39 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-21 14:59 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-21 22:14 -0500
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:50 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-22 07:22 -0500
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 12:40 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-22 13:00 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 13:48 -0800
Re: Opinion sought: Is this strictly conforming C? scott@slp53.sl.home (Scott Lurndal) - 2018-02-22 22:14 +0000
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:05 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:39 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-22 10:39 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 13:20 -0800
Re: Opinion sought: Is this strictly conforming C? asetofsymbols@gmail.com - 2018-02-23 00:30 -0800
Re: Opinion sought: Is this strictly conforming C? Manfred <noname@invalid.add> - 2018-02-16 20:05 +0100
Re: Opinion sought: Is this strictly conforming C? lee@ansync.com - 2018-02-16 11:14 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 11:30 -0800
Re: Opinion sought: Is this strictly conforming C? Steve Carroll <frelwizzen@gmail.com> - 2018-03-01 09:01 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 11:08 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-19 17:16 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:16 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-22 00:25 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-22 07:41 -0500
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:24 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-25 12:07 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-25 18:34 -0500
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-26 08:21 -0800
Re: Opinion sought: Is this strictly conforming C? Richard Damon <Richard@Damon-Family.org> - 2018-02-26 22:22 -0500
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-27 09:16 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-27 08:48 -0800
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-28 10:24 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-28 09:03 -0800
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-02-28 22:31 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-28 15:19 -0800
Re: Opinion sought: Is this strictly conforming C? David Brown <david.brown@hesbynett.no> - 2018-03-01 12:37 +0100
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-03-01 11:03 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-03-01 11:22 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 11:48 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 15:11 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 16:14 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 19:53 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 21:27 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 00:52 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-02 01:54 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 06:11 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-03-01 12:31 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-03-01 13:15 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-01 14:55 -0800
Re: Opinion sought: Is this strictly conforming C? Tim Rentsch <txr@alumni.caltech.edu> - 2018-03-03 02:19 -0800
Re: Opinion sought: Is this strictly conforming C? Steven Petruzzellis <frelwizzen@gmail.com> - 2018-03-03 03:03 -0800
Re: Opinion sought: Is this strictly conforming C? Keith Thompson <kst-u@mib.org> - 2018-02-25 15:36 -0800
Re: Opinion sought: Is this strictly conforming C? supercat@casperkitty.com - 2018-02-26 10:17 -0800
csiph-web