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


Groups > comp.lang.c > #91534

Re: string swap

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: string swap
Date 2016-08-01 14:36 -0700
Organization None to speak of
Message-ID <ln60rk41e7.fsf@kst-u.example.com> (permalink)
References (1 earlier) <nnl4jp$gog$1@dont-email.me> <nnlkgp$1jhc$1@gioia.aioe.org> <2d6cdbc5-d5a7-df68-2cbd-8a427828d2e0@verizon.net> <b5cac7d1-762e-ca1a-dd57-33284faea2de@verizon.net> <cf8aea31-c4ba-4ea3-ac90-bbeab637cbd0@googlegroups.com>

Show all headers | View raw


supercat@casperkitty.com writes:
> On Monday, August 1, 2016 at 2:35:38 PM UTC-5, James R. Kuyper wrote:
>> Let's consider a function that is called through a pointer, which might 
>> point at any one of several different functions. If the return value 
>> tells you something about the function called, any one of those 
>> functions will always return the same value, but it can still make sense 
>> for the caller to look at that value, because the caller doesn't know 
>> which of those functions is the one it called through that pointer.
>
> A nasty gotcha related to that is that: falling through a non-void function
> without returning a value has defined behavior if the caller doesn't do
> anything with the return value, but UB if the caller doesn't "immediately"
> ignore the result.  This can make wrapper design needlessly difficult.

I don't think that's particularly nasty.  It's there to cater to
pre-ANSI code that couldn't use "void".  A function that didn't
return a meaningful value was defined with no explicit return type,
which was equivalent to returning int.  Falling off the end would
return an unspecified result which the caller was expected to ignore.

I don't know what you mean by

    doesn't "immediately" ignore the result.

UB occurs if the caller *uses* the result.

So don't do that.  If a function is defined to return a non-void type,
make sure it always returns a value of that type.  If it returns int,
add `return 0;` at the bottom.  If it returns a pointer, add `return
NULL;` at the bottom.  If you don't, at least some compilers will give
you a warning similar to "control reaches end of non-void function".

(The main function is of course a special case.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
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"

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


Thread

string swap sinbad <sinbad.sinbad@gmail.com> - 2016-07-31 07:45 -0700
  Re: string swap Melzzzzz <mel@zzzzz.com> - 2016-07-31 16:51 +0200
    Re: string swap Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-07-31 12:48 -0400
  Re: string swap James Kuyper <jameskuyper@verizon.net> - 2016-07-31 11:14 -0400
    Re: string swap Real Troll <real.troll@trolls.com> - 2016-07-31 15:43 -0400
      Re: string swap "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-01 12:55 -0400
        Re: string swap "James R. Kuyper" <jameskuyper@verizon.net> - 2016-08-01 15:35 -0400
          Re: string swap supercat@casperkitty.com - 2016-08-01 13:09 -0700
            Re: string swap Keith Thompson <kst-u@mib.org> - 2016-08-01 14:36 -0700
            Re: string swap jameskuyper@verizon.net - 2016-08-01 15:58 -0700
  Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-07-31 15:38 +0000
    Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-31 12:42 -0700
      Re: string swap Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2016-07-31 18:16 -0500
        Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-01 07:33 -0700
      Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-03 13:21 +0000
        Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-05 09:11 -0700
          Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-10 12:56 +0000
            Re: string swap Tim Rentsch <txr@alumni.caltech.edu> - 2016-08-17 09:45 -0700
              Re: string swap raltbos@xs4all.nl (Richard Bos) - 2016-08-21 12:42 +0000
  Re: string swap Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-31 17:16 +0100
  Re: string swap Les Cargill <lcargill99@comcast.com> - 2016-07-31 14:28 -0500

csiph-web