Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: An experimental max_n for int's... Date: Mon, 07 Sep 2020 08:28:51 -0700 Organization: A noiseless patient Spider Lines: 71 Message-ID: <86363t7gi4.fsf@linuxsc.com> References: <86wo1h8we4.fsf@linuxsc.com> <86sgc48lrd.fsf@linuxsc.com> <1RL2H.1168869$OD.885133@fx22.am4> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="3a0d80ec11b985543b02873bf97ed477"; logging-data="9029"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18m+bUx6xm6XwmAPSBlis66juL642Fi6f4=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:8khuR5FlY3QbQ/7CCYj+ajhLIPI= sha1:M8YarCcW/rn11nmjJi1XmmB7XJ8= Xref: csiph.com comp.lang.c:154674 Bart writes: > On 30/08/2020 11:35, Tim Rentsch wrote: > >> "Chris M. Thomasson" writes: >> >>> On 8/29/2020 5:33 AM, Tim Rentsch wrote: >>> >>>> "Chris M. Thomasson" writes: >>>> >>>>> What do you think of this [int array maximum function] >>>>> [code] >>>>> Is there a better way to write this? >>>> >>>> I would be inclined to write something more like this: >>>> >>>> #include >>>> #include >>>> >>>> static int >>>> maximum( int *v, size_t n, int r ){ >>>> return n == 0 ? r : maximum( v+1, n-1, r < *v ? *v : r ); >>>> } >>>> >>>> int >>>> max_n_int( int *p, size_t n ){ >>>> return maximum( p, n, INT_MIN ); >>>> } >>>> >>>> The INT_MIN is there to handle the degenerate case of arrays >>>> with no elements. >>> >>> I like the idea of using INT_MIN for the degenerate case. However, >>> your maximum is recursive. Can it blow the stack apart if feed with >>> a large enough array? >> >> Oh, excellent question. I'm glad you asked. >> >> Here is the generated code under gcc -O2 (white space editing >> only). Note the lack of any function call instructions: > > > >> I write small recursive functions like this all the time. My >> experience is that compilers are almost always smart enough to >> turn tail-call recursion into loops, like what was done in the >> two compilations shown above. Of course it is possible, if a >> naive compiler is used or if optimization settings are too low, >> that the tail-call optimization won't happen, and truly recursive >> object code will be the result. Usually though that doesn't >> happen. If we are being careful -- and we should be -- the lack >> of actual recursion in the object code should be verified; after >> all, we don't want any nasty surprises. But this isn't hard to >> do. It's a fairly simple matter to write a program in the >> scripting language of your choice (I used awk) that can scan code >> in this format and report cases of recursive calls in the object >> code. Having the tool in hand, making use of tail calls and tail >> recursion can be done with confidence. >> >> I recommend developing an ability to write simple functions like >> this using a recursive, tail-call-optimizable, style. Once you >> get the knack I think you will find it quite empowering. > > This sounds wrong in several ways: > > [objections to using a functional style like the example] You should learn to read more carefully. My suggestion is to develop an ability to write simple functions in a functional style like that of the example. The question of _when_ to write code that way is an entirely separate conversation.