🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #485

Re: Taming The Switch-Statement

#485
From: David Brown
Date: Sun, 18 Oct 2026 10:38
63 lines
4160 bytes
On 16/10/2026 23:46, Scott Lurndal wrote:
> Lawrence =?iso-8859-13?q?D=FFOliveiro?= <ldo@nz.invalid> writes:
>> On Fri, 16 Oct 2026 15:24:01 -0000 (UTC), Waldek Hebisch wrote:
>>
>>> Concernig 'return', having it in place where all work inside
>>> function is done help me to see flow control and avoids
>>> complications. In case of search functions it means return from the
>>> middle of the loop. IMO possible alternatives like flag variables or
>>> break and retrun after loop are worse.
>>
>> Code tends to do a lot of dynamic resource allocation these days, and
>> that requires cleanup. Doing a direct “return” from the middle of a
>> function body doesn’t allow for that.
>>
>> Unless you use a GCC extension like “defer”, I suppose.

"defer" is not a gcc extension.  You can "defer" macro, using gcc's 
"cleanup" attribute (which /is/ an extension) :

<https://gustedt.gitlabpages.inria.fr/defer/>

As I understand it, "defer" is in the proposal stage and could make it 
into C2y, but it is not yet in the draft standard.  There are test 
implementations in gcc, clang, and a couple of other compilers.

> 
> Better to use the cleanup attribute (or RAII in C++).
> 
> #define RAII_CHM __attribute__((cleanup(auto_free_ptr)))
> #define RAII_FILE __attribute__((cleanup(auto_close_file)))
> 
> int main(void) {
>      // Memory and file handles are bound to this block scope
>      RAII_CHM char *buffer = malloc(100);
>      RAII_FILE FILE *fp = fopen("example.txt", "w");
> 
>      if (!buffer || !fp) {
>          return -1; // Even on early exit, validly opened resources are cleaned up!
>      }
> 
>      fprintf(fp, "Hello, RAII in C!");
>      
>      return 0; // Both buffer and fp are automatically freed/closed here
> }

"defer" has a number of benefits over "cleanup", but "cleanup" has the 
advantage that it is available now on gcc and clang - if portability is 
not an issue.

Still, I would not write code like you show above - in C, people expect 
more explicit code, and you are hiding non-standard C behaviour with 
somewhat obscure names.  (Obscurity is, of course, dependent on how much 
you use the feature in your code.)

I'd rather have :

#define RAII(cleanup_func) __attribute__((cleanup(cleanup_func)))

	RAII(auto_free_ptr) char * buffer = malloc(100);
	RAII(auto_close_file) FILE * fp = fopen("example.txt", "w");


Technical Details
Message-ID: <11b27js$121v2$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <11akm9q$29tpl$1@paganini.bofh.team> <11am99u$ul56$2@dont-email.me> <11atfif$38b7p$1@paganini.bofh.team> <11au5ob$3m5co$2@dont-email.me> <OWwAS.61663$R0u.10250@fx07.iad>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 18 Oct 2026 12:38:51 +0200
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <11b27js$121v2$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11afvqt$2kloc$1@dont-email.me>
 <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me>
 <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me>
 <11agv1t$30q22$2@kst.eternal-september.org>
 <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me>
 <20261012223144.00000f95@yahoo.com> <11akm9q$29tpl$1@paganini.bofh.team>
 <11am99u$ul56$2@dont-email.me> <11atfif$38b7p$1@paganini.bofh.team>
 <11au5ob$3m5co$2@dont-email.me> <OWwAS.61663$R0u.10250@fx07.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Oct 2026 10:38:54 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1116130"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/mRlnaDnOGv9lCuECmnaTFNoJiKCF2Iik="; posting-host="a272f2752d0a4376a695806f42b83d2a"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/R2q+VXjgVn061z8p0zFmXtznOo= sha256:ahUKZMNI/RU7xvkU/nIMnITmUbfxTvr4u2cnjuF6OdQ=
	sha1:lRGsh8lw9+8vj1SAlsOrCPVGFx8= sha256:TtUKtAXlAdNvGo9kq4ffBYr870Ybx6llj6+5tpePLtI=
In-Reply-To: <OWwAS.61663$R0u.10250@fx07.iad>
Content-Language: en-GB