Re: technology discussion → does the world need a "new" C ?

Liste des GroupesRevenir à l c 
Sujet : Re: technology discussion → does the world need a "new" C ?
De : already5chosen (at) *nospam* yahoo.com (Michael S)
Groupes : comp.lang.c
Date : 17. Jul 2024, 14:34:57
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240717163457.000067bb@yahoo.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
User-Agent : Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
On Wed, 17 Jul 2024 12:38:15 +0100
Bart <bc@freeuk.com> wrote:

On 13/07/2024 10:39, BGB wrote:
 
But, as I see it, no real point in arguing this stuff (personally,
I have better stuff to be doing...). 
 
We all do. But this group seems to be about arguing about pointless
stuff and you might come here when you want a respite from proper
work.
 
However (here I assume you've gone back to Quake but that other
interested parties might be reading this), consider the program below.
 
That sets up an array and then sums its elements by calling 3
different functions to do the job:
 
(1) Using normal C pass-by-value
 
(2) Using C pass-by-value to emulate call-by-reference
 
(3) Using fantasy true call-by-reference as it might appear if C had
the feature
 
(I'd hoped C++ would run this, but it didn't even like the middle
function.)
 
I'm asking people to compare the first and third functions and their
calls, and to see if there's any appreciable difference between them.
There will obviously be a difference in how the A parameter is
declared.
 
---------------------------------------------
#include <stdio.h>
 
typedef int T;
 
int sum_byvalue(T* A, int n) {
     int i, sum=0;
     for (i=0; i<n; ++i) sum += A[i];
     return sum;
}
 
int sum_bymanualref(T(*A)[], int n) {
     int i, sum=0;
     for (i=0; i<n; ++i) sum += (*A)[i];
     return sum;
}
 
int sum_bytrueref(T (&A)[], int n) {
     int i, sum=0;
     for (i=0; i<n; ++i) sum += A[i];
     return sum;
}
 
int main(void) {
     enum {N = 10};
     T A[N] = {10,20,30,40,50,60,70,80,90,100};
     int total=0;
 
     total += sum_byvalue     (A, N);
     total += sum_bymanualref (&A, N);
     total += sum_bytrueref   (A, N);
 
     printf("%d\n", total);             // would show 1650
}
---------------------------------------------
 
Find anything? I thought not.
 
Those findings might suggest that C doesn't need call-by-reference,
not for arrays anyway. Except that at present you can do this:
 
     T x=42;
     sum_byvalue(&x, N);
 
which would not be possible with call-by-reference. Nor with
sum_bymanualref, but apparently nobody wants to be doing with all
that extra, fiddly syntax. Better to be unsafe!

The C++ syntax your are looking for is sum_bytrueref(std::array<T,10>&A,
And indeed, the generated code is the same.
https://godbolt.org/z/dYGoWsdjE
But why is it the same? Because in C++ arrays are also 2nd class
citizen, like in C ! std::array is not a 'native' C++ type, but a
wrapper around array-within-struct pattern.
The proper comparison would be vs language that has arrays as 1st class
citizen.

I tried to produce Ada example on Godbolt, but it seems that support
for Ada on GB is too limited. I was not able to convince it to compile
package. And without packages I was not able to illustrate my point.


Date Sujet#  Auteur
5 Jul 24 * Re: technology discussion → does the world need a "new" C ?305BGB
5 Jul 24 +* Re: technology discussion → does the world need a "new" C ?2Lawrence D'Oliveiro
5 Jul 24 i`- Re: technology discussion → does the world need a "new" C ?1yeti
15 Jul 25 +- 
5 Jul 24 `* Re: technology discussion → does the world need a "new" C ?26bart
5 Jul 24  +- Re: technology discussion → does the world need a "new" C ?1BGB
6 Jul 24  `* Re: technology discussion → does the world need a "new" C ?24Lawrence D'Oliveiro
6 Jul 24   +* Re: technology discussion → does the world need a "new" C ?17Keith Thompson
6 Jul 24   i+- Re: technology discussion → does the world need a "new" C ?1Janis Papanagnou
6 Jul 24   i`* Re: technology discussion → does the world need a "new" C ?15Lawrence D'Oliveiro
6 Jul 24   i +- Re: technology discussion → does the world need a "new" C ?1Ben Bacarisse
6 Jul 24   i +- Re: technology discussion → does the world need a "new" C ?1Keith Thompson
7 Jul 24   i +* Re: technology discussion → does the world need a "new" C ?10James Kuyper
10 Jul 24   i i`* Re: technology discussion → does the world need a "new" C ?9Lawrence D'Oliveiro
10 Jul 24   i i `* Re: technology discussion → does the world need a "new" C ?8James Kuyper
11 Jul 24   i i  `* Re: technology discussion → does the world need a "new" C ?7Lawrence D'Oliveiro
11 Jul 24   i i   +* Re: technology discussion → does the world need a "new" C ?2David Brown
11 Jul 24   i i   i`- Re: technology discussion → does the world need a "new" C ?1Malcolm McLean
11 Jul 24   i i   +* Re: technology discussion → does the world need a "new" C ?3bart
11 Jul 24   i i   i`* Re: technology discussion → does the world need a "new" C ?2Chris M. Thomasson
12 Jul 24   i i   i `- Re: technology discussion → does the world need a "new" C ?1Chris M. Thomasson
11 Jul 24   i i   `- Re: technology discussion → does the world need a "new" C ?1James Kuyper
7 Jul 24   i +- Re: technology discussion → does the world need a "new" C ?1Tim Rentsch
25 Aug 24   i `- Re: technology discussion ? does the world need a "new" C ?1dave thompson 2
6 Jul 24   +- Re: technology discussion → does the world need a "new" C ?1Janis Papanagnou
6 Jul 24   +- Re: technology discussion → does the world need a "new" C ?1James Kuyper
6 Jul 24   `* Re: technology discussion → does the world need a "new" C ?4bart
7 Jul 24    `* Re: technology discussion → does the world need a "new" C ?3Keith Thompson
7 Jul 24     `* Re: technology discussion → does the world need a "new" C ?2bart
7 Jul 24      `- Re: technology discussion → does the world need a "new" C ?1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal