Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #67874

Re: I want to ask a question...

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: I want to ask a question...
Date 2015-08-20 10:02 +0200
Organization A noiseless patient Spider
Message-ID <mr41gc$as3$1@dont-email.me> (permalink)
References (1 earlier) <87pp2y7wl7.fsf@gmail.com> <lnmvy2g291.fsf@kst-u.example.com> <d9ea783a-d784-4e13-8133-3cb8d7b42032@googlegroups.com> <lnmvxm3gwa.fsf@kst-u.example.com> <mr38rc$mbb$1@speranza.aioe.org>

Show all headers | View raw


On 20/08/15 03:00, glen herrmannsfeldt wrote:
> Keith Thompson <kst-u@mib.org> wrote:
>> supercat@casperkitty.com writes:
> 
> (snip)
>>> If a pointer isn't null, doesn't identify a valid object, and isn't a
>>> "one-past" pointer of a valid object, even something as seemingly-benign as
> 
>>>     free(ptr);
>>>     if (condition1) {foo(); ptr=NULL;}
>>>     if (condition2) {bar(); ptr=NULL;}
>>>     if (ptr != NULL) printf("Forgot to clear out pointer!");
> 
>>> may cause some compilers to infer that if condition1 is false, bar()
>>> should be called unconditionally since the Standard would not require
>>> that the compiler evaluate condition2 in such case.  Unfortunately,
>>> some compilers writers seem to think that such "optimizations" are a
>>> good thing.
>  
>> The code has undefined behavior if condition1 and condition2 are both
>> false.
> 
> I completely agree that it is official UB.
> 
>> Since the code is incorrect, it shouldn't matter much how it behaves.
>> But it would be nice if the compiler printed a warning.
> 
> I suppose a warning is fine, but on most processors it isn't
> all that hard to do what the program says. Doing the optimization
> and then not warning about it is mean. Legal, but mean.
> 
> How about a special option that enables this kind of optimization,
> so it can be turned off when one doesn't want it.
> 

While such warnings or optimisation options sound nice, it quickly
becomes extremely difficult to handle correctly.  You could make it work
on simple test examples, but complex code - where you /really/ need such
features, as the UB may be hard to spot - become intractable.

Compilers usually do not "take advantage" of UB in code generation
unless there is a lot to be gained - if it is just as easy to generate
code that works the way the programmer seems to intend, then compilers
will (in my experience - though hopefully the UB is not in /my/ code!)
generate the results the programmer expected.  But it is, I think,
impossible to properly define and delineate how the compiler should
handle such situations.

And it's always great when a compiler can warn of UB issues in the code
- but again, there is no way for the compiler to detect all such cases
as this.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: I want to ask a question... Gareth Owen <gwowen@gmail.com> - 2015-08-07 20:00 +0100
  Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 19:04 +0000
  Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 15:30 -0700
    Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 22:52 +0000
      Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 16:46 -0700
        Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-07 23:29 +0000
          Re: I want to ask a question... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-08-08 01:37 +0100
          Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-07 18:55 -0700
            Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-08 05:51 +0000
              Re: I want to ask a question... Robert Wessel <robertwessel2@yahoo.com> - 2015-08-08 01:44 -0500
    Re: I want to ask a question... Ken Brody <kenbrody@spamcop.net> - 2015-08-10 12:46 -0400
    Re: I want to ask a question... supercat@casperkitty.com - 2015-08-19 16:43 -0700
      Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-19 18:09 -0700
        Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 01:00 +0000
          Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-19 20:25 -0700
            Re: I want to ask a question... supercat@casperkitty.com - 2015-08-20 08:15 -0700
              Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-20 11:38 -0700
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 18:47 +0000
                Re: I want to ask a question... supercat@casperkitty.com - 2015-08-20 14:01 -0700
            Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 16:43 +0000
              Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 13:02 -0400
                Re: I want to ask a question... Tonton Th <tTh@nowhere.invalid> - 2015-08-20 17:40 +0000
                Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 15:27 -0400
                Re: I want to ask a question... David Brown <david.brown@hesbynett.no> - 2015-08-20 22:58 +0200
                Re: I want to ask a question... James Kuyper <jameskuyper@verizon.net> - 2015-08-20 23:24 -0400
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-21 06:19 +0000
                Re: I want to ask a question... Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-08-21 09:20 -0600
                Re: I want to ask a question... supercat@casperkitty.com - 2015-08-22 12:25 -0700
                Re: I want to ask a question... Robert Wessel <robertwessel2@yahoo.com> - 2015-08-23 01:33 -0500
                Re: I want to ask a question... glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-08-20 19:28 +0000
              Re: I want to ask a question... Keith Thompson <kst-u@mib.org> - 2015-08-20 12:11 -0700
          Re: I want to ask a question... David Brown <david.brown@hesbynett.no> - 2015-08-20 10:02 +0200
        Re: I want to ask a question... raltbos@xs4all.nl (Richard Bos) - 2015-08-22 13:42 +0000
          Re: I want to ask a question... supercat@casperkitty.com - 2015-08-22 12:23 -0700

csiph-web