Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

19 May, 2024: Line wrapping has been changed to be more consistent with Usenet standards.
 If you find that it is broken please let me know here rocksolid.nodes.help


devel / comp.arch / Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

SubjectAuthor
* On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
|+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
|| +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
|| `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
|+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Bill Findlay
||`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
|+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Anton Ertl
||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Niklas Holsti
|||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||| +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||| |`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Niklas Holsti
||| +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Niklas Holsti
||| |`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Jean-Marc Bourguet
||| `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
|||`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
|| +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
|| |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
|| |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
|| |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Tim Rentsch
|| | `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(chrisq
|| |  `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Tim Rentsch
|| `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Anton Ertl
||  +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||  |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Anton Ertl
||  ||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||  || `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Tim Rentsch
||  ||  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||  ||   `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Tim Rentsch
||  |`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
||   `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Anton Ertl
||    +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||    |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||    |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Niklas Holsti
||    ||+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||    ||`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Anton Ertl
||    | +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||    | |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | | +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(robf...@gmail.com
||    | | `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(robf...@gmail.com
||    | |  |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
||    | |  |+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Timothy McCaffrey
||    | |  || +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  || `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
||    | |  |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    | |  | +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  | +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  | |`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    | |  | `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(John Dallman
||    | |  |  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    | |  |   `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  |    `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(EricP
||    | |  |     `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  |      `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(EricP
||    | |  |       `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  |||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
||    | |  ||||`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  |||+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Timothy McCaffrey
||    | |  |||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Timothy McCaffrey
||    | |  ||||`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Stephen Fuld
||    | |  |||+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Stephen Fuld
||    | |  |||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||| +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  ||| `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  |||  `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  || +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  || |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  || | `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  || |  +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  || |  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
||    | |  || |   `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  || `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(John Dallman
||    | |  ||  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  ||   +* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||   |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
||    | |  ||   ||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||   || `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Terje Mathisen
||    | |  ||   |+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  ||   ||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||   |||+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  ||   ||||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||   |||| `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  ||   |||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Tim Rentsch
||    | |  ||   ||| `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(chrisq
||    | |  ||   ||`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  ||   || `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(BGB
||    | |  ||   ||  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Stephen Fuld
||    | |  ||   ||   `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Scott Lurndal
||    | |  ||   |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    | |  ||   `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(John Dallman
||    | |  |`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | |  `* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(MitchAlsup
||    | +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Michael S
||    | `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
||    +- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(David Brown
||    `- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Mike Stump
|+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
|`- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(John Dallman
+* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Andy Valencia
+- Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Thomas Koenig
+* Re: C !!num idiom [was On the pitfalls of C and UB: C23 seems toEricP
`* Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(Peter Lund

Pages:123456789
Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<2023Apr10.164359@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31615&group=comp.arch#31615

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 14:43:59 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 27
Message-ID: <2023Apr10.164359@mips.complang.tuwien.ac.at>
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <68VYL.2039083$9sn9.730653@fx17.iad>
Injection-Info: dont-email.me; posting-host="d467012bc54a5e50b1ef0fc7e27c5d2c";
logging-data="2328499"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19YdtuKT6PRFIDZTZPa73Yk"
Cancel-Lock: sha1:jDm6x77DQ0fFVqxBLzZMFRtsyjQ=
X-newsreader: xrn 10.11
 by: Anton Ertl - Mon, 10 Apr 2023 14:43 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>David Brown <david.brown@hesbynett.no> writes:
>>>memmove(), and other standard library functions, are part of the
>>>implementation. They were traditionally implemented in assembly, and
>>>only later in C. But as part of the implementation, they can use
>>>features that are implementation-specific - there was never any need or
>>>intention that such functions should be implementable in portable
>>>standards-compliant C. The language never had any need to support
>>>writing an efficient memmove() in C - it gave you a ready-made memmove()
>>>function so that implementing it yourself is not necessary.
>>
>>For a supposed systems programming language like C, the inability to
>>implement memmove() is a failure.
>
>How so? A simple byte-by-byte loop, reversed if necessary,
>is sufficient. E.g. subtract the two pointers and depending on whether
>the subtraction is negative or positive do a byte-by-byte copy either
>forward or in reverse. Perfectly legal C, if not particuarly performant.

Subtraction of two pointers that do not point to the same object is
undefined behaviour, too.

- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<KwVYL.1294624$gGD7.576331@fx11.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31616&group=comp.arch#31616

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.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: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Newsgroups: comp.arch
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <68VYL.2039083$9sn9.730653@fx17.iad> <2023Apr10.164359@mips.complang.tuwien.ac.at>
Lines: 27
Message-ID: <KwVYL.1294624$gGD7.576331@fx11.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 10 Apr 2023 15:00:58 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 10 Apr 2023 15:00:58 GMT
X-Received-Bytes: 2368
 by: Scott Lurndal - Mon, 10 Apr 2023 15:00 UTC

anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>>David Brown <david.brown@hesbynett.no> writes:
>>>>memmove(), and other standard library functions, are part of the
>>>>implementation. They were traditionally implemented in assembly, and
>>>>only later in C. But as part of the implementation, they can use
>>>>features that are implementation-specific - there was never any need or
>>>>intention that such functions should be implementable in portable
>>>>standards-compliant C. The language never had any need to support
>>>>writing an efficient memmove() in C - it gave you a ready-made memmove()
>>>>function so that implementing it yourself is not necessary.
>>>
>>>For a supposed systems programming language like C, the inability to
>>>implement memmove() is a failure.
>>
>>How so? A simple byte-by-byte loop, reversed if necessary,
>>is sufficient. E.g. subtract the two pointers and depending on whether
>>the subtraction is negative or positive do a byte-by-byte copy either
>>forward or in reverse. Perfectly legal C, if not particuarly performant.
>
>Subtraction of two pointers that do not point to the same object is
>undefined behaviour, too.

An array object that encompasses the entire virtual address space
is easily defined on modern (nonsegmented) processors, in which case
ptrdiff_t is defined for subtraction of any two virtual addresses.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u118f4$27ckk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31617&group=comp.arch#31617

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 17:03:00 +0200
Organization: A noiseless patient Spider
Lines: 174
Message-ID: <u118f4$27ckk$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 10 Apr 2023 15:03:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0754576eefb1e753d18a5603f257975e";
logging-data="2339476"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/caQHxQPh9x+YXsWb1mljnehYeQ2Nhcqs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:lCF54tPeyp+4Mv0TCLqUBzTUj9Y=
In-Reply-To: <2023Apr10.143646@mips.complang.tuwien.ac.at>
Content-Language: en-GB
 by: David Brown - Mon, 10 Apr 2023 15:03 UTC

On 10/04/2023 14:36, Anton Ertl wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 04/04/2023 07:39, Anton Ertl wrote:
>>> One reason for comparing pointers is if you want to implement
>>> memmove(), as discussed in the article.
>>
>> memmove(), and other standard library functions, are part of the
>> implementation. They were traditionally implemented in assembly, and
>> only later in C. But as part of the implementation, they can use
>> features that are implementation-specific - there was never any need or
>> intention that such functions should be implementable in portable
>> standards-compliant C. The language never had any need to support
>> writing an efficient memmove() in C - it gave you a ready-made memmove()
>> function so that implementing it yourself is not necessary.
>
> For a supposed systems programming language like C, the inability to
> implement memmove() is a failure.

If you want to implement a function "my_memmove" in C that acts like
"memmove" but is written in fully portable C, you can do so :

#include <string.h>

void * my_memmove(void * s1, const void * s2, size_t n) {
return memmove(s1, s2, n);
}

There. Finished.

That was perhaps a bit patronising, but the standard library is part of
C. It is not some extra feature - writing code that calls standard
library functions is as much "programming in C" as writing "for" loops
and using pointers to unsigned chars.

Now, I will certainly agree that it would be /nice/ to have a fully
portable way to implement the standard library function "memmove" in C.
I just don't agree that it is /necessary/ - or that the lack of such a
method is a "failure". As with any language, there are always things
that people would like to do in the language, but can't - the
alternative is that people will complain that the language (and/or
standard library) is too big. (See C++ for evidence of that!)

Writing a mostly-portable memmove() is not difficult - for a great many
implementations, you can convert the pointers to "uintptr_t" and use the
results to choose between forwards or backwards movement. For the
platforms where you can't do that, you're going to have no sensible
comparison of ordinary pointers anyway. But even if you could portably
compare pointers, you are going to have a great deal of difficulty
writing something that is efficient without making it
implementation-specific - the ideal lump size, unrolling thresholds,
alignment handling, etc., is going to be highly dependent on the target.

Thus on good quality compilers, real-life calls to functions like
"memmove" or "memcpy" will often result in direct code generation,
rather than a library call (they may even be omitted altogether).
Likewise, manual code may be turned into calls to memove() or memcpy().
The actual usefulness of being able to implement memmove() is rather
limited.

I do agree that it would be nice for arbitrary pointer comparisons to be
valid in cases where it makes sense, and being able to implement
"memmove" would fall out of that - but I don't think being able to
implement "memmove" is good motivation for such a feature. I'd rank
being able to sort pointers for making binary trees or other data
structures as being more relevant. (And perhaps also for garbage
collection algorithms or other points discussed in this thread - I don't
know enough about the implementations of such algorithms to comment.)

>
>> It's absolutely fine to say that you'd /like/ C to support comparison of
>> arbitrary pointers, and there are other uses that are more useful than
>> replicating existing standard library functions.
>
> "memmove()" more succinct than describing the general case of an
> operation that reads from one memory block and writes to a potentially
> overlapping block in a way that is equivalent to doing all the reading
> before doing any of the writing. We leave it to the intelligence of
> the reader to extrapolate from "memmove()" to the general case. And
> of course it's clear that an advocate like you will try to confuse
> things by pointing out that the special case that was so succinctly
> expressed can be fulfilled with the succinct library call.
>

I am trying to say how things are, to look at what things are important
or merely nice to have, and to look at the implications of insisting on
particular features. I am a believer in specifying things that are
useful to specify, and leaving unspecified or undefined things that are
not - because I find the freedom useful. I am fully aware that
different people would prefer different balances, and that different
languages have different balances from C. I also appreciate that many
people feel that C has design flaws or would prefer it to be somewhat
different - I don't know of anyone, including myself, who is a
significant user of C and who things the language or its standards are
ideal. All languages are compromises, because users disagree about
different things in the language.

The difference between us is not that /you/ think C is broken and /I/
think it is perfect - I don't think it is perfect. It is not even just
a matter of differing in which things we would want to change. It is
that I look at C and see a language which is quite good, but not quite
what I'd like - thus I learn about how it works and decide I can use it
even though it is sub-optimal for /me/. You, apparently, see the
aspects of C that you don't like and declare that the language is broken
and compilers are broken for the way they handle the aspects you dislike.

I am aware that my personal needs and preferences are my own, while you
think your needs and preferences are fundamental and apply to all. Is
that what you mean when you say I am "an advocate" ?

>>> If you
>>> want a language that has a flat address space, use Forth.
>>>
>>
>> There are plenty of systems where you don't have a single flat address
>> space, and it's nice to be able to use C on them. An example would be
>> the extremely popular AVR microcontrollers, where flash and ram IO
>> spaces are entirely separate (with different assembly instructions for
>> accessing them).
>
> There are several Forth systems for AVR microcontrollers. The
> Forth-Gesellschaft even made a whole special edition of it's magazine
> for AVR
> <https://forth-ev.de/wiki/res/lib/exe/fetch.php/vd-archiv:4d2007-sonderheft-avr.pdf>
>
> So a flat address space in the programming language does not prevent
> implementation on the AVR.
>

Correct. But Forth is not C.

>> And then you have segmented address architectures,
>> like x86 - comparing pointers for ordering can be complicated if
>> calculations are needed to determine the real address.
>
> There is no "x86" architecture. If you mean the 8086, there have been
> many Forth systems for that. Some are for the tiny or small memory
> models. If you want one that deals with more memory, look at F-PC (in
> particular, look for "Internals" in
> <https://github.com/uho/F-PC/blob/main/fpc/fpcwords.txt>). There the
> standard Forth words @, !, etc. access 64KB of memory (making F-PC a
> medium-model system in terms of 8086 memory models), but it also has
> words @L, !L, CMOVEL etc. for accessing more memory; the latter words
> did not find the way into the Forth standard, and nobody has ever
> publically missed them. Looking at
> <https://devblogs.microsoft.com/oldnewthing/20200728-00/?p=104012>,
> similar things were used in C, e.g., _fmemcpy() for copying involving
> far pointers (probably a non-standard C type).
>
>> But are you going to allow "p < q" to be valid
>> while "q - p" might not be, due to an overflow in the ptrdiff_t range?
>
> C has this problem even without the 8086 nonsense and with the
> restriction of comparing and subtracting only pointers to the same
> object. I.e., standard C has this problem. Consider a sane system
> like a PDP-11, and you have an array
>
> char c[40000U];
> char *p=c;
> char *q=&c[39999];
>
> The C standard defines what q<p means, but does not define what p-q
> means, does it?
>

The answer is implementation-dependent, but could certainly be "yes".

There are always going to be trade-offs in how much a language can
specify, its efficient use in some cases, and how sensible or useful the
specifications are.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u119rf$27iis$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31618&group=comp.arch#31618

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 17:26:39 +0200
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <u119rf$27iis$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<u1132k$26kkr$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 10 Apr 2023 15:26:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0754576eefb1e753d18a5603f257975e";
logging-data="2345564"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18GYY1qABARgt7csJ8/ww99KWR2ItVve1I="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:c7JC91yafqbjLZmRevOR/FsPzC0=
In-Reply-To: <u1132k$26kkr$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Mon, 10 Apr 2023 15:26 UTC

On 10/04/2023 15:30, Terje Mathisen wrote:
> David Brown wrote:
>> On 04/04/2023 07:39, Anton Ertl wrote:
>>> One reason for comparing pointers is if you want to implement
>>> memmove(), as discussed in the article.
>>
>> memmove(), and other standard library functions, are part of the
>> implementation.  They were traditionally implemented in assembly, and
>> only later in C.  But as part of the implementation, they can use
>> features that are implementation-specific - there was never any need
>> or intention that such functions should be implementable in portable
>> standards-compliant C.  The language never had any need to support
>> writing an efficient memmove() in C - it gave you a ready-made
>> memmove() function so that implementing it yourself is not necessary.
>
> Is pointer comparison implementation defined (ID) or UB?

Pointer equality comparison is defined behaviour for any two pointers to
compatible types or void.

Comparison for order (less than or greater than) is valid only if the
two pointers point to something within the same aggregate object (or one
past the end of an array). Otherwise it is undefined behaviour.

>
> If it is ID, then I can write my own version of memmove() (or similar
> ops) on any given compiler, as long as I do it the way that particular
> compiler have defined it, right?

That would be correct. But since the comparison you need is for
ordering, and it is /not/ implementation-defined behaviour, you can't
rely on it for fully portable code.

Of course implementations are free to support comparisons that are not
defined by the C standards - for most targets, a "pointer" will simply
contain an address (possibly a virtual address), and implementations are
free to treat comparisons of arbitrary pointers as simple address
comparisons.

When implementing an arbitrary function that takes two void* pointers,
the implementation will have to assume that they /are/ pointing within
the same array or structure, unless it is able to prove otherwise. So
you can expect code to be generated accordingly. However, the compiler
can handle things differently if it is able to see the source of the
pointers at the same time as interpreting the comparison.

Pointers can also be converted back and forth between integer types, in
an implementation-dependent manner. There's no guarantee that a given
integer type will be big enough to support all pointer values - but if
there is such a type, then "uintptr_t" and "intptr_t" will be big enough
and can be used. So you can write your "memmove" function by casting
the pointers to "uintptr_t" and then doing your comparison. It is not
fully portable, but will work for almost any system except for a few
implementations that use wide pointers which encode access information
in addition to the address. (There's also the hypothetical possibility
that "(uintptr_t)(p + 1)" does not have to equal "(uintptr_t) p +
sizeof(*p)", but that's highly unlikely in practice.)

>
> If it is UB, then that's impossible, you have to step outside of the C
> standard to implement it, and do so in a way that makes it impossible
> for the compiler to discover that you have done so. :-)
>

No, UB does not mean this is impossible - it means it is not guaranteed
to be possible. There's a difference. In practice, almost all modern C
libraries will implement memmove() in C, relying on implementation
features that may or may not be documented but which the library
implementers know are valid for the library code. You are not stepping
outside C, merely outside of fully portable standards-compliant C. The
C standards never intended for the C standard library to be written in
"pure" portable C - the standard library is part of the implementation,
and its own source code can use special features of the implementation.

>> It's absolutely fine to say that you'd /like/ C to support comparison
>> of arbitrary pointers, and there are other uses that are more useful
>> than replicating existing standard library functions.  A lot of
>> implementations will support it - or at the very least, they will have
>> a "uintptr_t" type so that you can cast the pointers to uintptr_t
>> values and compare those.
>
> That's fine: If I can take two arbitrary pointers and convert them to
> uintptr_t, and the resulting values are comparable, at least to the
> point where it is possible to discover if one object could overlap
> another, then I can write my own memmove().
>

Yes - except that "uintptr_t" is not guaranteed to exist. I have never
used a C99+ system that did /not/ support it, but they do exist.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u11iff$28r0r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31619&group=comp.arch#31619

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: cr88...@gmail.com (BGB)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 12:53:45 -0500
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <u11iff$28r0r$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at>
<68VYL.2039083$9sn9.730653@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 10 Apr 2023 17:53:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2411eb9bd174bbfd2feaf4a7ad7088d2";
logging-data="2386971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nCI4LFIc2le7wUw2WsAcb"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.9.1
Cancel-Lock: sha1:aUnGZxvgxg7WvHf/CieUkFpBAYo=
In-Reply-To: <68VYL.2039083$9sn9.730653@fx17.iad>
Content-Language: en-US
 by: BGB - Mon, 10 Apr 2023 17:53 UTC

On 4/10/2023 9:34 AM, Scott Lurndal wrote:
> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 04/04/2023 07:39, Anton Ertl wrote:
>>>> One reason for comparing pointers is if you want to implement
>>>> memmove(), as discussed in the article.
>>>
>>> memmove(), and other standard library functions, are part of the
>>> implementation. They were traditionally implemented in assembly, and
>>> only later in C. But as part of the implementation, they can use
>>> features that are implementation-specific - there was never any need or
>>> intention that such functions should be implementable in portable
>>> standards-compliant C. The language never had any need to support
>>> writing an efficient memmove() in C - it gave you a ready-made memmove()
>>> function so that implementing it yourself is not necessary.
>>
>> For a supposed systems programming language like C, the inability to
>> implement memmove() is a failure.
>
> How so? A simple byte-by-byte loop, reversed if necessary,
> is sufficient. E.g. subtract the two pointers and depending on whether
> the subtraction is negative or positive do a byte-by-byte copy either
> forward or in reverse. Perfectly legal C, if not particuarly performant.
>
> In real life, memmove is often implemented in assembler for performance.
>

On many (most?) modern targets, a naive byte-for-byte copy loop is
potentially orders of magnitude slower than using larger units (such as
copying the data 128 bits at a time, and overlapping the loads and
stores in the pipeline).

This was less true of 32-bit x86, where one is limited mostly to moving
32 bits at a time, and the limited register space isn't sufficient to
fully pipeline the copy. I had noted, for example, that on a 20 year old
laptop, this causes memcpy (for small buffers) to peak out at only
around 800 MB/s. This would drop to 200 MB/s if copying a byte at a time.

Granted, this is still faster than what one gets if the buffer doesn't
fit into the L1 cache (seemingly early 2000s RAM wasn't very fast by
modern standards).

My modern desktop PC is faster here on both metrics by over an order of
magnitude.

Granted, it may be possible to do better here, dunno.

Note that in general, laptop is fast enough to run Half-Life and Quake3.
Its GPU seems to be R100 based (fixed-function, OpenGL 1.3 and DX7).

Half-Life 2 sorta works (some graphical limitations and single-digit
frame-rates); pretty much anything newer is no-go.

And, ironically, the ~ 280 MB/s for L1 memcpy I can get on my BJX2 core
is only around 3x slower than said laptop (despite the laptop having a
fairly large clock-speed advantage).

Mostly all due to ability to do fully pipelined 128-bit load/store
sequences.

Meanwhile, what about a naive byte copy loop?...
How about around 4 MB/s ...

Note that in a naive sense, Doom would also be limited in this way,
however one can sorta organize the column drawing loops and similar such
that the inner parts handle multiple pixels at a time in a "closer to
pipelined" fashion, and thus "not suck quite as bad", mostly falling
back to "one pixel at a time" for the last few pixels of the column (*).

....

*: Had tested, and working directly in RGB555 with RGB modulation in
place of the colormap tables didn't really offer any real speed
advantage. However, because of the more accurate luma scaling, it caused
the "brightness drops off slightly with distance" effect to have much
more obvious banding artifacts (with the colormap tables, these add a
little jitter that reduces the effect).

My current port still uses color-map tables, however it has switched to
partially blending the original palette colors with a calculated
RGB-scaled value, slightly increasing luma accuracy but not so much as
to cause the banding to become super obvious (the color map tables and
framebuffer in this case operating using RGB555 / hi-color).

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<2023Apr10.175439@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31620&group=comp.arch#31620

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 15:54:39 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 95
Message-ID: <2023Apr10.175439@mips.complang.tuwien.ac.at>
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
Injection-Info: dont-email.me; posting-host="d467012bc54a5e50b1ef0fc7e27c5d2c";
logging-data="2379182"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QLf4vY3uL4rgiubK1ycSu"
Cancel-Lock: sha1:a4gm2wCS98fS1SfrspTa2Dz9TqE=
X-newsreader: xrn 10.11
 by: Anton Ertl - Mon, 10 Apr 2023 15:54 UTC

David Brown <david.brown@hesbynett.no> writes:
>On 10/04/2023 14:36, Anton Ertl wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>> On 04/04/2023 07:39, Anton Ertl wrote:
[...]
>#include <string.h>
>
>void * my_memmove(void * s1, const void * s2, size_t n) {
> return memmove(s1, s2, n);
>}
>
>There. Finished.

Well done. Next:

vadd(double *a, double *b, double c, size_t n)
{ /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
parameters are passed according to C rules, so a and b may or may
not be pointers into the same array */
}

>As with any language, there are always things
>that people would like to do in the language, but can't - the
>alternative is that people will complain that the language (and/or
>standard library) is too big.

C already includes pointer comparison. Making it undefined for
certain cases increases the things a programmer has to remember in
order to use C, i.e., the language the programmer experiences becomes
bigger.

>(See C++ for evidence of that!)

Does C++ define pointer comparison? I don't think so. So C++ bigness
is, at best, an argument for my position.

>The difference between us is not that /you/ think C is broken and /I/
>think it is perfect - I don't think it is perfect. It is not even just
>a matter of differing in which things we would want to change. It is
>that I look at C and see a language which is quite good, but not quite
>what I'd like - thus I learn about how it works and decide I can use it
>even though it is sub-optimal for /me/. You, apparently, see the
>aspects of C that you don't like and declare that the language is broken
>and compilers are broken for the way they handle the aspects you dislike.

No, the difference is that I see production C programs that are
compiled as intended by some compilers, and if another release of the
compiler miscompiles the program, I see this as bug in the compiler.
Some people claim that the program is buggy and has always been, and
base this claim on certain parts of the C standard. Of course, they
prefer to ignore other parts of the C standard, such as the parts
about conforming and strictly conforming C programs.

>>> There are plenty of systems where you don't have a single flat address
>>> space, and it's nice to be able to use C on them. An example would be
>>> the extremely popular AVR microcontrollers, where flash and ram IO
>>> spaces are entirely separate (with different assembly instructions for
>>> accessing them).
>>
>> There are several Forth systems for AVR microcontrollers. The
>> Forth-Gesellschaft even made a whole special edition of it's magazine
>> for AVR
>> <https://forth-ev.de/wiki/res/lib/exe/fetch.php/vd-archiv:4d2007-sonderheft-avr.pdf>
>>
>> So a flat address space in the programming language does not prevent
>> implementation on the AVR.
>>
>
>Correct. But Forth is not C.

And the relevance of that is? If Forth can be implement on the AVR,
surely a flat-address-space C can be implemented on the AVR, too.

>> Consider a sane system
>> like a PDP-11, and you have an array
>>
>> char c[40000U];
>> char *p=c;
>> char *q=&c[39999];
>>
>> The C standard defines what q<p means, but does not define what p-q
>> means, does it?
>>
>
>The answer is implementation-dependent, but could certainly be "yes".

Is this another of your dreamed-up scenarios, one where ptrdiff_t on a
PDP-11 is larger than 16 bits? Or can you name an implementation
where p-q is defined for the case above?

- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31621&group=comp.arch#31621

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:6214:a53:b0:5ad:953f:3bcd with SMTP id ee19-20020a0562140a5300b005ad953f3bcdmr1678894qvb.7.1681151675728;
Mon, 10 Apr 2023 11:34:35 -0700 (PDT)
X-Received: by 2002:a05:6870:e392:b0:183:fa09:d20b with SMTP id
x18-20020a056870e39200b00183fa09d20bmr5244023oad.5.1681151675430; Mon, 10 Apr
2023 11:34:35 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 11:34:35 -0700 (PDT)
In-Reply-To: <2023Apr10.175439@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: already5...@yahoo.com (Michael S)
Injection-Date: Mon, 10 Apr 2023 18:34:35 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 36
 by: Michael S - Mon, 10 Apr 2023 18:34 UTC

On Monday, April 10, 2023 at 9:00:45 PM UTC+3, Anton Ertl wrote:
> David Brown <david...@hesbynett.no> writes:
> >On 10/04/2023 14:36, Anton Ertl wrote:
> >> David Brown <david...@hesbynett.no> writes:
> >>> On 04/04/2023 07:39, Anton Ertl wrote:
> [...]
> >#include <string.h>
> >
> >void * my_memmove(void * s1, const void * s2, size_t n) {
> > return memmove(s1, s2, n);
> >}
> >
> >There. Finished.
> Well done. Next:
>
> vadd(double *a, double *b, double c, size_t n)
> {
> /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
> parameters are passed according to C rules, so a and b may or may
> not be pointers into the same array */
> }
>

In Fortran, when a and b are pointers to the same array the result is
undefined. I suppose, it's not what you had in mind.

I suppose, you meant to produce the same result as slow_vadd() but efficiently.
slow_vadd(double *a, double *b, double c, size_t n)
{ double* tmp = malloc(n*sizeof(*tmp));
for (size_t i = 0; i < n; ++i)
tmp[i] = b[i] + c;
memcpy(a, tmp, n*sizeof(*tmp));
}

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<06c04eba-87ce-4fcc-bbf8-31e0a68c4857n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31622&group=comp.arch#31622

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:622a:1899:b0:3d9:556b:10dd with SMTP id v25-20020a05622a189900b003d9556b10ddmr4259109qtc.11.1681151786337;
Mon, 10 Apr 2023 11:36:26 -0700 (PDT)
X-Received: by 2002:aca:2203:0:b0:389:6b0e:9d8f with SMTP id
b3-20020aca2203000000b003896b0e9d8fmr3131643oic.0.1681151786075; Mon, 10 Apr
2023 11:36:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!panix!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 11:36:25 -0700 (PDT)
In-Reply-To: <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <06c04eba-87ce-4fcc-bbf8-31e0a68c4857n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: already5...@yahoo.com (Michael S)
Injection-Date: Mon, 10 Apr 2023 18:36:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2904
 by: Michael S - Mon, 10 Apr 2023 18:36 UTC

On Monday, April 10, 2023 at 9:34:37 PM UTC+3, Michael S wrote:
> On Monday, April 10, 2023 at 9:00:45 PM UTC+3, Anton Ertl wrote:
> > David Brown <david...@hesbynett.no> writes:
> > >On 10/04/2023 14:36, Anton Ertl wrote:
> > >> David Brown <david...@hesbynett.no> writes:
> > >>> On 04/04/2023 07:39, Anton Ertl wrote:
> > [...]
> > >#include <string.h>
> > >
> > >void * my_memmove(void * s1, const void * s2, size_t n) {
> > > return memmove(s1, s2, n);
> > >}
> > >
> > >There. Finished.
> > Well done. Next:
> >
> > vadd(double *a, double *b, double c, size_t n)
> > {
> > /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
> > parameters are passed according to C rules, so a and b may or may
> > not be pointers into the same array */
> > }
> >
> In Fortran, when a and b are pointers to the same array the result is
> undefined. I suppose, it's not what you had in mind.
>
> I suppose, you meant to produce the same result as slow_vadd() but efficiently.
> slow_vadd(double *a, double *b, double c, size_t n)
> {
> double* tmp = malloc(n*sizeof(*tmp));
> for (size_t i = 0; i < n; ++i)
> tmp[i] = b[i] + c;
> memcpy(a, tmp, n*sizeof(*tmp));
> }

Ogh, I forgot free(tmp);

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<k9jbe3Fl00gU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31623&group=comp.arch#31623

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.h...@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 23:55:31 +0300
Organization: Tidorum Ltd
Lines: 41
Message-ID: <k9jbe3Fl00gU1@mid.individual.net>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net D344at03qE4EmE+iapli0wsJQuMOKRQ9+pQUR/CYVFa+8Cl2A9
Cancel-Lock: sha1:rN6akt2D8UWhx/bEXFxwhtE6uek=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.6.1
Content-Language: en-US
In-Reply-To: <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
 by: Niklas Holsti - Mon, 10 Apr 2023 20:55 UTC

On 2023-04-10 21:34, Michael S wrote:
> On Monday, April 10, 2023 at 9:00:45 PM UTC+3, Anton Ertl wrote:
>> David Brown <david...@hesbynett.no> writes:
>>> On 10/04/2023 14:36, Anton Ertl wrote:
>>>> David Brown <david...@hesbynett.no> writes:
>>>>> On 04/04/2023 07:39, Anton Ertl wrote:
>> [...]
>>> #include <string.h>
>>>
>>> void * my_memmove(void * s1, const void * s2, size_t n) {
>>> return memmove(s1, s2, n);
>>> }
>>>
>>> There. Finished.
>> Well done. Next:
>>
>> vadd(double *a, double *b, double c, size_t n)
>> {
>> /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
>> parameters are passed according to C rules, so a and b may or may
>> not be pointers into the same array */
>> }
>>
>
> In Fortran, when a and b are pointers to the same array the result is
> undefined. I suppose, it's not what you had in mind.

Are you sure about that? The F2018 Interpretation Document states, for
an assignment of the form "variable = expr", that:

"The execution of the assignment shall have the same effect as if the
evaluation of expr and the evaluation of all expressions in variable
occurred before any portion of the variable is defined by the assignment."

(https://j3-fortran.org/doc/year/18/18-007r1.pdf, section 10.2.1.2)

So the Fortran assignment should work as if the right-hand expr is
evaluated into a temporary, and then the temporary is assigned (moved)
to the left-hand variable.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<2023Apr10.230314@mips.complang.tuwien.ac.at>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31624&group=comp.arch#31624

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: ant...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 21:03:14 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 36
Message-ID: <2023Apr10.230314@mips.complang.tuwien.ac.at>
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
Injection-Info: dont-email.me; posting-host="d467012bc54a5e50b1ef0fc7e27c5d2c";
logging-data="2446864"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yBtSn6rM5dlgorecfSHdy"
Cancel-Lock: sha1:NnHL5nYeZ1LibRLfte/d2m4P4fk=
X-newsreader: xrn 10.11
 by: Anton Ertl - Mon, 10 Apr 2023 21:03 UTC

Michael S <already5chosen@yahoo.com> writes:
>On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
>> vadd(double *a, double *b, double c, size_t n)=20
>> {=20
>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
>=20
>> parameters are passed according to C rules, so a and b may or may=20
>> not be pointers into the same array */
>> }=20
>>=20
>
>In Fortran, when a and b are pointers to the same array the result is=20
>undefined.

Thomas Koenig wrote something along the lines that there are standard
Fortran idioms that generate aliases, and that if the compiler does
not know that the operands in an array expression may overlap, the
compiler has to generate code that takes potential overlap into
account. What in particular is undefined IYO?

>I suppose, you meant to produce the same result as slow_vadd() but efficie=
>ntly.
>slow_vadd(double *a, double *b, double c, size_t n)
>{
> double* tmp =3D malloc(n*sizeof(*tmp));
> for (size_t i =3D 0; i < n; ++i)
> tmp[i] =3D b[i] + c;
> memcpy(a, tmp, n*sizeof(*tmp)); =20
>}

And with a guarantee that the malloc() always succeeds.

- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<XZ_YL.1523107$8_id.1501370@fx09.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31625&group=comp.arch#31625

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx09.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: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Newsgroups: comp.arch
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com> <2023Apr10.230314@mips.complang.tuwien.ac.at>
Lines: 40
Message-ID: <XZ_YL.1523107$8_id.1501370@fx09.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 10 Apr 2023 21:13:27 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 10 Apr 2023 21:13:27 GMT
X-Received-Bytes: 2548
 by: Scott Lurndal - Mon, 10 Apr 2023 21:13 UTC

anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>Michael S <already5chosen@yahoo.com> writes:
>>On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
>>> vadd(double *a, double *b, double c, size_t n)=20
>>> {=20
>>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
>>=20
>>> parameters are passed according to C rules, so a and b may or may=20
>>> not be pointers into the same array */
>>> }=20
>>>=20
>>
>>In Fortran, when a and b are pointers to the same array the result is=20
>>undefined.
>
>Thomas Koenig wrote something along the lines that there are standard
>Fortran idioms that generate aliases, and that if the compiler does
>not know that the operands in an array expression may overlap, the
>compiler has to generate code that takes potential overlap into
>account. What in particular is undefined IYO?
>
>>I suppose, you meant to produce the same result as slow_vadd() but efficie=
>>ntly.
>>slow_vadd(double *a, double *b, double c, size_t n)
>>{
>> double* tmp =3D malloc(n*sizeof(*tmp));
>> for (size_t i =3D 0; i < n; ++i)
>> tmp[i] =3D b[i] + c;
>> memcpy(a, tmp, n*sizeof(*tmp)); =20
>>}
>
>And with a guarantee that the malloc() always succeeds.

use alloca(3) instead of malloc(3) and you don't even
need to explicitly free it before slow_vadd returns.

Of course, the stack region would need to be sufficiently
large for 'n' (which is a per-process resource limit on linux, easily
adjusted - particularly on 64-bit systems were large virtual address
spaces are available).

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<fc58e8f9-df2f-4036-afda-dd5eec0f2a2an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31626&group=comp.arch#31626

  copy link   Newsgroups: comp.arch
X-Received: by 2002:ac8:5c87:0:b0:3bf:e265:9bf with SMTP id r7-20020ac85c87000000b003bfe26509bfmr132190qta.5.1681161522755;
Mon, 10 Apr 2023 14:18:42 -0700 (PDT)
X-Received: by 2002:a05:6870:73d3:b0:184:50a3:eb13 with SMTP id
a19-20020a05687073d300b0018450a3eb13mr286245oan.10.1681161521994; Mon, 10 Apr
2023 14:18:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 14:18:41 -0700 (PDT)
In-Reply-To: <k9jbe3Fl00gU1@mid.individual.net>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<k9jbe3Fl00gU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fc58e8f9-df2f-4036-afda-dd5eec0f2a2an@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: already5...@yahoo.com (Michael S)
Injection-Date: Mon, 10 Apr 2023 21:18:42 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3910
 by: Michael S - Mon, 10 Apr 2023 21:18 UTC

On Monday, April 10, 2023 at 11:55:36 PM UTC+3, Niklas Holsti wrote:
> On 2023-04-10 21:34, Michael S wrote:
> > On Monday, April 10, 2023 at 9:00:45 PM UTC+3, Anton Ertl wrote:
> >> David Brown <david...@hesbynett.no> writes:
> >>> On 10/04/2023 14:36, Anton Ertl wrote:
> >>>> David Brown <david...@hesbynett.no> writes:
> >>>>> On 04/04/2023 07:39, Anton Ertl wrote:
> >> [...]
> >>> #include <string.h>
> >>>
> >>> void * my_memmove(void * s1, const void * s2, size_t n) {
> >>> return memmove(s1, s2, n);
> >>> }
> >>>
> >>> There. Finished.
> >> Well done. Next:
> >>
> >> vadd(double *a, double *b, double c, size_t n)
> >> {
> >> /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
> >> parameters are passed according to C rules, so a and b may or may
> >> not be pointers into the same array */
> >> }
> >>
> >
> > In Fortran, when a and b are pointers to the same array the result is
> > undefined. I suppose, it's not what you had in mind.
> Are you sure about that?

No, I don't.
I just assumed that rules for subroutines apply to "implicit subrotines" like above.

> The F2018 Interpretation Document states, for
> an assignment of the form "variable = expr", that:
>
> "The execution of the assignment shall have the same effect as if the
> evaluation of expr and the evaluation of all expressions in variable
> occurred before any portion of the variable is defined by the assignment."
>
> (https://j3-fortran.org/doc/year/18/18-007r1.pdf, section 10.2.1.2)
>
> So the Fortran assignment should work as if the right-hand expr is
> evaluated into a temporary, and then the temporary is assigned (moved)
> to the left-hand variable.

If above applies to vector portion of the language then I am wrong.

The question is - how could we possibly cause a and b to refer to different
overlapping portions of the same array?
I can think about two ways - common block in the same compilation module,
in which case compiler knows exactly about relationships, and passing to
subroutine, in which case UB happens before we got to our line of interest.
But my knowledge of Fortran is really weak, possibly there are other ways.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<4ad1c031-33e1-42fc-aa66-7e3bb8659331n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31627&group=comp.arch#31627

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a37:43cf:0:b0:746:75f7:c8a2 with SMTP id q198-20020a3743cf000000b0074675f7c8a2mr2242720qka.4.1681161970948;
Mon, 10 Apr 2023 14:26:10 -0700 (PDT)
X-Received: by 2002:aca:2b14:0:b0:389:7d66:131c with SMTP id
i20-20020aca2b14000000b003897d66131cmr1747968oik.2.1681161970666; Mon, 10 Apr
2023 14:26:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 14:26:10 -0700 (PDT)
In-Reply-To: <XZ_YL.1523107$8_id.1501370@fx09.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at> <XZ_YL.1523107$8_id.1501370@fx09.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4ad1c031-33e1-42fc-aa66-7e3bb8659331n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: already5...@yahoo.com (Michael S)
Injection-Date: Mon, 10 Apr 2023 21:26:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Michael S - Mon, 10 Apr 2023 21:26 UTC

On Tuesday, April 11, 2023 at 12:13:31 AM UTC+3, Scott Lurndal wrote:
> an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >Michael S <already...@yahoo.com> writes:
> >>On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
> >>> vadd(double *a, double *b, double c, size_t n)=20
> >>> {=20
> >>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
> >>=20
> >>> parameters are passed according to C rules, so a and b may or may=20
> >>> not be pointers into the same array */
> >>> }=20
> >>>=20
> >>
> >>In Fortran, when a and b are pointers to the same array the result is=20
> >>undefined.
> >
> >Thomas Koenig wrote something along the lines that there are standard
> >Fortran idioms that generate aliases, and that if the compiler does
> >not know that the operands in an array expression may overlap, the
> >compiler has to generate code that takes potential overlap into
> >account. What in particular is undefined IYO?
> >
> >>I suppose, you meant to produce the same result as slow_vadd() but efficie=
> >>ntly.
> >>slow_vadd(double *a, double *b, double c, size_t n)
> >>{
> >> double* tmp =3D malloc(n*sizeof(*tmp));
> >> for (size_t i =3D 0; i < n; ++i)
> >> tmp[i] =3D b[i] + c;
> >> memcpy(a, tmp, n*sizeof(*tmp)); =20
> >>}
> >
> >And with a guarantee that the malloc() always succeeds.
> use alloca(3) instead of malloc(3) and you don't even
> need to explicitly free it before slow_vadd returns.
>
> Of course, the stack region would need to be sufficiently
> large for 'n' (which is a per-process resource limit on linux, easily
> adjusted - particularly on 64-bit systems were large virtual address
> spaces are available).

With C99 we don't even have to use alloca() which IIRC is not the part of
the standard, although widely available in practice.
We can express ourselves cleaner with VLA.
However the topic is about C23 and in C23 VLA support is optional.
Anyway, that's off topic of off topic.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<be33ee28-9965-4718-a55e-210b913b239an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31628&group=comp.arch#31628

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:6214:4a5e:b0:5ee:e59c:4de0 with SMTP id ph30-20020a0562144a5e00b005eee59c4de0mr79600qvb.4.1681162818698;
Mon, 10 Apr 2023 14:40:18 -0700 (PDT)
X-Received: by 2002:a05:6870:d1cb:b0:17a:eab4:b67f with SMTP id
b11-20020a056870d1cb00b0017aeab4b67fmr4028788oac.10.1681162818473; Mon, 10
Apr 2023 14:40:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!newsfeed.hasname.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 14:40:18 -0700 (PDT)
In-Reply-To: <2023Apr10.230314@mips.complang.tuwien.ac.at>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:19d4:37c7:ca91:90dc
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <be33ee28-9965-4718-a55e-210b913b239an@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: already5...@yahoo.com (Michael S)
Injection-Date: Mon, 10 Apr 2023 21:40:18 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3995
 by: Michael S - Mon, 10 Apr 2023 21:40 UTC

On Tuesday, April 11, 2023 at 12:10:14 AM UTC+3, Anton Ertl wrote:
> Michael S <already...@yahoo.com> writes:
> >On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
> >> vadd(double *a, double *b, double c, size_t n)=20
> >> {=20
> >> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
> >=20
> >> parameters are passed according to C rules, so a and b may or may=20
> >> not be pointers into the same array */
> >> }=20
> >>=20
> >
> >In Fortran, when a and b are pointers to the same array the result is=20
> >undefined.
>
> Thomas Koenig wrote something along the lines that there are standard
> Fortran idioms that generate aliases, and that if the compiler does
> not know that the operands in an array expression may overlap, the
> compiler has to generate code that takes potential overlap into
> account. What in particular is undefined IYO?
>

I am pretty sure that it is opposite way around, at least for most common
form of Fortran parameters passing, i.e. passing array - compiler assumes
no aliasing unless it knows for sure that aliasing presents.
For a newer less common form of parameter passing, i.e. pointers, the rules
are similar to 'C'. But I don't know if syntax like in your example is legal for
both for arrays and for pointers.

As discussed above, your example is not a subroutine call, so let's say that
I am no more sure in anything.

May be, both of us should not make statements about Fortran, because both
of us lack the knowledge. Otherwise we look too much like few other c.a. posters
that like to discuss Ada without ever reading a single Ada reference book about it
or ever writing a single Ada program.

> >I suppose, you meant to produce the same result as slow_vadd() but efficie> >ntly.
> >slow_vadd(double *a, double *b, double c, size_t n)
> >{
> > double* tmp =3D malloc(n*sizeof(*tmp));
> > for (size_t i =3D 0; i < n; ++i)
> > tmp[i] =3D b[i] + c;
> > memcpy(a, tmp, n*sizeof(*tmp)); =20
> >}
>
> And with a guarantee that the malloc() always succeeds.
> - anton
> --
> 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
> Mitch Alsup, <c17fcd89-f024-40e7...@googlegroups.com>

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u12jij$2gd1b$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31629&group=comp.arch#31629

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: cr88...@gmail.com (BGB)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Mon, 10 Apr 2023 22:18:36 -0500
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <u12jij$2gd1b$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at>
<XZ_YL.1523107$8_id.1501370@fx09.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 11 Apr 2023 03:18:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a6a4710ef1de506fa195e36e24a90235";
logging-data="2634795"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DoZW934BZ0B9zxc50Tnba"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.9.1
Cancel-Lock: sha1:9/w3QbrUzqrd8bCDQK9M4fI89mI=
In-Reply-To: <XZ_YL.1523107$8_id.1501370@fx09.iad>
Content-Language: en-US
 by: BGB - Tue, 11 Apr 2023 03:18 UTC

On 4/10/2023 4:13 PM, Scott Lurndal wrote:
> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>> Michael S <already5chosen@yahoo.com> writes:
>>> On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
>>>> vadd(double *a, double *b, double c, size_t n)=20
>>>> {=20
>>>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
>>> =20
>>>> parameters are passed according to C rules, so a and b may or may=20
>>>> not be pointers into the same array */
>>>> }=20
>>>> =20
>>>
>>> In Fortran, when a and b are pointers to the same array the result is=20
>>> undefined.
>>
>> Thomas Koenig wrote something along the lines that there are standard
>> Fortran idioms that generate aliases, and that if the compiler does
>> not know that the operands in an array expression may overlap, the
>> compiler has to generate code that takes potential overlap into
>> account. What in particular is undefined IYO?
>>
>>> I suppose, you meant to produce the same result as slow_vadd() but efficie=
>>> ntly.
>>> slow_vadd(double *a, double *b, double c, size_t n)
>>> {
>>> double* tmp =3D malloc(n*sizeof(*tmp));
>>> for (size_t i =3D 0; i < n; ++i)
>>> tmp[i] =3D b[i] + c;
>>> memcpy(a, tmp, n*sizeof(*tmp)); =20
>>> }
>>
>> And with a guarantee that the malloc() always succeeds.
>
> use alloca(3) instead of malloc(3) and you don't even
> need to explicitly free it before slow_vadd returns.
>
> Of course, the stack region would need to be sufficiently
> large for 'n' (which is a per-process resource limit on linux, easily
> adjusted - particularly on 64-bit systems were large virtual address
> spaces are available).

FWIW, some of us implemented "alloca()" as a wrapper on top of
"malloc()" with an "automatically free when function returns" mechanism
(alloca adds the object to an invisible linked list, and the compiler
then inserts a runtime call to a function that frees everything in the
list).

This mechanism was also used for VLAs and for arrays exceeding a certain
maximum cut-off size, ...

This being partly because:
Variable size stack frames are evil;
If one excludes large allocations from the stack (including large
arrays), one may find that 128k of stack space is sufficient for most
programs.

Though, this is more relevant for NOMMU systems, as with an MMU, burning
1MB (or several MB) of virtual address space on a stack is less of an issue.

But, it is maybe a waste to spend 1MB on the stack for a program that
can easily fit into 64k or 128k.

However, it is a tradeoff, because if too many of the arrays that might
have otherwise been put onto the stack are put into the heap, this may
have an adverse effect on performance. So, the limit might still end up
being in kB territory (and as such, only a few levels with big arrays,
or a modestly recursive program with large stack frames, may still end
up blowing out the stack).

....

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u12rt2$3mpv7$1@newsreader4.netcologne.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31631&group=comp.arch#31631

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.2a0a-a540-1f8e-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de!not-for-mail
From: tkoe...@netcologne.de (Thomas Koenig)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Tue, 11 Apr 2023 05:40:50 -0000 (UTC)
Organization: news.netcologne.de
Distribution: world
Message-ID: <u12rt2$3mpv7$1@newsreader4.netcologne.de>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at>
<u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<k9jbe3Fl00gU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 11 Apr 2023 05:40:50 -0000 (UTC)
Injection-Info: newsreader4.netcologne.de; posting-host="2a0a-a540-1f8e-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de:2a0a:a540:1f8e:0:7285:c2ff:fe6c:992d";
logging-data="3893223"; mail-complaints-to="abuse@netcologne.de"
User-Agent: slrn/1.0.3 (Linux)
 by: Thomas Koenig - Tue, 11 Apr 2023 05:40 UTC

Niklas Holsti <niklas.holsti@tidorum.invalid> schrieb:
> On 2023-04-10 21:34, Michael S wrote:
>> On Monday, April 10, 2023 at 9:00:45 PM UTC+3, Anton Ertl wrote:
>>> David Brown <david...@hesbynett.no> writes:
>>>> On 10/04/2023 14:36, Anton Ertl wrote:
>>>>> David Brown <david...@hesbynett.no> writes:
>>>>>> On 04/04/2023 07:39, Anton Ertl wrote:
>>> [...]
>>>> #include <string.h>
>>>>
>>>> void * my_memmove(void * s1, const void * s2, size_t n) {
>>>> return memmove(s1, s2, n);
>>>> }
>>>>
>>>> There. Finished.
>>> Well done. Next:
>>>
>>> vadd(double *a, double *b, double c, size_t n)
>>> {
>>> /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
>>> parameters are passed according to C rules, so a and b may or may
>>> not be pointers into the same array */
>>> }

The exact equivalent is impossible to write, because Fortran does not
support pointer arithmetic.

What you wrote is what f2c or nagfor (which compile Fortran to C) would generate
for

SUBROUTINE VADD (A, B, C, N)
INTEGER N
DOUBLE PRECISION A(N), B(N), C

but this is an implementation detail, other argument passing conventions
are possible.

And if they overlap, the program is not valid Fortran, which is
I assume what MichaelS meant when he wrote

>> In Fortran, when a and b are pointers to the same array the result is
>> undefined. I suppose, it's not what you had in mind.

so the task above is actually ill-formed, and does not constitute an
argument for or against C's suitability as a systems level language.

> Are you sure about that? The F2018 Interpretation Document states, for
> an assignment of the form "variable = expr", that:
>
> "The execution of the assignment shall have the same effect as if the
> evaluation of expr and the evaluation of all expressions in variable
> occurred before any portion of the variable is defined by the assignment."
>
> (https://j3-fortran.org/doc/year/18/18-007r1.pdf, section 10.2.1.2)

That is correct. If arguments are passed separately, like in
Anton's example, then the compiler can determine that there is
no overlap, and procede with a straightforward loop.

> So the Fortran assignment should work as if the right-hand expr is
> evaluated into a temporary, and then the temporary is assigned (moved)
> to the left-hand variable.

Correct. And this is what compilers actually do. For example,
the code

subroutine foo(a,n,i,j,m)
real, dimension(n) :: a
a(i:i+m) = a (j:j+m) + 42.
end subroutine foo

which realizes such an overlap is actually translated to (part of
gfortran's -fdump-tree-original output)

D.3912 = (void * restrict) __builtin_malloc (D.3910 ? 1 : MAX_EXPR <(unsigned long) (D.3911 * 4), 1>);
D.3913 = D.3912;
atmp.2.data = D.3913;
atmp.2.offset = 0;
D.3914 = NON_LVALUE_EXPR <D.3882>;
D.3915 = NON_LVALUE_EXPR <D.3884>;
{
integer(kind=8) S.3;

S.3 = 0;
while (1)
{
if (S.3 > D.3886) goto L.1;
(*(real(kind=4)[0:] * restrict) atmp.2.data)[S.3] = (*a)[(S.3 + D.3914) + -1] + 4.2e+1;
S.3 = S.3 + 1;
}
L.1:;
S.3 = 0;
while (1)
{
if (S.3 > D.3886) goto L.2;
(*a)[(S.3 + D.3915) + -1] = (*(real(kind=4)[0:] * restrict) atmp.2.data)[S.3];
S.3 = S.3 + 1;
}
L.2:;
}
__builtin_free ((void *) atmp.2.data);
}

so this uses a temporary (same with nagfor). This particular case
is a known missed optimization because conditional loop reversal
would work here, see https://gcc.gnu.org/bugzilla/PR108665 .

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u12s33$3mpv7$2@newsreader4.netcologne.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31632&group=comp.arch#31632

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!.POSTED.2a0a-a540-1f8e-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de!not-for-mail
From: tkoe...@netcologne.de (Thomas Koenig)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Tue, 11 Apr 2023 05:44:03 -0000 (UTC)
Organization: news.netcologne.de
Distribution: world
Message-ID: <u12s33$3mpv7$2@newsreader4.netcologne.de>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at>
<u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at>
Injection-Date: Tue, 11 Apr 2023 05:44:03 -0000 (UTC)
Injection-Info: newsreader4.netcologne.de; posting-host="2a0a-a540-1f8e-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de:2a0a:a540:1f8e:0:7285:c2ff:fe6c:992d";
logging-data="3893223"; mail-complaints-to="abuse@netcologne.de"
User-Agent: slrn/1.0.3 (Linux)
 by: Thomas Koenig - Tue, 11 Apr 2023 05:44 UTC

Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb:
> Michael S <already5chosen@yahoo.com> writes:
>>On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
>>> vadd(double *a, double *b, double c, size_t n)=20
>>> {=20
>>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
>>=20
>>> parameters are passed according to C rules, so a and b may or may=20
>>> not be pointers into the same array */
>>> }=20
>>>=20
>>
>>In Fortran, when a and b are pointers to the same array the result is=20
>>undefined.
>
> Thomas Koenig wrote something along the lines that there are standard
> Fortran idioms that generate aliases, and that if the compiler does
> not know that the operands in an array expression may overlap, the
> compiler has to generate code that takes potential overlap into
> account. What in particular is undefined IYO?

It's not related to normal argument passing.

What you may be referring to is when arrays have to be packed.
Assume

call (a(1:n:2),size(a(1:n:2))

subroutine foo(a,n)
real :: a(n)

where two different passing conventions are mixed. Here, foo has to
receive a contiguous argument, and is passed a non-contiguous one.
For this, packing (and unpacking, because INTENT is not specified)
is necessary.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<f662051a-65d0-4642-8b2e-0d25dbc42ba5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31633&group=comp.arch#31633

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:620a:1903:b0:746:98fc:c6cf with SMTP id bj3-20020a05620a190300b0074698fcc6cfmr3380424qkb.3.1681196161997;
Mon, 10 Apr 2023 23:56:01 -0700 (PDT)
X-Received: by 2002:a05:6808:639a:b0:384:893:a924 with SMTP id
ec26-20020a056808639a00b003840893a924mr458124oib.3.1681196161752; Mon, 10 Apr
2023 23:56:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.niel.me!glou.org!news.glou.org!fdn.fr!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Mon, 10 Apr 2023 23:56:01 -0700 (PDT)
In-Reply-To: <u12jij$2gd1b$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2607:fea8:1dde:6a00:89b7:4274:570d:53ab;
posting-account=QId4bgoAAABV4s50talpu-qMcPp519Eb
NNTP-Posting-Host: 2607:fea8:1dde:6a00:89b7:4274:570d:53ab
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at> <XZ_YL.1523107$8_id.1501370@fx09.iad>
<u12jij$2gd1b$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f662051a-65d0-4642-8b2e-0d25dbc42ba5n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: robfi...@gmail.com (robf...@gmail.com)
Injection-Date: Tue, 11 Apr 2023 06:56:01 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: robf...@gmail.com - Tue, 11 Apr 2023 06:56 UTC

On Monday, April 10, 2023 at 11:18:47 PM UTC-4, BGB wrote:
> On 4/10/2023 4:13 PM, Scott Lurndal wrote:
> > an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >> Michael S <already...@yahoo.com> writes:
> >>> On Monday, April 10, 2023 at 9:00:45=E2=80=AFPM UTC+3, Anton Ertl wrote:
> >>>> vadd(double *a, double *b, double c, size_t n)=20
> >>>> {=20
> >>>> /* write the equivalent of the Fortran a(1:n)=3Db(1:n)+c; note that the=
> >>> =20
> >>>> parameters are passed according to C rules, so a and b may or may=20
> >>>> not be pointers into the same array */
> >>>> }=20
> >>>> =20
> >>>
> >>> In Fortran, when a and b are pointers to the same array the result is=20
> >>> undefined.
> >>
> >> Thomas Koenig wrote something along the lines that there are standard
> >> Fortran idioms that generate aliases, and that if the compiler does
> >> not know that the operands in an array expression may overlap, the
> >> compiler has to generate code that takes potential overlap into
> >> account. What in particular is undefined IYO?
> >>
> >>> I suppose, you meant to produce the same result as slow_vadd() but efficie=
> >>> ntly.
> >>> slow_vadd(double *a, double *b, double c, size_t n)
> >>> {
> >>> double* tmp =3D malloc(n*sizeof(*tmp));
> >>> for (size_t i =3D 0; i < n; ++i)
> >>> tmp[i] =3D b[i] + c;
> >>> memcpy(a, tmp, n*sizeof(*tmp)); =20
> >>> }
> >>
> >> And with a guarantee that the malloc() always succeeds.
> >
> > use alloca(3) instead of malloc(3) and you don't even
> > need to explicitly free it before slow_vadd returns.
> >
> > Of course, the stack region would need to be sufficiently
> > large for 'n' (which is a per-process resource limit on linux, easily
> > adjusted - particularly on 64-bit systems were large virtual address
> > spaces are available).
> FWIW, some of us implemented "alloca()" as a wrapper on top of
> "malloc()" with an "automatically free when function returns" mechanism
> (alloca adds the object to an invisible linked list, and the compiler
> then inserts a runtime call to a function that frees everything in the
> list).
>
> This mechanism was also used for VLAs and for arrays exceeding a certain
> maximum cut-off size, ...
>
>
> This being partly because:
> Variable size stack frames are evil;
> If one excludes large allocations from the stack (including large
> arrays), one may find that 128k of stack space is sufficient for most
> programs.

>
> Though, this is more relevant for NOMMU systems, as with an MMU, burning
> 1MB (or several MB) of virtual address space on a stack is less of an issue.
>
> But, it is maybe a waste to spend 1MB on the stack for a program that
> can easily fit into 64k or 128k.
>
>
> However, it is a tradeoff, because if too many of the arrays that might
> have otherwise been put onto the stack are put into the heap, this may
> have an adverse effect on performance. So, the limit might still end up
> being in kB territory (and as such, only a few levels with big arrays,
> or a modestly recursive program with large stack frames, may still end
> up blowing out the stack).
>
> ...
I agree.

I have seen a very large piece of software using only a few kB of stack.
It went 68 levels deep on stack.
A16kB memory page may be enough.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u136s9$2incc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31635&group=comp.arch#31635

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Tue, 11 Apr 2023 10:48:09 +0200
Organization: A noiseless patient Spider
Lines: 158
Message-ID: <u136s9$2incc$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 11 Apr 2023 08:48:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="22b76096fb402042beebc8c1f2bbd923";
logging-data="2710924"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nbE/xLVy0Nhb+h+Wy/rg/zKpV+p9DLMU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:KMn2wihJcB14d3g02IexA3n6+cQ=
Content-Language: en-GB
In-Reply-To: <2023Apr10.175439@mips.complang.tuwien.ac.at>
 by: David Brown - Tue, 11 Apr 2023 08:48 UTC

On 10/04/2023 17:54, Anton Ertl wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 10/04/2023 14:36, Anton Ertl wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>> On 04/04/2023 07:39, Anton Ertl wrote:
> [...]
>> #include <string.h>
>>
>> void * my_memmove(void * s1, const void * s2, size_t n) {
>> return memmove(s1, s2, n);
>> }
>>
>> There. Finished.
>
> Well done. Next:
>
> vadd(double *a, double *b, double c, size_t n)
> {
> /* write the equivalent of the Fortran a(1:n)=b(1:n)+c; note that the
> parameters are passed according to C rules, so a and b may or may
> not be pointers into the same array */
> }

I don't know much about Fortran, and therefore don't know the details of
the semantics. I believe in Fortran there is always the assumption that
such pointers are completely independent (that's one of the reasons why
some things can be implemented more efficiently in Fortran than C, at
least until "restrict" was added in C99). But I assume you mean it is
quite possible for a(1:n) and b(1:n) to overlap, otherwise your question
would be pointless.

The answer is just to write non-portable code - cast your pointers to
uintptr_t and do the comparison there. (I'd first question the
requirements of the function, however.)

>
>> As with any language, there are always things
>> that people would like to do in the language, but can't - the
>> alternative is that people will complain that the language (and/or
>> standard library) is too big.
>
> C already includes pointer comparison.

It does, yes - but like everything else in the language (and in all
other well-specified and standardised languages), it works according to
the definition in the standard. It does not work according to whatever
idea pops into some programmer's head - no matter how rational or useful
that idea may seem.

> Making it undefined for
> certain cases increases the things a programmer has to remember in
> order to use C, i.e., the language the programmer experiences becomes
> bigger.

It's hardly rocket science. If you want to use a programming language,
you have to learn it. All programming languages let you express things
that don't make sense. Some try harder than others to force you to
write correct code - others, like C, trust the programmer to know what
he/she is doing.

>
>> (See C++ for evidence of that!)
>
> Does C++ define pointer comparison? I don't think so. So C++ bigness
> is, at best, an argument for my position.

The point is that if you keep adding features to a language, people will
complain that there are too many features.

>
>> The difference between us is not that /you/ think C is broken and /I/
>> think it is perfect - I don't think it is perfect. It is not even just
>> a matter of differing in which things we would want to change. It is
>> that I look at C and see a language which is quite good, but not quite
>> what I'd like - thus I learn about how it works and decide I can use it
>> even though it is sub-optimal for /me/. You, apparently, see the
>> aspects of C that you don't like and declare that the language is broken
>> and compilers are broken for the way they handle the aspects you dislike.
>
> No, the difference is that I see production C programs that are
> compiled as intended by some compilers, and if another release of the
> compiler miscompiles the program, I see this as bug in the compiler.

So if one program works by luck rather than design, all future compilers
and language standards have to enshrine that luck as though it were a
rule? What a totally bizarre concept. Seriously - what you write is
absolute piddle. And I don't think you really mean it. I think you
mean that /some/ such code should be supported for ever more - such as
the code /you/ write. You probably also think it should apply to some
of the software you use - but not code /you/ deem to be badly written or
not particularly important. Compiler writers should read /your/ mind
and understand /your/ personal, arbitrary and vague idea of where to
draw the line between "production C programs" and "bad C code", and
support these "production C programs" for all eternity.

Honestly, I /do/ understand wanting such as coding utopia. What I
completely fail to understand is how anyone would think it is remotely
realistic - and how you can fail to understand it is a matter of one
person believing that his own personal ideas are somehow absolute and
objective, and should apply to everyone.

Programming is based on contracts. The programmer promises to write
code according to the rules of the language. The compiler promises to
implement any code written according to the rules. If the programmer
breaks their side of the bargain, the compiler is under no obligation to
read their mind and generate object code to match the programmer's
unwritten intentions.

It is /irrelevant/ what the programmer thinks the rules should be - even
if they are good, useful and practical ideas. It is only the rules
specified in the standards and any other documentation (such as compiler
manuals) that are relevant.

Of course real-world tools go way beyond this minimum. They work hard
to provide help to developers - such as by static analysis, run-time
checkers, and other tools to help you get things right. Some compilers
give extra guarantees or features, making the programmer's life easier
at the expense of non-portable code. (There's nothing wrong with
writing non-portable code, if that's appropriate for the task in hand.)

But in the end, regardless of the features of the tool, if the
programmer does not know the relevant parts of the language they are
writing, that is /their/ problem, not the compiler's. If they know the
language details and still write incorrect code, that is /their/ problem.

> Some people claim that the program is buggy and has always been, and
> base this claim on certain parts of the C standard. Of course, they
> prefer to ignore other parts of the C standard, such as the parts
> about conforming and strictly conforming C programs.
>

>
>>> Consider a sane system
>>> like a PDP-11, and you have an array
>>>
>>> char c[40000U];
>>> char *p=c;
>>> char *q=&c[39999];
>>>
>>> The C standard defines what q<p means, but does not define what p-q
>>> means, does it?
>>>
>>
>> The answer is implementation-dependent, but could certainly be "yes".
>
> Is this another of your dreamed-up scenarios, one where ptrdiff_t on a
> PDP-11 is larger than 16 bits? Or can you name an implementation
> where p-q is defined for the case above?
>

From C99 until C23, ptrdiff_t was minimum 17 bits in size. (Prior to
C99, the minimum size was not specified.) Maybe the PDP-11 never had a
compliant C99 compiler - it's not a system I have ever used personally.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<ljdZL.420088$5S78.182599@fx48.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31636&group=comp.arch#31636

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx48.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: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Newsgroups: comp.arch
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com> <2023Apr10.230314@mips.complang.tuwien.ac.at> <XZ_YL.1523107$8_id.1501370@fx09.iad> <u12jij$2gd1b$1@dont-email.me>
Lines: 13
Message-ID: <ljdZL.420088$5S78.182599@fx48.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 11 Apr 2023 13:32:01 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 11 Apr 2023 13:32:01 GMT
X-Received-Bytes: 1457
 by: Scott Lurndal - Tue, 11 Apr 2023 13:32 UTC

BGB <cr88192@gmail.com> writes:
>On 4/10/2023 4:13 PM, Scott Lurndal wrote:
>> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>> Michael S <already5chosen@yahoo.com> writes:

>This being partly because:
>Variable size stack frames are evil;
>If one excludes large allocations from the stack (including large
>arrays), one may find that 128k of stack space is sufficient for most
>programs.

It's not 1989 anymore.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<74afe76f-5b24-4ad9-8a0d-798af5fb13f2n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31637&group=comp.arch#31637

  copy link   Newsgroups: comp.arch
X-Received: by 2002:ac8:7d16:0:b0:3d2:8d87:6710 with SMTP id g22-20020ac87d16000000b003d28d876710mr4567479qtb.12.1681223792986;
Tue, 11 Apr 2023 07:36:32 -0700 (PDT)
X-Received: by 2002:a05:6808:1996:b0:383:fad3:d19 with SMTP id
bj22-20020a056808199600b00383fad30d19mr4164693oib.9.1681223792717; Tue, 11
Apr 2023 07:36:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Tue, 11 Apr 2023 07:36:32 -0700 (PDT)
In-Reply-To: <ljdZL.420088$5S78.182599@fx48.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=99.251.79.92; posting-account=QId4bgoAAABV4s50talpu-qMcPp519Eb
NNTP-Posting-Host: 99.251.79.92
References: <u0f6gf$32irr$1@dont-email.me> <a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at> <XZ_YL.1523107$8_id.1501370@fx09.iad>
<u12jij$2gd1b$1@dont-email.me> <ljdZL.420088$5S78.182599@fx48.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <74afe76f-5b24-4ad9-8a0d-798af5fb13f2n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: robfi...@gmail.com (robf...@gmail.com)
Injection-Date: Tue, 11 Apr 2023 14:36:32 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3337
 by: robf...@gmail.com - Tue, 11 Apr 2023 14:36 UTC

On Tuesday, April 11, 2023 at 9:32:07 AM UTC-4, Scott Lurndal wrote:
> BGB <cr8...@gmail.com> writes:
> >On 4/10/2023 4:13 PM, Scott Lurndal wrote:
> >> an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >>> Michael S <already...@yahoo.com> writes:
>
> >This being partly because:
> >Variable size stack frames are evil;
> >If one excludes large allocations from the stack (including large
> >arrays), one may find that 128k of stack space is sufficient for most
> >programs.
> It's not 1989 anymore.

I may be a bit out of date here, but, I think some rules still apply.
Passing large objects like arrays as parameters on the stack is not
generally a good idea. It may lead to copying lots of memory around
uselessly. Objects can be allocated on the stack but usually they are
allocated on the heap especially if they are large, so they can be
managed. Allocating them on the stack forces them to be deallocated
at method return.
I usually try and statically allocate any buffers that are known to be
large and can be of a fixed size. This causes the program to fail right
away at startup, instead of in the middle after processing tons of
records. It also frees the program from doing memory management
work while it is running, and moves the work to startup and shutdown.
But a lot depends on the purpose and type of application.
Having said my two cents, I do use a lot more stack space in programs
that I used to, simply because the space is available. It can reduce
development time, not thinking through every algorithm. Development
time cost. I used to do a lot of high-speed development with
“throw-away” apps.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u13t7h$2l6fc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31638&group=comp.arch#31638

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Tue, 11 Apr 2023 17:09:36 +0200
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <u13t7h$2l6fc$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at>
<XZ_YL.1523107$8_id.1501370@fx09.iad> <u12jij$2gd1b$1@dont-email.me>
<ljdZL.420088$5S78.182599@fx48.iad>
<74afe76f-5b24-4ad9-8a0d-798af5fb13f2n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 11 Apr 2023 15:09:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="22b76096fb402042beebc8c1f2bbd923";
logging-data="2791916"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+m6GxV3h3EASU2dKf1v2Fh8WCwad/UPj0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:UCMmLZ/EBJR5zZILE2OE7/wuN5A=
In-Reply-To: <74afe76f-5b24-4ad9-8a0d-798af5fb13f2n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Tue, 11 Apr 2023 15:09 UTC

On 11/04/2023 16:36, robf...@gmail.com wrote:
> On Tuesday, April 11, 2023 at 9:32:07 AM UTC-4, Scott Lurndal wrote:
>> BGB <cr8...@gmail.com> writes:
>>> On 4/10/2023 4:13 PM, Scott Lurndal wrote:
>>>> an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>>>> Michael S <already...@yahoo.com> writes:
>>
>>> This being partly because:
>>> Variable size stack frames are evil;
>>> If one excludes large allocations from the stack (including large
>>> arrays), one may find that 128k of stack space is sufficient for most
>>> programs.
>> It's not 1989 anymore.
>
> I may be a bit out of date here, but, I think some rules still apply.
> Passing large objects like arrays as parameters on the stack is not
> generally a good idea. It may lead to copying lots of memory around
> uselessly.

This is jumbling several different concepts which may or may not be an
issue.

Useless copying of lots of memory is never good for efficiency, but
that's completely orthogonal to putting things on the stack rather than
the heap (or statically allocating them). You can put large arrays on
the stack without ever making unnecessary copies of them.

Variable stack frames are also orthogonal to the size of the stack
frames. Variable size stack frames are harder to analyse to be sure of
correctness, but can be more efficient (in memory use, and especially
cache use) than larger fixed size frames.

Stacks are sometimes limited in size, depending on the target system and
OS, and sometimes they grow automatically (limited only by actual memory
available). They are less flexible for allocation than the heap as you
have to deallocate in strict reverse order, but they can be much faster
to use.

> Objects can be allocated on the stack but usually they are
> allocated on the heap especially if they are large, so they can be
> managed. Allocating them on the stack forces them to be deallocated
> at method return.

In my world, small-systems embedded programming, the heap should be
avoided unless there is no other way to handle the task in hand.
Putting objects - even big ones and variable sized ones - on the stack
is fine, as long as you are sure of the maximum sizes and the amount of
stack you have available. Requirements vary for different kinds of
programming.

> I usually try and statically allocate any buffers that are known to be
> large and can be of a fixed size.

I do too.

> This causes the program to fail right
> away at startup, instead of in the middle after processing tons of
> records. It also frees the program from doing memory management
> work while it is running, and moves the work to startup and shutdown.
> But a lot depends on the purpose and type of application.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<dYeZL.244208$SdR7.64232@fx04.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31639&group=comp.arch#31639

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!news2.arglkargh.de!news.in-chemnitz.de!3.eu.feeder.erje.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx04.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: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Newsgroups: comp.arch
References: <u0f6gf$32irr$1@dont-email.me> <2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at> <5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com> <2023Apr10.230314@mips.complang.tuwien.ac.at> <XZ_YL.1523107$8_id.1501370@fx09.iad> <u12jij$2gd1b$1@dont-email.me> <ljdZL.420088$5S78.182599@fx48.iad> <c7ecba88-b16a-4bad-ae81-9cb04ccb8177n@googlegroups.com>
Lines: 22
Message-ID: <dYeZL.244208$SdR7.64232@fx04.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 11 Apr 2023 15:23:53 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 11 Apr 2023 15:23:53 GMT
X-Received-Bytes: 1935
 by: Scott Lurndal - Tue, 11 Apr 2023 15:23 UTC

MitchAlsup <MitchAlsup@aol.com> writes:
>On Tuesday, April 11, 2023 at 8:32:07=E2=80=AFAM UTC-5, Scott Lurndal wrote=
>:
>> BGB <cr8...@gmail.com> writes:=20
>> >On 4/10/2023 4:13 PM, Scott Lurndal wrote:=20
>> >> an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:=20
>> >>> Michael S <already...@yahoo.com> writes:=20
>>=20
>> >This being partly because:=20
>> >Variable size stack frames are evil;=20
>> >If one excludes large allocations from the stack (including large=20
>> >arrays), one may find that 128k of stack space is sufficient for most=20
>> >programs.
><
>> It's not 1989 anymore.
><
>Algol60 had no where other than the stack to put dynamically allocated=20
>arrays.......

The burroughs systems were stack based architectures. That said, the
array "descriptor" was on the stack. The array itself would be allocated
elsewhere in memory.

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<u1454q$2mbg6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31640&group=comp.arch#31640

  copy link   Newsgroups: comp.arch
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From: cr88...@gmail.com (BGB)
Newsgroups: comp.arch
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
Date: Tue, 11 Apr 2023 12:24:34 -0500
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <u1454q$2mbg6$1@dont-email.me>
References: <u0f6gf$32irr$1@dont-email.me>
<a533d5c4-8dca-44eb-ba43-bf38ab7f9f34n@googlegroups.com>
<2023Apr4.073909@mips.complang.tuwien.ac.at> <u10pbe$259vd$1@dont-email.me>
<2023Apr10.143646@mips.complang.tuwien.ac.at> <u118f4$27ckk$1@dont-email.me>
<2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com>
<2023Apr10.230314@mips.complang.tuwien.ac.at>
<XZ_YL.1523107$8_id.1501370@fx09.iad> <u12jij$2gd1b$1@dont-email.me>
<ljdZL.420088$5S78.182599@fx48.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 11 Apr 2023 17:24:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a6a4710ef1de506fa195e36e24a90235";
logging-data="2829830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WxNyxExXJ+4bBZpaunJYN"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.9.1
Cancel-Lock: sha1:klakiJM4k5nWDIbJFxfxSBjaFF0=
In-Reply-To: <ljdZL.420088$5S78.182599@fx48.iad>
Content-Language: en-US
 by: BGB - Tue, 11 Apr 2023 17:24 UTC

On 4/11/2023 8:32 AM, Scott Lurndal wrote:
> BGB <cr88192@gmail.com> writes:
>> On 4/10/2023 4:13 PM, Scott Lurndal wrote:
>>> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>>> Michael S <already5chosen@yahoo.com> writes:
>
>> This being partly because:
>> Variable size stack frames are evil;
>> If one excludes large allocations from the stack (including large
>> arrays), one may find that 128k of stack space is sufficient for most
>> programs.
>
> It's not 1989 anymore.
>

?...

AFAIK, back then stack sizes then were usually smaller, say, 8K or 16K?...

Where, say:
CS, program;
DS = SS, global variables and stack.
Also heap for "small" or "tiny" model.
ES, stomped for sake of "far" pointers.

Comparably, 128K is massive...

I have tested, and a lot of stuff does not work with a 16K or 32K stack,
whereas 64K is borderline (say, Doom works, but Quake overflows the stack).

Meanwhile, 128K usually works, and 256K if one "wants to be safe".

GLQuake didn't work at first, but this was because they had put a large
array (256x256 32-bit) the stack for upscaling non-power-of-2 textures,
which immediately blew out the stack.
Had turned this into something like:
static int *texbuf=NULL;
if(!texbuf)
texbuf=malloc(256*256*4);

In this case, doing an "alloca()" for every call to
"GL_UploadTexture32()" (or something to this effect) would be not ideal.
This array is also "a little on the large side" for ".bss" as well.

Windows typically uses 1MB for the stack, but this seems overkill IME,
and also Windows always operates with virtual memory enabled.

But, then again, as I have noted, in some of the programs I have tested
there are also non-zero amounts of code using K&R style declaration
syntax, eg:
foo(p, x, y) int x, y; char *p; {
...
}

Thus far, my TestKern thing is often operating with the MMU disabled
(unless a pagefile is detected). In this case, only physical RAM is
available, which leaves things a little more cramped.

Say, 128MB of physical-mapped RAM doesn't go very far if one is
allocating 1MB for stack buffers and similar.

Note that my PBO ABI was designed for use in NOMMU configurations (along
with putting multiple logical processes in a shared virtual address
space), so works OK here.

I wasn't using the pagefile in the "actual hardware" tests thus far, as
it seems there is an issue with my SDcard code and writing data to the
SDcard did not seem to be working correctly.

Had tried correcting it (apparently my code for writing to the SDcard
wasn't quite right), but seems to not have worked...

Though, I am getting conflicting information on whether the CMD24 "Write
Block" operation needs a CRC. A PDF from SanDisk mentions a CRC
following the data for a CMD24, it is possible I may need to figure out
this CRC for data to "actually get written" to the SDcard? ...

A lot of code online seemingly just sends two 0xFF bytes (following the
main 512 bytes of block data), but, as noted, this did not seem to be
working in my case (with the SDcard effectively behaving almost like it
was Read-Only).

Everything works in simulation, but as noted, my SDcard mockup doesn't
check for or generate CRCs.

....

Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

<a85d3e0c-e0fd-4569-b7c9-2779ab298849n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=31641&group=comp.arch#31641

  copy link   Newsgroups: comp.arch
X-Received: by 2002:a05:622a:1aa7:b0:3e0:ed1:9f72 with SMTP id s39-20020a05622a1aa700b003e00ed19f72mr4829158qtc.7.1681235747742;
Tue, 11 Apr 2023 10:55:47 -0700 (PDT)
X-Received: by 2002:a9d:758f:0:b0:6a3:e4ac:efb8 with SMTP id
s15-20020a9d758f000000b006a3e4acefb8mr2898413otk.4.1681235747525; Tue, 11 Apr
2023 10:55:47 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch
Date: Tue, 11 Apr 2023 10:55:47 -0700 (PDT)
In-Reply-To: <EUgZL.2295552$vBI8.1111497@fx15.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=2600:1700:291:29f0:15c8:5e5c:64f4:ef97;
posting-account=H_G_JQkAAADS6onOMb-dqvUozKse7mcM
NNTP-Posting-Host: 2600:1700:291:29f0:15c8:5e5c:64f4:ef97
References: <u0f6gf$32irr$1@dont-email.me> <2023Apr4.073909@mips.complang.tuwien.ac.at>
<u10pbe$259vd$1@dont-email.me> <2023Apr10.143646@mips.complang.tuwien.ac.at>
<u118f4$27ckk$1@dont-email.me> <2023Apr10.175439@mips.complang.tuwien.ac.at>
<5a6c05d3-c011-4d2c-84c4-057afe0b5749n@googlegroups.com> <2023Apr10.230314@mips.complang.tuwien.ac.at>
<XZ_YL.1523107$8_id.1501370@fx09.iad> <u12jij$2gd1b$1@dont-email.me>
<ljdZL.420088$5S78.182599@fx48.iad> <u1454q$2mbg6$1@dont-email.me> <EUgZL.2295552$vBI8.1111497@fx15.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a85d3e0c-e0fd-4569-b7c9-2779ab298849n@googlegroups.com>
Subject: Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(
From: MitchAl...@aol.com (MitchAlsup)
Injection-Date: Tue, 11 Apr 2023 17:55:47 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3580
 by: MitchAlsup - Tue, 11 Apr 2023 17:55 UTC

On Tuesday, April 11, 2023 at 12:36:39 PM UTC-5, Scott Lurndal wrote:
> BGB <cr8...@gmail.com> writes:
> >On 4/11/2023 8:32 AM, Scott Lurndal wrote:
> >> BGB <cr8...@gmail.com> writes:
> >>> On 4/10/2023 4:13 PM, Scott Lurndal wrote:
> >>>> an...@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> >>>>> Michael S <already...@yahoo.com> writes:
> >>
> >>> This being partly because:
> >>> Variable size stack frames are evil;
> >>> If one excludes large allocations from the stack (including large
> >>> arrays), one may find that 128k of stack space is sufficient for most
> >>> programs.
> >>
> >> It's not 1989 anymore.
> >>
> >
> >?...
> >
> >AFAIK, back then stack sizes then were usually smaller, say, 8K or 16K?....
> The VAX had a 32-bit flat address space in 1978[*]. The i386 had one by 1985.
>
> Not to mention the dozens of mainframe and minicomputers available with
> large memory spaces from the last 1960s.
<
CDC 6600, 7600, CRAY 1, 1S, XMP, YMP,
IBM 360s, 370s, 30x0s,
Univac 1108,
S.E.L., InterData 32,
PDP 6, PDP 10, PDP 11, VAX, Alpha,
All RISC architectures,
:: all had flat address spaces.
>
> [*] Although in 1979, 4MB was a large amount of physical memory, VMS
> had working sets and paging, as did the 370 successors and most other
> mainframe lines. Even the PDP-10 had 256k words (36-bit) of magnetic
> core memory.
<
When I bought my first computer in 1995, It came with 4 MB, and after using
it for 2 days, I went out and bought 16MB more. Since windows 3.1 did not
use more than 16 MB, I configured the other 4MB as the disk cache, and
turned off the swapper. My 33 MHz 486 outran my wife's 100 MHz Pentium.
<
The real question is why anyone would want something other than a large flat
address space ??


devel / comp.arch / Re: On the pitfalls of C and UB: C23 seems to be even worse. :-(

Pages:123456789
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor