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


Groups > comp.lang.c > #61837

Re: Return value for strcat and similar functions

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Return value for strcat and similar functions
Date 2015-05-10 21:30 +0200
Organization A noiseless patient Spider
Message-ID <miobiq$f2o$1@dont-email.me> (permalink)
References (1 earlier) <kfnegmti3fe.fsf@x-alumni2.alumni.caltech.edu> <mif7an$hem$1@dont-email.me> <kfny4l0f1wi.fsf@x-alumni2.alumni.caltech.edu> <mihue7$t9s$1@dont-email.me> <kfn4mnkfne5.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


On 10/05/15 17:44, Tim Rentsch wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 08/05/15 00:39, Tim Rentsch wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 07/05/15 03:25, Tim Rentsch wrote:
>>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>>
>>>>>> Does anyone know why the return value for strcat and similar
>>>>>> library functions is defined in such an unhelpful (IMHO) way?
>>>>>>
>>>>>> The return value of strcat(s1, s2) is always s1, which is a
>>>>>> value you already have available.  Returning a pointer to the
>>>>>> end of s1 (i.e., s1 + strlen(s1)) would be much more efficient
>>>>>> when building up a string from parts.
>>>>>
>>>>> You're assuming that the value of the first argument is readily
>>>>> available otherwise, which isn't always true - it could be the
>>>>> result of a function call, for example.
>>>>
>>>> It /is/ readily available -
>>>
>>> Not exactly.  What you mean is not that it is readily available
>>> but that it can be made available, by declaring a variable and
>>> assigning the returned value.
>>
>> That is certainly what /I/ mean be "readily available".  You can
>> get the value by simply attaching a name to it - that's good
>> enough.
>
> I don't want to get into a quibbling match about definitions;
> the key thing is your description is wrong.  You cannot simply
> attach a name to the needed value - sometimes a name must be
> declared before being assigned to.  That is a non-negligible
> cost.

If you think that this cost is non-negligible (assuming a half-decent 
compiler), then I suppose that explains your problem here.  I, on the 
other hand, have no doubts that it is negligible, and have great 
difficulty in imagining a situation where it is significantly easier, 
clearer, faster, or more efficient to use "s1 = strcat(foo(), s2)" 
rather than "strcat(s1 = foo(), s2)", or my own preferred choice of two 
lines "s1 = foo(); strcat(s1, s2);".

I think that means we have approximately the same definition of "readily 
available", but different ideas about the cost of attaching a name to a 
value that the compiler has already calculated and will need later on.

>
>>>> if you as a programmer need it, you have access to it.  If your
>>>> "s1" is the result of calling "foo()", then all you need to write
>>>> is "strcat(s1 = foo(), s2)".
>>>
>>> My point is either choice has its own advantages.  Personally I
>>> think the choice made for strcat() is the right one for what it
>>> is.  If something different or more elaborate is wanted, it's
>>> easy enough to program that.
>>
>> I agree that it's easy to write a replacement for strcat() that
>> returns a pointer to the end terminator (or the length of the
>> string, if preferred).  And of course your personal opinion is up
>> to you.  But I can't see any good reason why the standard library
>> version is specified the way it is, rather than the much more
>> useful alternative of returning a pointer to the end of the
>> string.
>
> What's wrong with the idea that someone thought it valuable to
> make the resulting (entire) string available so it could be
> given, eg, as an argument to another function (or perhaps one
> branch of a ?: operator, or return expression, etc), taking
> advantage of a functional style?  Is it so hard for you to
> imagine that someone else would have different priorities than
> you do?

The point of this thread was to ask people with different experiences 
and styles if they could think of an advantage of the way strcat is 
defined - precisely because while I could not think of one myself, I 
thought that someone else might.  So far no one (including you) has 
given any suggestions other than "it seemed a good idea at the time" or 
"they standardised the function they were using, without thinking about 
other use-cases".  This is, of course, entirely normal - most projects 
of any size have decisions that are odd, inefficient, or unfortunately 
when looked at later with hindsight.  I merely wondered if the early C 
authors had a good reason that I didn't know of, and it seems not to be 
the case.

>
>> (And if you want a version returning the first argument,
>> that's easy to write yourself.)
>
> I don't have to, I can just use strcat().  :)
>

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


Thread

Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-06 10:02 +0200
  Re: Return value for strcat and similar functions Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2015-05-06 10:34 +0200
  Re: Return value for strcat and similar functions Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-05-06 16:51 +0200
  Re: Return value for strcat and similar functions Keith Thompson <kst-u@mib.org> - 2015-05-06 08:58 -0700
    Re: Return value for strcat and similar functions Richard Heathfield <rjh@cpax.org.uk> - 2015-05-06 17:07 +0100
      Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-06 12:01 -0500
    Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 10:19 +0200
      Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-07 13:21 +0000
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 16:24 +0200
          Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-07 18:02 +0000
      Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 10:17 -0500
        Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 10:56 -0500
          Re: Return value for strcat and similar functions asetofsymbols@gmail.com - 2015-05-07 11:00 -0700
            Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 14:28 -0500
              Re: Return value for strcat and similar functions asetofsymbols@gmail.com - 2015-05-07 12:47 -0700
          Re: Return value for strcat and similar functions Nathan Wagner <nw@hydaspes.if.org> - 2015-05-08 09:31 +0000
        Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 15:55 -0700
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-08 11:03 +0200
          Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-08 05:07 -0500
  Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-06 18:25 -0700
    Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 10:22 +0200
      Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 15:39 -0700
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-08 11:09 +0200
          Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-10 08:44 -0700
            Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-10 21:30 +0200
              Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-11 20:42 -0700
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-12 09:52 +0200
                Re: Return value for strcat and similar functions Bartc <bc@freeuk.com> - 2015-05-12 13:33 +0100
                Re: Return value for strcat and similar functions Ike Naar <ike@iceland.freeshell.org> - 2015-05-14 20:15 +0000
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-14 23:06 +0200
                Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-20 11:08 -0700
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-21 10:55 +0200
                Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-21 13:05 +0000
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-21 20:42 +0200
                Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-10 10:40 -0700
    Re: Return value for strcat and similar functions raltbos@xs4all.nl (Richard Bos) - 2015-05-08 21:56 +0000
      Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-10 09:07 -0700
  Re: Return value for strcat and similar functions Bartc <bc@freeuk.com> - 2015-05-07 10:11 +0100

csiph-web