🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #457

Re: Taming The Switch-Statement

#457
From: Scott Lurndal
Date: Fri, 16 Oct 2026 21:46
33 lines
2539 bytes
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.

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
}
Technical Details
Message-ID: <OWwAS.61663$R0u.10250@fx07.iad>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx07.iad.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx07.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Taming The Switch-Statement
Newsgroups: comp.lang.c
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>
Lines: 33
Message-ID: <OWwAS.61663$R0u.10250@fx07.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 16 Oct 2026 21:46:22 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 16 Oct 2026 21:46:22 GMT
X-Received-Bytes: 2305