🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #217

Re: Taming The Switch-Statement

#217
From: Waldek Hebisch
Date: Sun, 11 Oct 2026 14:42
77 lines
3593 bytes
bart <bc@freeuk.com> wrote:
> On 10/10/2026 16:50, David Brown wrote:
>> On 10/10/2026 15:11, bart wrote:
> 
> Because unfortunately C's flexibility makes weird stuff possible.
> 
> C's 'switch' is truly bizarre. Below is an experiment where I took an 
> ordinary function body, and wrapped 'switch' around it with randomly 
> placed case labels.
> 
> It still worked (as I placed 'default:' near the start, and providing 
> 'm' is greater than 7), but What The Hell?
> 
> gcc with warnings says nothing useful about it.
> 
<snip>
> The fall-through feature is a minor detail.
> 
> -----------------------
> 
> static void nsieve(int m) {
>     unsigned int count = 0, i, j;
>     boolean * flags = (boolean *) malloc(m * sizeof(boolean));
>     for (i = 0; i < m; ++i) flags[i]=1;
> 
>     for (i = 2; i < m; ++i)
>         if (flags[i]) {
>             ++count;
>             for (j = i << 1; j < m; j += i)
>                    flags[j] = 0;
>     }
> 
>     free(flags);
>     printf("Primes up to %8u %8u\n", m, count);
> }
> 
> static void nsieve_sw(int m) {
>     switch (m) {
>     default:
>     case 1:
>         unsigned int count = 0, i, j;
>     case 2:
>         boolean * flags = (boolean *) malloc(m * sizeof(boolean));
>         for (i = 0; i < m; ++i)
>             case 3:
>                 flags[i]=1;
> 
>         for (i = 2; i < m; ++i)
>             if (flags[i]) {
>             case 4:
>                 ++count;
>             case 5:
>                 for (j = i << 1; j < m; j += i)
>             case 6:
>                        flags[j] = 0;
>         }
> 
>         free(flags);
>             case 7:
>         printf("Primes up to %8u %8u\n", m, count);
>     }
> }
> 

I am disappointed by gcc optimizations (at least by gcc-12).  gcc
should notice that m equal to 3 immediately leads to use of
uninitialized variable, so is undefined behaviour.  Similarly
m equal to 6.  When m equals 4 or m equals 5 then test for
containing loop will lead to use of uninitialized variable.
AFAICS m equal 1 and m equal 2 are useless.  gcc should assume
that there is no undefined behaviour, so the only remaining
case is m = 7.  So gcc could simplify this function quite a lot.
But looking at assembly shows that gcc generated actual switch
with 5 cases (corresponding to m being 3, 4, 5, 6, 7).

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11ag78a$1nqv3$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@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!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Sun, 11 Oct 2026 14:42:20 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11ag78a$1nqv3$1@paganini.bofh.team>
References: <119qf6l$2rmg3$1@dont-email.me> <11ac0mi$19kfd$1@dont-email.me> <11ac2jv$1a84t$1@dont-email.me> <11ac783$1bkuo$1@kst.eternal-september.org> <11ad5eq$1l8hg$1@dont-email.me> <11ad9fg$cjm0$4@dont-email.me> <11addhr$1oakb$1@dont-email.me> <11admri$1s1lb$1@dont-email.me> <11afq4b$2idte$1@dont-email.me>
Injection-Date: Sun, 11 Oct 2026 14:42:20 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="1829859"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2