Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Real programs don't eat cache.


devel / comp.lang.c / Effect of CPP tags

SubjectAuthor
* Effect of CPP tagsJanis Papanagnou
+- Re: Effect of CPP tagsLowell Gilbert
+* Re: Effect of CPP tagsKaz Kylheku
|`* Re: Effect of CPP tagsSpiros Bousbouras
| `- Re: Effect of CPP tagsTim Rentsch
+* Re: Effect of CPP tagsJanis Papanagnou
|+* Re: Effect of CPP tagsLowell Gilbert
||+* Re: Effect of CPP tagsKeith Thompson
|||`* Re: Effect of CPP tagsKaz Kylheku
||| `* Re: Effect of CPP tagsKeith Thompson
|||  `* Re: Effect of CPP tagsTim Rentsch
|||   `* Re: Effect of CPP tagsKaz Kylheku
|||    +- Re: Effect of CPP tagsJames Kuyper
|||    +* Re: Effect of CPP tagsJames Kuyper
|||    |`* Re: Effect of CPP tagsKaz Kylheku
|||    | +* Re: Effect of CPP tagsJames Kuyper
|||    | |`- Re: Effect of CPP tagsTim Rentsch
|||    | `* Re: Effect of CPP tagsTim Rentsch
|||    |  `* Re: Effect of CPP tagsKeith Thompson
|||    |   +- Re: Effect of CPP tagsDavid Brown
|||    |   +* Re: Effect of CPP tagsTim Rentsch
|||    |   |`- Re: Effect of CPP tagsKeith Thompson
|||    |   `- Re: Effect of CPP tagsTim Rentsch
|||    `- Re: Effect of CPP tagsTim Rentsch
||+* Re: Effect of CPP tagsKaz Kylheku
|||+- Re: Effect of CPP tagsKaz Kylheku
|||`* Re: Effect of CPP tagsLowell Gilbert
||| `- Re: Effect of CPP tagsJanis Papanagnou
||`* Re: Effect of CPP tagsJanis Papanagnou
|| `- Re: Effect of CPP tagsKaz Kylheku
|+- Re: Effect of CPP tagsKaz Kylheku
|`* Re: Effect of CPP tagsScott Lurndal
| +* Re: Effect of CPP tagsJanis Papanagnou
| |`* Re: Effect of CPP tagsKeith Thompson
| | +* Re: Effect of CPP tagsScott Lurndal
| | |`* Re: Effect of CPP tagsDavid Brown
| | | `* Re: Effect of CPP tagsJames Kuyper
| | |  `- Re: Effect of CPP tagsDavid Brown
| | `- Re: Effect of CPP tagsTim Rentsch
| `- usleep (Was: Effect of CPP tags)Kenny McCormack
+* Re: Effect of CPP tagsLawrence D'Oliveiro
|`* Re: Effect of CPP tagsBart
| +* Re: Effect of CPP tagsDavid Brown
| |`* Re: Effect of CPP tagsKeith Thompson
| | `* Re: Effect of CPP tagsKaz Kylheku
| |  `* Re: Effect of CPP tagsBart
| |   +* Re: Effect of CPP tagsLawrence D'Oliveiro
| |   |`* Re: Effect of CPP tagsBart
| |   | `* Re: Effect of CPP tagsLawrence D'Oliveiro
| |   |  `* Re: Effect of CPP tagsBart
| |   |   +* Re: Effect of CPP tagsScott Lurndal
| |   |   |+* Re: Effect of CPP tagsDavid Brown
| |   |   ||`- Re: Effect of CPP tagsBGB
| |   |   |`* Re: Effect of CPP tagsBart
| |   |   | `- Re: Effect of CPP tagsDavid Brown
| |   |   `- Re: Effect of CPP tagsLawrence D'Oliveiro
| |   `* Re: Effect of CPP tagsDavid Brown
| |    +* Re: Effect of CPP tagsBart
| |    |+- Re: Effect of CPP tagsScott Lurndal
| |    |+* Re: Effect of CPP tagsKaz Kylheku
| |    ||+* Re: Effect of CPP tagsBart
| |    |||`* Re: Effect of CPP tagsBart
| |    ||| +- Re: Effect of CPP tagsKeith Thompson
| |    ||| `* Re: Effect of CPP tagsKaz Kylheku
| |    |||  `* Re: Effect of CPP tagsKeith Thompson
| |    |||   +* Re: Effect of CPP tagsJanis Papanagnou
| |    |||   |`- Re: Effect of CPP tagsKeith Thompson
| |    |||   `- Re: Effect of CPP tagsKaz Kylheku
| |    ||`- Re: Effect of CPP tagsScott Lurndal
| |    |`- Re: Effect of CPP tagsDavid Brown
| |    `* Re: Effect of CPP tagsLawrence D'Oliveiro
| |     +* Re: Effect of CPP tagsChris M. Thomasson
| |     |`* Re: Effect of CPP tagsLawrence D'Oliveiro
| |     | `* Re: Effect of CPP tagsChris M. Thomasson
| |     |  `* Re: Effect of CPP tagsLawrence D'Oliveiro
| |     |   +- Re: Effect of CPP tagsChris M. Thomasson
| |     |   +- Re: Effect of CPP tagsChris M. Thomasson
| |     |   +- Re: Effect of CPP tagsKaz Kylheku
| |     |   `- Re: Effect of CPP tagsBlue-Maned_Hawk
| |     +* Re: Effect of CPP tagsDavid Brown
| |     |+* Re: Effect of CPP tagsBart
| |     ||+* Re: Effect of CPP tagsDavid Brown
| |     |||+- Re: Effect of CPP tagsBlue-Maned_Hawk
| |     |||`* Re: Effect of CPP tagsBart
| |     ||| `* Re: Effect of CPP tagsDavid Brown
| |     |||  `* Re: Effect of CPP tagsBart
| |     |||   +* Re: Effect of CPP tagsChris M. Thomasson
| |     |||   |`- Re: Effect of CPP tagsChris M. Thomasson
| |     |||   +* Re: Effect of CPP tagstTh
| |     |||   |+- Re: Effect of CPP tagsLawrence D'Oliveiro
| |     |||   |+- Re: Effect of CPP tagsKaz Kylheku
| |     |||   |`* Re: Effect of CPP tagsBart
| |     |||   | `* Re: Effect of CPP tagsScott Lurndal
| |     |||   |  `* Re: Effect of CPP tagsBart
| |     |||   |   `* Re: Effect of CPP tagsDavid Brown
| |     |||   |    +* Re: Effect of CPP tagsKaz Kylheku
| |     |||   |    |`* Re: Effect of CPP tagsDavid Brown
| |     |||   |    | `- Re: Effect of CPP tagsKaz Kylheku
| |     |||   |    `* Re: Effect of CPP tagsBart
| |     |||   |     +* Re: Effect of CPP tagsScott Lurndal
| |     |||   |     |`* Re: Effect of CPP tagsBart
| |     |||   |     `* Re: Effect of CPP tagsDavid Brown
| |     |||   `* Re: Effect of CPP tagsDavid Brown
| |     ||`* Re: Effect of CPP tagsBlue-Maned_Hawk
| |     |`* Re: Effect of CPP tagsLawrence D'Oliveiro
| |     `* Re: Effect of CPP tagsKaz Kylheku
| +- Re: Effect of CPP tagsRichard Damon
| +* Re: Effect of CPP tagsKaz Kylheku
| +* Re: Effect of CPP tagsBlue-Maned_Hawk
| `- Re: Effect of CPP tagsLawrence D'Oliveiro
`* Re: Effect of CPP tagsTim Rentsch

Pages:123456789101112131415161718192021222324252627
Re: Effect of CPP tags

<un9ap4$77ft$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30618&group=comp.lang.c#30618

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 16:29:24 +0000
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <un9ap4$77ft$2@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Jan 2024 16:29:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de412a63226b1270d33da06065ff1dc3";
logging-data="237053"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yGyLuQ6LWBbSBXQuyvK4sq3q1XgWEIl4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WJFGUJJ9tTobj9R9uK09CQ/6R2Q=
In-Reply-To: <un92c7$6325$1@dont-email.me>
Content-Language: en-GB
 by: Bart - Fri, 5 Jan 2024 16:29 UTC

On 05/01/2024 14:05, David Brown wrote:
> On 05/01/2024 02:53, Bart wrote:
>> On 04/01/2024 23:25, Lawrence D'Oliveiro wrote:
>>> On Thu, 4 Jan 2024 22:48:17 +0000, Bart wrote:
>>>
>>>> For 40 years 99% of my coding has been in a language where you just
>>>> write:
>>>>
>>>>       A.len
>>>
>>> Better still:
>>>
>>>      len(A)
>>>
>>> which is just a generic wrapper around
>>>
>>>      A.__len__()
>>>
>>> so it will work with your custom object types as well, all they have
>>> to do
>>> is define a method with that name.
>>
>> If a language has a built-in like A.len or len(A) or A'len and so on,
>> then it can choose to allow overloading for that operator. The syntax
>> chosen doesn't matter.
>>
>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>
>
> One option for a useful array length operator/function/macro is a simple
> and limited feature that works for arrays of known size and gives a hard
> (compile-time) error when the size is not known.  The one you have in
> your own language covers most of that, except for the insanity of
> evaluating to 0 when given a pointer/reference to an "unbounded" array.
> The "ARRAY_SIZE" macro in Linux is almost perfect - the only
> disadvantage is that it relies on compiler extensions:
>
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
> __must_be_array(arr))
>
> #define __same_type(a, b) __builtin_types_compatible_p(typeof(a),
> typeof(b)))
>
> #define __must_be_array(a)    BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>
> #define BUILD_BUG_ON_ZERO(e)   (sizeof(struct { int:-!!(e); }))

At least Linux recognises some of the problems of the
sizeof(A)/sizeof(A[10]) idiom.

Which is more than can said for Scott, who can't see any possible issues
at all. Apparently writing B for one of those As is impossible:

"Any programmer will see that they don't match immediately. It is
obviously wrong."

No matter long or short or similar they are.

Re: Effect of CPP tags

<un9b2f$7a5s$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30619&group=comp.lang.c#30619

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 17:34:23 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <un9b2f$7a5s$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <871qavu5z9.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 5 Jan 2024 16:34:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d5f950065253521c024e28ae053c5667";
logging-data="239804"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qf6mM0Iq9F6TtpFsFVaFmIY0WzHZqDn8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:I5TEximWCMmgvAZ1U+S0szj9uXY=
Content-Language: en-GB
In-Reply-To: <871qavu5z9.fsf@nosuchdomain.example.com>
 by: David Brown - Fri, 5 Jan 2024 16:34 UTC

On 05/01/2024 16:58, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> One option for a useful array length operator/function/macro is a
>> simple and limited feature that works for arrays of known size and
>> gives a hard (compile-time) error when the size is not known. The one
>> you have in your own language covers most of that, except for the
>> insanity of evaluating to 0 when given a pointer/reference to an
>> "unbounded" array. The "ARRAY_SIZE" macro in Linux is almost perfect -
>> the only disadvantage is that it relies on compiler extensions:
>>
>> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
>> __must_be_array(arr))
>>
>> #define __same_type(a, b) __builtin_types_compatible_p(typeof(a),
>> typeof(b)))
>>
>> #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>>
>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
> [...]
>
> When you wrote "in Linux", I wondered if you were being imprecise, but
> in fact that code is in the Linux kernel.
>

I am perhaps being /too/ precise - "Linux" is technically the kernel,
rather than the rest of the system (including user C code). But yes, it
is in the headers for use when compiling the kernel, rather than for
user code compiled for Linux systems.

And yes, you can copy them (or variations of them) to your own code.

> That means the macros aren't directly available to normal C code, but
> you can always copy their definitions (*if* you're using a compiler
> that supports the __builtin_types_compatible_p extension).
>

Re: Effect of CPP tags

<20240105103637.607@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30620&group=comp.lang.c#30620

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 18:37:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <20240105103637.607@kylheku.com>
References: <umet9d$3hir9$1@dont-email.me> <umuj4q$28384$1@dont-email.me>
<umun7n$28l7q$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <un72f1$3p72u$1@dont-email.me>
<un73om$3pd33$1@dont-email.me> <un8gkp$3ne6$1@dont-email.me>
Injection-Date: Fri, 5 Jan 2024 18:37:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="624dae6c469856f06c49a782a2b1407e";
logging-data="275916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SNKwNmJs6qL173gpdAWqsjRzVWbtbRNs="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Gyi3VfI4+IP/DcRmEXPv/fy6Yak=
 by: Kaz Kylheku - Fri, 5 Jan 2024 18:37 UTC

On 2024-01-05, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> Why so paranoid defensive? :-)

For consistency with lengthy past history.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Effect of CPP tags

<20240105103833.654@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30621&group=comp.lang.c#30621

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 18:42:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <20240105103833.654@kylheku.com>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <871qavu5z9.fsf@nosuchdomain.example.com>
Injection-Date: Fri, 5 Jan 2024 18:42:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="624dae6c469856f06c49a782a2b1407e";
logging-data="275916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Bs8GTqmsdXswfS7SrLTBjr2UcdCkWbqk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:OlfbbtyuI2hppKOJBiOZpxhyaaw=
 by: Kaz Kylheku - Fri, 5 Jan 2024 18:42 UTC

On 2024-01-05, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> When you wrote "in Linux", I wondered if you were being imprecise, but
> in fact that code is in the Linux kernel.
>
> That means the macros aren't directly available to normal C code, but
> you can always copy their definitions (*if* you're using a compiler
> that supports the __builtin_types_compatible_p extension).

You can always copy their definitions, if you're using a compiler
that doesn't arbitrarily define __GNUC__ without providing the
associated behaviors:

#ifdef __GNU__

// define array_size in the Linux kernel way

#else

#define arrray_size(x) (sizeof (x)/sizeof *(x))

#endif

If you regularly build the code with a compiler that provides GNU
extensions (like as part of your CI), you're covered, even if you're
going to production with something else.

I use C++ this way in C projects; I have some macro features that
provide extra checks under C++. I get the benefit even if I just
compile the code as C++ only once before every release.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Effect of CPP tags

<h6YlN.142867$c3Ea.107377@fx10.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30622&group=comp.lang.c#30622

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx10.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Effect of CPP tags
Newsgroups: comp.lang.c
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me> <un9ap4$77ft$2@dont-email.me>
Lines: 72
Message-ID: <h6YlN.142867$c3Ea.107377@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 05 Jan 2024 18:44:29 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 05 Jan 2024 18:44:29 GMT
X-Received-Bytes: 3566
 by: Scott Lurndal - Fri, 5 Jan 2024 18:44 UTC

Bart <bc@freeuk.cm> writes:
>On 05/01/2024 14:05, David Brown wrote:
>> On 05/01/2024 02:53, Bart wrote:
>>> On 04/01/2024 23:25, Lawrence D'Oliveiro wrote:
>>>> On Thu, 4 Jan 2024 22:48:17 +0000, Bart wrote:
>>>>
>>>>> For 40 years 99% of my coding has been in a language where you just
>>>>> write:
>>>>>
>>>>>       A.len
>>>>
>>>> Better still:
>>>>
>>>>      len(A)
>>>>
>>>> which is just a generic wrapper around
>>>>
>>>>      A.__len__()
>>>>
>>>> so it will work with your custom object types as well, all they have
>>>> to do
>>>> is define a method with that name.
>>>
>>> If a language has a built-in like A.len or len(A) or A'len and so on,
>>> then it can choose to allow overloading for that operator. The syntax
>>> chosen doesn't matter.
>>>
>>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>>
>>
>> One option for a useful array length operator/function/macro is a simple
>> and limited feature that works for arrays of known size and gives a hard
>> (compile-time) error when the size is not known.  The one you have in
>> your own language covers most of that, except for the insanity of
>> evaluating to 0 when given a pointer/reference to an "unbounded" array.
>> The "ARRAY_SIZE" macro in Linux is almost perfect - the only
>> disadvantage is that it relies on compiler extensions:
>>
>> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
>> __must_be_array(arr))
>>
>> #define __same_type(a, b) __builtin_types_compatible_p(typeof(a),
>> typeof(b)))
>>
>> #define __must_be_array(a)    BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>>
>> #define BUILD_BUG_ON_ZERO(e)   (sizeof(struct { int:-!!(e); }))
>
>At least Linux recognises some of the problems of the
>sizeof(A)/sizeof(A[10]) idiom.
>
>Which is more than can said for Scott, who can't see any possible issues
>at all. Apparently writing B for one of those As is impossible:

Even David has claimed to use the sizeof/sizeof[0] idiom.

>
>"Any programmer will see that they don't match immediately. It is
>obviously wrong."
>
>No matter long or short or similar they are.

There are thousands of ways programmers can introduce
bugs far more easily which are significantly harder
to find and fix; and yes, I expect programmers to
fully understand the code they're writing and test
their code to find bugs which they then will subsequently
fix.

Do lookup the word 'strawman' as it relates to discourse.

The use of that idiom is _not a problem_.

Re: Effect of CPP tags

<un9l2e$8qt5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30623&group=comp.lang.c#30623

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 19:25:02 +0000
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <un9l2e$8qt5$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <umuj4q$28384$1@dont-email.me>
<umun7n$28l7q$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <un72f1$3p72u$1@dont-email.me>
<un73om$3pd33$1@dont-email.me> <un8gkp$3ne6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 5 Jan 2024 19:25:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de412a63226b1270d33da06065ff1dc3";
logging-data="289701"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uTiExdGUAaTz3J4oXi8QgKByl8S5Xq3M="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6cUDXgO6HPlXOF6ea3DHsOJyH2w=
Content-Language: en-GB
In-Reply-To: <un8gkp$3ne6$1@dont-email.me>
 by: Bart - Fri, 5 Jan 2024 19:25 UTC

On 05/01/2024 09:03, Janis Papanagnou wrote:
> On 04.01.2024 21:17, Bart wrote:

> Specifically when discussing C there's IMO countless examples of what's
> missing, and what's wrongly designed, and whatnot. - Thus I am merely
> astonished that you fastened on that bit so vigorously, and also with
> so much text and repetitions.

Within one post or multiple? If I'm repeating myself it is either
because others do, or I'm being attacked on multiple fronts.

> There's (IMO) so much wrong with arrays in C that the discussed special
> case (which is also not generally applicable as pointed out repeatedly)
> is of little interest (for me; YMMV).

There is a huge amount wrong with C at this level of language, which is
about par with my own.

Many will just move on from C to modern alternatives, but they are
generally bigger, more complex, and cumbersome.

I like systems coding at this level. So as a long time user and
developer of my alternate language, I can give talk about 100-200
different aspects that I have made better. Most are microfeatures, but
some are big ones.

> Mind, I started with K&R C in the 1980's, at a time where much more
> sophisticated languages had already been present; the formally much
> more appealing Algol(68), the concepts-outstanding Simula(67), even
> Pascal

Mine has a syntax based largely on Algol and Pascal, notably Algol68.

Algol68 has LWB a and UPB a, I turned those into a.lwb and a.upb, and
added a.len. But my language was far simpler semantically.

> Above we identified a "resume" of the previous posts. Another one
> shall finalize this post; no one is forced to use C.

Neither was I, until 1992, then I needed to start interacting with
libraries defined via a C API. I was supposed to switch to using C (and
get a proper job using that), but I couldn't do it.

I realised my 'temporary' in-house language that I'd hacked together in
my spare time was actually better.

Re: Effect of CPP tags

<un9lir$8qt5$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30624&group=comp.lang.c#30624

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 19:33:48 +0000
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <un9lir$8qt5$2@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Jan 2024 19:33:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de412a63226b1270d33da06065ff1dc3";
logging-data="289701"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ufXRXnFxClY9kJ/CMH6TX21C/JD6iPyg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wyua3rbUkQVrOu80tveqNk9p0Cg=
Content-Language: en-GB
In-Reply-To: <h6YlN.142867$c3Ea.107377@fx10.iad>
 by: Bart - Fri, 5 Jan 2024 19:33 UTC

On 05/01/2024 18:44, Scott Lurndal wrote:
> Bart <bc@freeuk.cm> writes:
>> On 05/01/2024 14:05, David Brown wrote:

>>> One option for a useful array length operator/function/macro is a simple
>>> and limited feature that works for arrays of known size and gives a hard
>>> (compile-time) error when the size is not known.  The one you have in
>>> your own language covers most of that, except for the insanity of
>>> evaluating to 0 when given a pointer/reference to an "unbounded" array.
>>> The "ARRAY_SIZE" macro in Linux is almost perfect - the only
>>> disadvantage is that it relies on compiler extensions:
>>>
>>> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
>>> __must_be_array(arr))
>>>
>>> #define __same_type(a, b) __builtin_types_compatible_p(typeof(a),
>>> typeof(b)))
>>>
>>> #define __must_be_array(a)    BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>>>
>>> #define BUILD_BUG_ON_ZERO(e)   (sizeof(struct { int:-!!(e); }))
>>
>> At least Linux recognises some of the problems of the
>> sizeof(A)/sizeof(A[10]) idiom.
>>
>> Which is more than can said for Scott, who can't see any possible issues
>> at all. Apparently writing B for one of those As is impossible:
>
> Even David has claimed to use the sizeof/sizeof[0] idiom.
>
>>
>> "Any programmer will see that they don't match immediately. It is
>> obviously wrong."
>>
>> No matter long or short or similar they are.
>
> There are thousands of ways programmers can introduce
> bugs far more easily which are significantly harder
> to find and fix; and yes, I expect programmers to
> fully understand the code they're writing and test
> their code to find bugs which they then will subsequently
> fix.
>
> Do lookup the word 'strawman' as it relates to discourse.

And yet, despite it having no problems according to you, people still
feel the need to create macros like ARRAY_SIZE.

So I'm puzzled. Why do they do that? What imaginary problem (according
to you) do they solve?

Re: Effect of CPP tags

<6jZlN.143006$c3Ea.41958@fx10.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30625&group=comp.lang.c#30625

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx10.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Effect of CPP tags
Newsgroups: comp.lang.c
References: <umet9d$3hir9$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me> <un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad> <un9lir$8qt5$2@dont-email.me>
Lines: 17
Message-ID: <6jZlN.143006$c3Ea.41958@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 05 Jan 2024 20:06:26 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 05 Jan 2024 20:06:26 GMT
X-Received-Bytes: 1536
 by: Scott Lurndal - Fri, 5 Jan 2024 20:06 UTC

Bart <bc@freeuk.cm> writes:
>On 05/01/2024 18:44, Scott Lurndal wrote:

>> Do lookup the word 'strawman' as it relates to discourse.
>
>And yet, despite it having no problems according to you, people still
>feel the need to create macros like ARRAY_SIZE.
>
>So I'm puzzled. Why do they do that? What imaginary problem (according
>to you) do they solve?

Why does anyone do anything?

It's possible they believe it is fewer characters to
to write. Some may find it more readable. Some may
find it less readable.

Re: Effect of CPP tags

<un9va1$a752$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30626&group=comp.lang.c#30626

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 22:19:46 +0000
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <un9va1$a752$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 5 Jan 2024 22:19:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de412a63226b1270d33da06065ff1dc3";
logging-data="335010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lZ55ImcG7O031kTIx3aA22P49XfNt5Zs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:DhV2RlFRxUG6FpGyyXxGzVHJwUQ=
Content-Language: en-GB
In-Reply-To: <un92c7$6325$1@dont-email.me>
 by: Bart - Fri, 5 Jan 2024 22:19 UTC

On 05/01/2024 14:05, David Brown wrote:
> On 05/01/2024 02:53, Bart wrote:

>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>
>
> One option for a useful array length operator/function/macro is a simple
> and limited feature that works for arrays of known size and gives a hard
> (compile-time) error when the size is not known.  The one you have in
> your own language covers most of that, except for the insanity of
> evaluating to 0 when given a pointer/reference to an "unbounded" array.

It gives zero because that is actually the compile-time type of the
array. But it is low priority because it is never used that way.

I mentioned elsewhere another mechanism (slices) for dealing better with
arrays passed to functions.

C as it is now has worse problems than this with array handling.

> The "ARRAY_SIZE" macro in Linux is almost perfect - the only
> disadvantage is that it relies on compiler extensions:
>
> #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) +
> __must_be_array(arr))
>
> #define __same_type(a, b) __builtin_types_compatible_p(typeof(a),
> typeof(b)))
>
> #define __must_be_array(a)    BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
>
> #define BUILD_BUG_ON_ZERO(e)   (sizeof(struct { int:-!!(e); }))
>
>
> The other option is a general mechanism for "length" as an
> operator/function/macro that can be applied to a wide range of types -
> including user-made types, assuming the language supports user-defined
> types.  Whether that is done by function overloads, custom methods,
> etc., is a design choice.
>

In my static language, '.len' works on two types (arrays and slices; for
the latter it happens at runtime).

In the dynamic one, '.len' works on 10 different types (list, array,
string, bit-array, bit-set, decimal, record, struct, range). That's
already enough to be dealing with. (Record types can have methods to
return such info.)

In C, sizeof() is usually compile-time, except when used on VLAs, and
here, as usual for VLAs, it is a lot more complicated than you'd think:

--------
#include <stdio.h>
#include <stdlib.h>

int main(void) {
int n=rand()+1;
typedef char (**T)[n];
n=-777;

T x;

printf("%zu\n", sizeof(**x));
} --------

This actually displays 42 (really!). 'x' is a pointer (to a pointer to
array). There is no actual array allocated. But it needs somewhere to
store the size of the type which is the target of those pointers.

I was hoping to do it without creating `x`, but I didn't know how. (I
couldn't do that in mine either; I'd need a way to get the target of a
pointer type T.)

Slices are child's play by comparison; they are basically:

struct {T* ptr; u64 length;};

-------
c:\c>gcc c.c && a
42

Re: Effect of CPP tags

<20240105144033.460@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30627&group=comp.lang.c#30627

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 5 Jan 2024 22:43:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20240105144033.460@kylheku.com>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
Injection-Date: Fri, 5 Jan 2024 22:43:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="624dae6c469856f06c49a782a2b1407e";
logging-data="338841"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ElCpDkJtGW6BB79nQit++i0Z4g39VoWI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Yc8rkMIABe5WizrOupkua3DH4sY=
 by: Kaz Kylheku - Fri, 5 Jan 2024 22:43 UTC

On 2024-01-05, Bart <bc@freeuk.cm> wrote:
> In my static language, '.len' works on two types (arrays and slices; for
> the latter it happens at runtime).

Yeah, but some widely-used languages also solve this.

Broadly speaking beyond the context of this array issue, those are your
competition, not C.

It's like you're dwelling in a room full of retards in order to feel
smart.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Effect of CPP tags

<87wmsns8by.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30628&group=comp.lang.c#30628

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 05 Jan 2024 14:50:57 -0800
Organization: None to speak of
Lines: 44
Message-ID: <87wmsns8by.fsf@nosuchdomain.example.com>
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad>
<un9lir$8qt5$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4d95b7a32aae07de52f424985a97135f";
logging-data="342273"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0xqDbwYma74NOBy5fZ1iq"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:prErqy2VyIE12xk88Qjw5VcckJ8=
sha1:v2lByL2DcLsqXECuLUuOuZ/feGY=
 by: Keith Thompson - Fri, 5 Jan 2024 22:50 UTC

Bart <bc@freeuk.cm> writes:
> On 05/01/2024 18:44, Scott Lurndal wrote:
[...]
>> There are thousands of ways programmers can introduce
>> bugs far more easily which are significantly harder
>> to find and fix; and yes, I expect programmers to
>> fully understand the code they're writing and test
>> their code to find bugs which they then will subsequently
>> fix.
>> Do lookup the word 'strawman' as it relates to discourse.
>
> And yet, despite it having no problems according to you, people still
> feel the need to create macros like ARRAY_SIZE.

Yes. That's how they use the idiom.

> So I'm puzzled. Why do they do that? What imaginary problem (according
> to you) do they solve?

I don't know what you're puzzled about.

Some programmers use the `sizeof arr / sizeof arr[0]` idiom by writing
it directly into an expression, presumably because they find it clear
enough. Others use it by defining a macro, probably with the intent of
making the code clearer to human readers. Others use an existing macro
if it happens to be defined in the project they're using.

Like anything, it's possible to use it incorrectly. I recognize that it
can quietly fail if, for example, you mistype one of the occurrences of
"arr", or if you apply it to a pointer expression. I don't believe I've
ever seen such an error in practice. I suspect that 99% of C
programmers who know how the idiom works also know enough about C arrays
and pointers that they can avoid such errors.

In a previous job, I modified some existing code to use the
sizeof/sizeof idiom. Since I actually knew what I was doing, I didn't
introduce any errors (and IIRC I solved an existing off-by-one error).
What do you think I should have done, waited until it was added as an
extension by the compiler I was using, or as a language feature in C3X?

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Effect of CPP tags

<87cyufxu1s.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30629&group=comp.lang.c#30629

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 05 Jan 2024 23:02:55 +0000
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <87cyufxu1s.fsf@bsb.me.uk>
References: <umet9d$3hir9$1@dont-email.me> <un1clg$2oq9u$1@dont-email.me>
<un1hek$2ph62$1@dont-email.me> <un1rdk$2quec$1@dont-email.me>
<un25vp$1qvm$1@news.gegeweb.eu> <un3hvm$36jtf$1@dont-email.me>
<Z5flN.88873$vFZa.33923@fx13.iad> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9va1$a752$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4227204443ea682b4943618c400113b4";
logging-data="345142"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T+F/IMoGNue3P+Epzz898tvobVyjE7yU="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:kFYGZ7+eVxkgh+fryIoxmXhQKV8=
sha1:Xg+v+as/+tmSIBQCZ0SRlADT5Ck=
X-BSB-Auth: 1.4ae6228a7963df110da4.20240105230255GMT.87cyufxu1s.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 5 Jan 2024 23:02 UTC

Bart <bc@freeuk.cm> writes:

> In C, sizeof() is usually compile-time, except when used on VLAs, and here,
> as usual for VLAs, it is a lot more complicated than you'd think:
>
> --------
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void) {
> int n=rand()+1;
> typedef char (**T)[n];
> n=-777;
>
> T x;
>
> printf("%zu\n", sizeof(**x));
> }
> --------
>
> This actually displays 42 (really!). 'x' is a pointer (to a pointer to
> array). There is no actual array allocated.

More to the point, x is uninitialised and thus evaluating sizeof **x is
undefined behaviour. gcc has some flags that could have helped you to
find that out.

> But it needs somewhere to store
> the size of the type which is the target of those pointers.
>
> I was hoping to do it without creating `x`, but I didn't know how.

You don't need to create an array, nor a pointer to one, nor a pointer
to a pointer to one, in order to show that the compiler must record the
size of a variably modified array type at runtime:

int n = rand() + 1;
typedef char T[n];
printf("%zu\n", sizeof (T));
n = 42;
printf("%zu\n", sizeof (T));

n is something of a red herring here. If you write the expression in
the typedef itself you might have been less surprised:

typedef char T[rand() + 1]; // the size obviously needs to be stored

--
Ben.

Re: Effect of CPP tags

<una97h$baq1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30630&group=comp.lang.c#30630

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 01:09:05 +0000
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <una97h$baq1$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad>
<un9lir$8qt5$2@dont-email.me> <87wmsns8by.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 01:09:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="371521"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oELCSsh9mC+rYHyNn9Uts4qN+6RBPB20="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Ezt39HpldCS5XpC8AUMsqW0jilo=
In-Reply-To: <87wmsns8by.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: Bart - Sat, 6 Jan 2024 01:09 UTC

On 05/01/2024 22:50, Keith Thompson wrote:
> Bart <bc@freeuk.cm> writes:
>> On 05/01/2024 18:44, Scott Lurndal wrote:
> [...]
>>> There are thousands of ways programmers can introduce
>>> bugs far more easily which are significantly harder
>>> to find and fix; and yes, I expect programmers to
>>> fully understand the code they're writing and test
>>> their code to find bugs which they then will subsequently
>>> fix.
>>> Do lookup the word 'strawman' as it relates to discourse.
>>
>> And yet, despite it having no problems according to you, people still
>> feel the need to create macros like ARRAY_SIZE.
>
> Yes. That's how they use the idiom.
>
>> So I'm puzzled. Why do they do that? What imaginary problem (according
>> to you) do they solve?
>
> I don't know what you're puzzled about.

I'm puzzled because people like Scott are suggesting those macros are a
waste of time, and yet you find them in big, important software.

Actually, I'm not really puzzled, I know he's just being contrary to
wind me up, and I was trying to catch him out.

Re: Effect of CPP tags

<unabbr$bhha$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30631&group=comp.lang.c#30631

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 01:45:31 +0000
Organization: A noiseless patient Spider
Lines: 93
Message-ID: <unabbr$bhha$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un1clg$2oq9u$1@dont-email.me>
<un1hek$2ph62$1@dont-email.me> <un1rdk$2quec$1@dont-email.me>
<un25vp$1qvm$1@news.gegeweb.eu> <un3hvm$36jtf$1@dont-email.me>
<Z5flN.88873$vFZa.33923@fx13.iad> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9va1$a752$1@dont-email.me> <87cyufxu1s.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 01:45:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="378410"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lvab68DjJ+90zI1xpc/GOOa8HmdhwBcA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hJt9z58bzBjq6Nj1ZIaXF7Tqm3A=
Content-Language: en-GB
In-Reply-To: <87cyufxu1s.fsf@bsb.me.uk>
 by: Bart - Sat, 6 Jan 2024 01:45 UTC

On 05/01/2024 23:02, Ben Bacarisse wrote:
> Bart <bc@freeuk.cm> writes:
>
>> In C, sizeof() is usually compile-time, except when used on VLAs,
and here,
>> as usual for VLAs, it is a lot more complicated than you'd think:
>>
>> --------
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> int main(void) {
>> int n=rand()+1;
>> typedef char (**T)[n];
>> n=-777;
>>
>> T x;
>>
>> printf("%zu\n", sizeof(**x));
>> }
>> --------
>>
>> This actually displays 42 (really!). 'x' is a pointer (to a pointer to
>> array). There is no actual array allocated.
>
> More to the point, x is uninitialised and thus evaluating sizeof **x is
> undefined behaviour. gcc has some flags that could have helped you to
> find that out.

I actually don't know if it's evaluating it or not. Why on earth should
it? If I change the program to print the original 'n', it is 42 too. So
it doesn't need to evaluate **x to get that dimension.

(When I implement sizeof(X), it works out the /type/ of X, at
compile-time. That should be the case with VLA types too, except the
type will involve that runtime element which is nothing to do with the
value of X or **x.)

>> But it needs somewhere to store
>> the size of the type which is the target of those pointers.
>>
>> I was hoping to do it without creating `x`, but I didn't know how.
>
> You don't need to create an array, nor a pointer to one, nor a pointer
> to a pointer to one, in order to show that the compiler must record the
> size of a variably modified array type at runtime:
>
> int n = rand() + 1;
> typedef char T[n];
> printf("%zu\n", sizeof (T));
> n = 42;
> printf("%zu\n", sizeof (T));
>
> n is something of a red herring here. If you write the expression in
> the typedef itself you might have been less surprised:
>
> typedef char T[rand() + 1]; // the size obviously needs to be stored
>

Yes, this why I set n to -777 to show that it has to capture the value
of n inside the typedef.

But in your example, the size of T /is/ the array dimension. In mine, T
is a pointer; how to double check what the value of n was:

int n=rand()+1;
typedef char (**T)[n];
n=-777;

The original value of n is lost. So how to print the dimension of the
array portion of that type, without creating an instance of the type?

The point of the example was to so show that mixing sizeof and VLAs can
get hairy.

Both gcc and tcc (and clang) seemed to work with my example.

lccwin32 crashed. When I hardcoded n to 4 or 5 (but still a VLA), then
the array size was reported as either 4 or 5, except by lccwin32 which
always said 8.

Initialising x to an actual pointer to pointer to array made no difference.

This compiler might be buggy, but it shows that VLAs can be difficult to
get right. My compiler doesn't even try.

I think my original point might have been that 'slices' are a modest
advance over plain arrays, which have to carry a length parameter. very
easy to understand and not hard to implement.

But C decided to skip such a feaure, and leap straight to the much
harder-to-implement VLAs.

Re: Effect of CPP tags

<87sf3brzs5.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30632&group=comp.lang.c#30632

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 05 Jan 2024 17:55:38 -0800
Organization: None to speak of
Lines: 55
Message-ID: <87sf3brzs5.fsf@nosuchdomain.example.com>
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad>
<un9lir$8qt5$2@dont-email.me>
<87wmsns8by.fsf@nosuchdomain.example.com>
<una97h$baq1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="fcace9a1b4289b2c091a644bf7a20934";
logging-data="380160"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198nygwRC0idr2e/vp9LeQA"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:nH6G31UuNV9qwuVPt+F+3O4WOb8=
sha1:0UkhIAELKCY7ktHBi9fGCLMkBC8=
 by: Keith Thompson - Sat, 6 Jan 2024 01:55 UTC

Bart <bc@freeuk.cm> writes:
> On 05/01/2024 22:50, Keith Thompson wrote:
>> Bart <bc@freeuk.cm> writes:
>>> On 05/01/2024 18:44, Scott Lurndal wrote:
>> [...]
>>>> There are thousands of ways programmers can introduce
>>>> bugs far more easily which are significantly harder
>>>> to find and fix; and yes, I expect programmers to
>>>> fully understand the code they're writing and test
>>>> their code to find bugs which they then will subsequently
>>>> fix.
>>>> Do lookup the word 'strawman' as it relates to discourse.
>>>
>>> And yet, despite it having no problems according to you, people still
>>> feel the need to create macros like ARRAY_SIZE.
>> Yes. That's how they use the idiom.
>>
>>> So I'm puzzled. Why do they do that? What imaginary problem (according
>>> to you) do they solve?
>> I don't know what you're puzzled about.
>
> I'm puzzled because people like Scott are suggesting those macros are
> a waste of time, and yet you find them in big, important software.

Oh, so *that's* what you're complaining about.

I just took a look at Scott's article to which you replied, and its
parent, and so on all the way to the first article in this thread.
Nowhere did Scott suggest that those macros are a waste of time.

He did write some things that suggest that he considers the
`sizeof arr / sizeof arr[0]` idiom clear enough to be written directly
as part of an expression rather than as a macro (and I agree), but I
didn't see him objecting to the idea of using a macro.

I can't speak for him, but I think he was objecting to your insistence
that using the idiom directly is unacceptable, and you interpreted that
as him insisting that wrapping the idiom in a macro is useless. He
didn't say that.

I'm making an assumption here, that you find code that uses the idiom
directly more objectionable than code that wraps it in a macro.

Personally, I'd be likely to define a macro if I'm going to be using it
multiple times. In an example I mentioned recently, where I modified
some existing code to use `sizeof array / sizeof array[0]`, I was only
doing it once, so just wrote the expression directly. Either method is
fine with me, and I suspect Scott would agree.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Effect of CPP tags

<unaces$bku4$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30633&group=comp.lang.c#30633

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 02:04:11 +0000
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <unaces$bku4$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
<20240105144033.460@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 02:04:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="381892"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qY9q+806GqGm/r0tu1uKM5WtRq+Dge8I="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:981s1zcCSb9+nk2iPPpVG3OBcww=
In-Reply-To: <20240105144033.460@kylheku.com>
Content-Language: en-GB
 by: Bart - Sat, 6 Jan 2024 02:04 UTC

On 05/01/2024 22:43, Kaz Kylheku wrote:
> On 2024-01-05, Bart <bc@freeuk.cm> wrote:
>> In my static language, '.len' works on two types (arrays and slices; for
>> the latter it happens at runtime).
>
> Yeah, but some widely-used languages also solve this.
>
> Broadly speaking beyond the context of this array issue, those are your
> competition, not C.

What, languages like Rust, Zig, Java, C#?

I don't think so. Even lesser known ones like C3 are pretty huge. The
nearest in scale to what I'm doing might be Hare.

C can be implemented [on and for x64] in under 200KB. Mine in something
over 300KB (but it is multipass; tcc is single pass). Most of those
others are 100MB and up.

I deliberately keep my systems language simple and low level, yet there
are lots of modest enhancements over C too.

> It's like you're dwelling in a room full of retards in order to feel
> smart.

I would have let my language die years ago. And yet, C is still
tremendously popular and widely used. It's something that people feel
they can master and be in control of, rather than be intimidated by.

But it's also full of terrible design choices.

So if there's a place for such a systems language, then mine is worth
maintaining too. The 'competitor' then is C.

Re: Effect of CPP tags

<87o7dzryrf.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30634&group=comp.lang.c#30634

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Fri, 05 Jan 2024 18:17:40 -0800
Organization: None to speak of
Lines: 83
Message-ID: <87o7dzryrf.fsf@nosuchdomain.example.com>
References: <umet9d$3hir9$1@dont-email.me> <un1rdk$2quec$1@dont-email.me>
<un25vp$1qvm$1@news.gegeweb.eu> <un3hvm$36jtf$1@dont-email.me>
<Z5flN.88873$vFZa.33923@fx13.iad> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9va1$a752$1@dont-email.me> <87cyufxu1s.fsf@bsb.me.uk>
<unabbr$bhha$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="dd6f382ccf7ae1061731f5cc993e4880";
logging-data="384389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mW6UF0D5sc9gGPQ1qMHX/"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:k2V/kFvRkzlv8hqnhTOS8eKDui0=
sha1:S2PGuLDdjOQU8iyGOGKh4Pg4TOo=
 by: Keith Thompson - Sat, 6 Jan 2024 02:17 UTC

Bart <bc@freeuk.cm> writes:
> On 05/01/2024 23:02, Ben Bacarisse wrote:
>> Bart <bc@freeuk.cm> writes:
>>> In C, sizeof() is usually compile-time, except when used on VLAs,
> and here,
>>> as usual for VLAs, it is a lot more complicated than you'd think:
>>>
>>> --------
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> int main(void) {
>>> int n=rand()+1;
>>> typedef char (**T)[n];
>>> n=-777;
>>>
>>> T x;
>>>
>>> printf("%zu\n", sizeof(**x));
>>> }
>>> --------
>>>
>>> This actually displays 42 (really!). 'x' is a pointer (to a pointer to
>>> array). There is no actual array allocated.
>>
>> More to the point, x is uninitialised and thus evaluating sizeof **x is
>> undefined behaviour. gcc has some flags that could have helped you to
>> find that out.
>
> I actually don't know if it's evaluating it or not. Why on earth
> should it? If I change the program to print the original 'n', it is 42
> too. So it doesn't need to evaluate **x to get that dimension.

The C standard currently says:

The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type. The size
is determined from the type of the operand. The result is an
integer. If the type of the operand is a variable length array type,
the operand is evaluated; otherwise, the operand is not evaluated
and the result is an integer constant.

The idea that the operand is "evaluated" is problematic. For example,
if the argument is a parenthesized type name, it's not clear what it
means to "evaluate" it. And there are cases (mostly contrived) where
the rule results in side effects that aren't actually necessary to
determine the size.

If I write:

int n = rand() % 10 + 1;
int vla[n];
printf("%zu\n", sizeof vla);

then the standard says that the expression `vla` is "evaluated". Since
it's of array type, that would logically involve retrieving the values
of each of its elements (there's no array-to-pointer conversion in this
context), which would have undefined behavior because they're
uninitialized. It's fairly clear that wasn't the intent; the only thing
that needs to be evaluated is the implicit metadata that gives the size
of the array.

Jens Gustedt has written a proposal to address this, probably in C26.
See <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3187.htm>.

In practice, applying sizeof to a VLA type, or to a named object of VLA
type, doesn't create any conceptual problems, and those are by far the
most common cases.

When a variable length array type is created, its length is determined
when execution reaches the definition, and the compiler generates code
to implicitly store that information somewhere for later use. If the
length expression happens to be the name of an object, any later changes
to the value of the object do not affect the VLA type or its size or
length; I suggest you stop being surprised by that fact. Applying sizeof
to a VLA type or object retrieves the stored size.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Effect of CPP tags

<unal66$uc4$1@news.gegeweb.eu>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30635&group=comp.lang.c#30635

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!news.gegeweb.eu!gegeweb.org!.POSTED.2a01:cb19:8674:1100:fc25:8d52:9611:1e52!not-for-mail
From: tth...@none.invalid (tTh)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 05:33:10 +0100
Organization: none
Message-ID: <unal66$uc4$1@news.gegeweb.eu>
References: <umet9d$3hir9$1@dont-email.me> <ummmqe$s4od$1@dont-email.me>
<87o7e8voy1.fsf@nosuchdomain.example.com> <20231229122012.850@kylheku.com>
<umnhtf$103sg$1@dont-email.me> <ums14k$1p0rs$1@dont-email.me>
<umsnbn$1ro5b$7@dont-email.me> <umuj4q$28384$1@dont-email.me>
<umun7n$28l7q$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <un5rpo$3jivl$2@dont-email.me>
<un67g4$3l74d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Jan 2024 04:33:10 -0000 (UTC)
Injection-Info: news.gegeweb.eu; posting-account="tontonth@usenet.local"; posting-host="2a01:cb19:8674:1100:fc25:8d52:9611:1e52";
logging-data="31108"; mail-complaints-to="abuse@gegeweb.eu"
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <un67g4$3l74d$1@dont-email.me>
Cancel-Lock: sha256:ErQdvx6mi3xRjD8dW21TmflAAGDoD6yurhXP1UreRag=
 by: tTh - Sat, 6 Jan 2024 04:33 UTC

On 1/4/24 13:15, Bart wrote:
>
>    for (i = 0; i<N; ++N)
>      ... Do something with A ...

And do nothing for the first element, big win.

--
+---------------------------------------------------------------------+
| https://tube.interhacker.space/a/tth/video-channels |
+---------------------------------------------------------------------+

Re: Effect of CPP tags

<unb03p$hchu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30636&group=comp.lang.c#30636

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 08:39:37 +0100
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <unb03p$hchu$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <871qavu5z9.fsf@nosuchdomain.example.com>
<20240105103833.654@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 07:39:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0215882be94597a6e6d53f95ee4af31b";
logging-data="569918"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++dPz5rg/R6y8OViu8DfeL8qvf/ql6qps="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TifE+chRUawGDOw1jGKhBwt+mGM=
In-Reply-To: <20240105103833.654@kylheku.com>
Content-Language: en-GB
 by: David Brown - Sat, 6 Jan 2024 07:39 UTC

On 05/01/2024 19:42, Kaz Kylheku wrote:
> On 2024-01-05, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> When you wrote "in Linux", I wondered if you were being imprecise, but
>> in fact that code is in the Linux kernel.
>>
>> That means the macros aren't directly available to normal C code, but
>> you can always copy their definitions (*if* you're using a compiler
>> that supports the __builtin_types_compatible_p extension).
>
> You can always copy their definitions, if you're using a compiler
> that doesn't arbitrarily define __GNUC__ without providing the
> associated behaviors:
>
> #ifdef __GNU__
>
> // define array_size in the Linux kernel way
>
> #else
>
> #define arrray_size(x) (sizeof (x)/sizeof *(x))
>
> #endif
>
> If you regularly build the code with a compiler that provides GNU
> extensions (like as part of your CI), you're covered, even if you're
> going to production with something else.
>
> I use C++ this way in C projects; I have some macro features that
> provide extra checks under C++. I get the benefit even if I just
> compile the code as C++ only once before every release.
>

That is a good tactic if your code needs to be used with compilers that
don't have such features for static checking (perhaps you are releasing
your source code, and can't influence the tools or settings users use).
In a way, you are using gcc (or C++) as a linter.

I've done this myself when the actual target compiler had little in the
way of static checking - I ran gcc in parallel, for a different target
(the generated output was ignored), but with lots of warning flags that
the real compiler did not support.

Re: Effect of CPP tags

<unb4v7$hum9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30637&group=comp.lang.c#30637

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 10:02:28 +0100
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <unb4v7$hum9$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un44ln$398sl$1@dont-email.me>
<un4bq2$3a8kc$1@dont-email.me> <un4ro2$3ce1u$1@dont-email.me>
<mgolN.15004$SyNd.1220@fx33.iad> <un54lv$3ddec$1@dont-email.me>
<RJAlN.141573$c3Ea.132913@fx10.iad> <un6toq$3ogqm$1@dont-email.me>
<%cFlN.140487$xHn7.115393@fx14.iad> <slrnupeb0t.qme.jj@iridium.wf32df>
<un7cjh$3qi9d$1@dont-email.me> <un7eoj$3qpmp$1@dont-email.me>
<un7ndt$3rr75$1@dont-email.me> <un92c7$6325$1@dont-email.me>
<un9ap4$77ft$2@dont-email.me> <h6YlN.142867$c3Ea.107377@fx10.iad>
<un9lir$8qt5$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 09:02:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0215882be94597a6e6d53f95ee4af31b";
logging-data="588489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gGHlu3GsbPFTwyolzfbWPhjUx+nEpPNU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:YzXSB3nie58VV55eqqSlDEj7m5Y=
Content-Language: en-GB
In-Reply-To: <un9lir$8qt5$2@dont-email.me>
 by: David Brown - Sat, 6 Jan 2024 09:02 UTC

On 05/01/2024 20:33, Bart wrote:

>
> And yet, despite it having no problems according to you, people still
> feel the need to create macros like ARRAY_SIZE.
>
> So I'm puzzled. Why do they do that? What imaginary problem (according
> to you) do they solve?
>

People are different, and like to do things different ways. I would be
a boring world if everyone solved the same problems or challenges
identically! Some people put the milk in their cup before their tea,
and some people put the tea in before the milk. Some drink their tea
without milk, some drink just the milk, some vary their habits, some
care a great deal and some don't care at all. Would you expect a
tea-before-milk fanatic to justify how a milk-before-tea fanatic thinks?

This is no different. Some people like to use a macro. Some people
like to write it out explicitly in the code. Some people like to use
other methods, such as referring to the constant used to define the
array. Some people like to use a language that provides an operator for
the task, or where they can define their own functions for the job.

Has your use of your own personal languages and tools over the years
made you so insular that you can't understand that different people have
different preferences?

Re: Effect of CPP tags

<unb5d3$hum9$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30638&group=comp.lang.c#30638

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 10:09:55 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <unb5d3$hum9$2@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Jan 2024 09:09:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0215882be94597a6e6d53f95ee4af31b";
logging-data="588489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SrAvqN++4wpiCUgy/sw+Mv+7xbkUiKVc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bQSdL+dn+zgVVW3kY996Iw0Td5Y=
In-Reply-To: <un9va1$a752$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 6 Jan 2024 09:09 UTC

On 05/01/2024 23:19, Bart wrote:
> On 05/01/2024 14:05, David Brown wrote:
>> On 05/01/2024 02:53, Bart wrote:
>
>>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>>
>>
>> One option for a useful array length operator/function/macro is a
>> simple and limited feature that works for arrays of known size and
>> gives a hard (compile-time) error when the size is not known.  The one
>> you have in your own language covers most of that, except for the
>> insanity of evaluating to 0 when given a pointer/reference to an
>> "unbounded" array.
>
> It gives zero because that is actually the compile-time type of the
> array. But it is low priority because it is never used that way.
>

That's the kind of potentially confusing short-cut you can make when
only you ever use the language. It is also fine for when people make
their own array_length macros for use in their own code. When you know
all the details of the operator/macro/function, and you are the only one
using it, it's okay if the specification is weird and unhelpful for some
cases. It's a different matter entirely if you are making something
that other people will use.

>
> In C, sizeof() is usually compile-time, except when used on VLAs, and
> here, as usual for VLAs, it is a lot more complicated than you'd think:
>

No it is not. I don't think "garbage in, garbage out" is a complicated
concept. You can write pathological shit in any language and get
meaningless results out - it has no bearing on anything in real code.
(It can be fun to play around with, but it does not mean there is a
problem with the language feature.)

Re: Effect of CPP tags

<unb9uk$igst$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30639&group=comp.lang.c#30639

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!nntp.comgw.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 10:27:32 +0000
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <unb9uk$igst$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
<unb5d3$hum9$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Jan 2024 10:27:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="607133"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Ewe6SA1VD/1DIDJoEbqOfwsmsOuhnUhI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:3aT/Hy6QQ2beQeXuUO+GxMzRpzw=
In-Reply-To: <unb5d3$hum9$2@dont-email.me>
Content-Language: en-GB
 by: Bart - Sat, 6 Jan 2024 10:27 UTC

On 06/01/2024 09:09, David Brown wrote:
> On 05/01/2024 23:19, Bart wrote:
>> On 05/01/2024 14:05, David Brown wrote:
>>> On 05/01/2024 02:53, Bart wrote:
>>
>>>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>>>
>>>
>>> One option for a useful array length operator/function/macro is a
>>> simple and limited feature that works for arrays of known size and
>>> gives a hard (compile-time) error when the size is not known.  The
>>> one you have in your own language covers most of that, except for the
>>> insanity of evaluating to 0 when given a pointer/reference to an
>>> "unbounded" array.
>>
>> It gives zero because that is actually the compile-time type of the
>> array. But it is low priority because it is never used that way.
>>
>
> That's the kind of potentially confusing short-cut you can make when
> only you ever use the language.  It is also fine for when people make
> their own array_length macros for use in their own code.  When you know
> all the details of the operator/macro/function, and you are the only one
> using it, it's okay if the specification is weird and unhelpful for some
> cases.  It's a different matter entirely if you are making something
> that other people will use.
>
>>
>> In C, sizeof() is usually compile-time, except when used on VLAs, and
>> here, as usual for VLAs, it is a lot more complicated than you'd think:
>>
>
> No it is not.  I don't think "garbage in, garbage out" is a complicated
> concept.  You can write pathological shit in any language and get
> meaningless results out - it has no bearing on anything in real code.
> (It can be fun to play around with, but it does not mean there is a
> problem with the language feature.)

And yet, in my example, lccwin32 gave the wrong result, and in one case
crashed. (I no longer have other smaller C compilers to try out.)

It suggests the problem is not trivial.

The concept is not that easy to get your head around either, the idea
that the variable aspects of a VLA are associated with its type, not its
value or instance.

In my example, there were no instances of any actual arrays, not even if
I declared an instance of one of the pointers involved, yet space had to
be allocated for its size. And I kept the type simple.

I remember also trying out VLAs within a struct, and there I got a wider
variance of results.

You can keep saying that VLAs are trivial to understand and implement; I
won't believe you.

Re: Effect of CPP tags

<unbih3$juqo$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30640&group=comp.lang.c#30640

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 12:53:56 +0000
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <unbih3$juqo$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
<unb5d3$hum9$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Jan 2024 12:53:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="654168"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EHweLFdsdspwkIfK34lX792CyNlBsbOY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:umni26ZwmEl7f7timjC9YxQrbQw=
In-Reply-To: <unb5d3$hum9$2@dont-email.me>
Content-Language: en-GB
 by: Bart - Sat, 6 Jan 2024 12:53 UTC

On 06/01/2024 09:09, David Brown wrote:
> On 05/01/2024 23:19, Bart wrote:
>> On 05/01/2024 14:05, David Brown wrote:
>>> On 05/01/2024 02:53, Bart wrote:
>>
>>>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>>>
>>>
>>> One option for a useful array length operator/function/macro is a
>>> simple and limited feature that works for arrays of known size and
>>> gives a hard (compile-time) error when the size is not known.  The
>>> one you have in your own language covers most of that, except for the
>>> insanity of evaluating to 0 when given a pointer/reference to an
>>> "unbounded" array.
>>
>> It gives zero because that is actually the compile-time type of the
>> array. But it is low priority because it is never used that way.
>>
>
> That's the kind of potentially confusing short-cut you can make when
> only you ever use the language.

I spent 10 mins trying to fix this today. So that '.len' on bounds
specified as `[]` rather than `[0]`, and not later set by init data,
would be an error.

Then I got stuck on examples like '[]int a = ()' and realised it's
tricky. And then I thought, why the hell am I doing this? A language
I've already used for a million lines of code.

Because somebody on the internet (who has never created and implemented
a real language AFAIK) told me so?

I don't claim it to be perfect, just that it does a better job than C.

At least it has '.len' and quite a few extra bits to do with fixed-size
arrays:

'M' C

Value arrays Y N
Ptr to array params Y N (also Y but non-idiomatic)
Pass-by-reference Y N

Index a pointer N Y
Deref an array N Y

0-based arrays Y Y
1-based Y N
N-based Y N

Number of bytes X.bytes sizeof(X)
T.bytes sizeof(T)

Array Length X.len sizeof(X)/sizeof(X[0])
T.len ?

Array Lower Bound X.lwb 0
T.lwb 0

Array Upper Bound X.upb sizeof(X)/sizeof(X[0])-1
T.upb ?

Table Indexing A[i,j] A[i][j] or j[A[i]] or j[i[A]]

Loop over bounds For i in A.bounds do
for (int i=0;
i<sizeof(A)/sizeof(A[0]); ++i)

Loop over values For x in A do
For i,x in A do # (both)
for (int i=0;
i<sizeof(A)/sizeof(A[0]); ++i) {
T x = A[i]; ...

Zero array clear A memset(A, 0, sizeof(A)/sizeof(A[0]));

Slices Y N

Re: Effect of CPP tags

<unbn37$knul$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30641&group=comp.lang.c#30641

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.cm (Bart)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 14:11:52 +0000
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <unbn37$knul$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
<unb5d3$hum9$2@dont-email.me> <unbih3$juqo$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 6 Jan 2024 14:11:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e3413a4c5308a82338b0d817d74fe26a";
logging-data="679893"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VWvEpMBziOl5jCEDV4hfLvTezDJmP+eI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:n/rai0jodjPy/EsOW9QtH+/sAn8=
In-Reply-To: <unbih3$juqo$1@dont-email.me>
Content-Language: en-GB
 by: Bart - Sat, 6 Jan 2024 14:11 UTC

On 06/01/2024 12:53, Bart wrote:
> On 06/01/2024 09:09, David Brown wrote:
>> On 05/01/2024 23:19, Bart wrote:

> At least it has '.len' and quite a few extra bits to do with
fixed-size arrays:
>
> 'M' C
>
> Value arrays Y N
> Ptr to array params Y N (also Y but non-idiomatic)
> Pass-by-reference Y N
>
> Index a pointer N Y
> Deref an array N Y
>
> 0-based arrays Y Y
> 1-based Y N
> N-based Y N
>
> Number of bytes X.bytes sizeof(X)
> T.bytes sizeof(T)
>
> Array Length X.len sizeof(X)/sizeof(X[0])
> T.len ?
>
> Array Lower Bound X.lwb 0
> T.lwb 0
>
> Array Upper Bound X.upb sizeof(X)/sizeof(X[0])-1
> T.upb ?
>
> Table Indexing A[i,j] A[i][j] or j[A[i]] or j[i[A]]
>
> Loop over bounds For i in A.bounds do
> for (int i=0;
i<sizeof(A)/sizeof(A[0]); ++i)
>
> Loop over values For x in A do
> For i,x in A do # (both)
> for (int i=0;
i<sizeof(A)/sizeof(A[0]); ++i) {
> T x = A[i]; ...
>
> Zero array clear A memset(A, 0, sizeof(A)/sizeof(A[0]));

The C is wrong; it should be just sizeof(A).

You can see how sizeof or sizeof/sizeof features quite a bit in the
right-hand column, but even the shorter .len is little seen on the left.

It's easy to get it wrong or nor immediately notice (unless your
initials are SL of course).

Re: Effect of CPP tags

<unbnpa$koa8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30642&group=comp.lang.c#30642

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Effect of CPP tags
Date: Sat, 6 Jan 2024 15:23:38 +0100
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <unbnpa$koa8$1@dont-email.me>
References: <umet9d$3hir9$1@dont-email.me> <un0pb2$2kp7q$1@dont-email.me>
<un1clg$2oq9u$1@dont-email.me> <un1hek$2ph62$1@dont-email.me>
<un1rdk$2quec$1@dont-email.me> <un25vp$1qvm$1@news.gegeweb.eu>
<un3hvm$36jtf$1@dont-email.me> <Z5flN.88873$vFZa.33923@fx13.iad>
<un44ln$398sl$1@dont-email.me> <un4bq2$3a8kc$1@dont-email.me>
<un4ro2$3ce1u$1@dont-email.me> <mgolN.15004$SyNd.1220@fx33.iad>
<un54lv$3ddec$1@dont-email.me> <RJAlN.141573$c3Ea.132913@fx10.iad>
<un6toq$3ogqm$1@dont-email.me> <%cFlN.140487$xHn7.115393@fx14.iad>
<slrnupeb0t.qme.jj@iridium.wf32df> <un7cjh$3qi9d$1@dont-email.me>
<un7eoj$3qpmp$1@dont-email.me> <un7ndt$3rr75$1@dont-email.me>
<un92c7$6325$1@dont-email.me> <un9va1$a752$1@dont-email.me>
<unb5d3$hum9$2@dont-email.me> <unb9uk$igst$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 6 Jan 2024 14:23:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0215882be94597a6e6d53f95ee4af31b";
logging-data="680264"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/tv73Q7z1oo6vd2AKvfiq6UcFjA/VB28A="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:3/C4GIdxOCS2yVpgcPK8UFMyiyo=
In-Reply-To: <unb9uk$igst$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 6 Jan 2024 14:23 UTC

On 06/01/2024 11:27, Bart wrote:
> On 06/01/2024 09:09, David Brown wrote:
>> On 05/01/2024 23:19, Bart wrote:
>>> On 05/01/2024 14:05, David Brown wrote:
>>>> On 05/01/2024 02:53, Bart wrote:
>>>
>>>>> It doesn't need to take a heavy-handed approach like C++ or Python.
>>>>>
>>>>
>>>> One option for a useful array length operator/function/macro is a
>>>> simple and limited feature that works for arrays of known size and
>>>> gives a hard (compile-time) error when the size is not known.  The
>>>> one you have in your own language covers most of that, except for
>>>> the insanity of evaluating to 0 when given a pointer/reference to an
>>>> "unbounded" array.
>>>
>>> It gives zero because that is actually the compile-time type of the
>>> array. But it is low priority because it is never used that way.
>>>
>>
>> That's the kind of potentially confusing short-cut you can make when
>> only you ever use the language.  It is also fine for when people make
>> their own array_length macros for use in their own code.  When you
>> know all the details of the operator/macro/function, and you are the
>> only one using it, it's okay if the specification is weird and
>> unhelpful for some cases.  It's a different matter entirely if you are
>> making something that other people will use.
>>
>>>
>>> In C, sizeof() is usually compile-time, except when used on VLAs, and
>>> here, as usual for VLAs, it is a lot more complicated than you'd think:
>>>
>>
>> No it is not.  I don't think "garbage in, garbage out" is a
>> complicated concept.  You can write pathological shit in any language
>> and get meaningless results out - it has no bearing on anything in
>> real code. (It can be fun to play around with, but it does not mean
>> there is a problem with the language feature.)
>
>
> And yet, in my example, lccwin32 gave the wrong result, and in one case
> crashed. (I no longer have other smaller C compilers to try out.)
>
> It suggests the problem is not trivial.

So what you are telling us is that a relatively simple and little-used
compiler has bugs in dealing with pathological cases of code
intentionally designed to be difficult and be far from any kind of
realistic code? Are we supposed to be surprised? I'm sure the lccwin32
author will be interested in the test case, because such extreme
corner-case testing is useful for finding bugs in software - but it is
only of interest to him and not to anyone actually writing C code. (And
in this case, since your code has UB, there is no such thing as a
correct result - and therefore no possibility of a wrong result.)

>
> The concept is not that easy to get your head around either, the idea
> that the variable aspects of a VLA are associated with its type, not its
> value or instance.

If you find VLA's difficult, don't use them. Even if you were
interested in making your tools compliant, they are optional from C11
onwards. Since you are the only person who uses your tools, and you
won't write C code with VLAs, you have nothing to lose by ignoring them
entirely.

>
> In my example, there were no instances of any actual arrays, not even if
> I declared an instance of one of the pointers involved, yet space had to
> be allocated for its size. And I kept the type simple.
>
> I remember also trying out VLAs within a struct, and there I got a wider
> variance of results.
>
> You can keep saying that VLAs are trivial to understand and implement; I
> won't believe you.
>

OK. Don't believe me.


devel / comp.lang.c / Effect of CPP tags

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor