Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: string swap Date: Mon, 01 Aug 2016 14:36:48 -0700 Organization: None to speak of Lines: 41 Message-ID: References: <6461d9d8-9101-47e4-a0a5-7609a32b8ec1@googlegroups.com> <2d6cdbc5-d5a7-df68-2cbd-8a427828d2e0@verizon.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="24930"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19an+68zPD0/2ELtMfMwOvS" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:entSC+FTOmE1r5pe2nvfNABo+jY= sha1:jToO1MSQomRrEYD2TB8ms8wvcEQ= Xref: csiph.com comp.lang.c:91534 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 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"