Path: csiph.com!news.mixmin.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: "Catch-23: The New C Standard,Sets the World on Fire" by Terence Kelly with Special Guest Borer Yekai Pan Date: Sun, 09 Apr 2023 12:55:24 -0700 Organization: A noiseless patient Spider Lines: 50 Message-ID: <86ile44yqb.fsf@linuxsc.com> References: <87zg7n89zw.fsf@bsb.me.uk> <874jpv84uv.fsf@bsb.me.uk> <20230407042121.909@kylheku.com> <86r0st3zj0.fsf@linuxsc.com> <877cuk3luf.fsf@bsb.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="2b6f87d227a563a0c00d2eacd0f30cf4"; logging-data="1945475"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Ng486pSXg/VwXUOVzHxYMtCtYk0kqdmA=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:ZpHrSdbZA1Nx0N95Fn6kXMiU/po= sha1:rPrvErjEqi27U9TE5u0XjTigWdk= Xref: csiph.com comp.lang.c:169889 Ben Bacarisse writes: > Tim Rentsch writes: > >> Kaz Kylheku <864-117-4973@kylheku.com> writes: >> >> [miscellaneous suggestions taken out] >> >>> On 2023-04-06, John Forkosh wrote: >>> >>>> void *realloc0 ( void *ptr, size_t size ) { >>>> void *reptr = NULL; >>>> if ( size < 1 ) { >>>> if ( ptr != NULL ) free(ptr); } >>>> else reptr = realloc(ptr,size); >>>> return ( reptr ); } >>> >>> Consider: >>> >>> return (size == 0) ? free(ptr), NULL : realloc(ptr, size); >>> >>> which avoids multiple returns, variable assignment, and >>> verbiage. >> >> I agree with the general tone of this suggestion. Unfortunately >> however the particular code suggested might not compile (because >> the macro NULL might be #define'd to be 0, which can result in an >> error). > > I'm not sure what you mean. "Can result in an error" sounds a bit > vague. I know from > >> ... the main point about >> avoiding the potential compilation error ... > > that you mean a compilation error, but that's not a standard term. If NULL is #define'd to be 0, then a diagnostic is required. The problem can be seen by attempting to compile this version: void * realloc0( void *p, size_t size ){ return size == 0 ? free( p ), 0 : realloc( p, size ); } The presence of a required diagnostic means the compilation can fail, without needing any other cause. Of course that doesn't mean a compilation must fail, but it can fail, and will fail if for example -Werror was used. Does that clarify what I was trying to get at?