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 : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 17. Jul 2024, 16:56:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v78piu$1su4u$1@dont-email.me>
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 25
User-Agent : Mozilla Thunderbird
On 17/07/2024 14:34, Michael S wrote:
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,
As written, the function's parameters match arrays of any size, with the length passed separately.

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.
Is it? Then that's not what I want; this not about emulating value arrays.
If C++ has pass-by-reference, then it should apply to any type. But it has trouble even with the middle function which is valid C.

The proper comparison would be vs language that has arrays as 1st class
citizen.
That is not neccessary here, since I'm trying to show that there is little difference in the calling code and the function body between the idiomatic C code for passing arrays, and what would be used with true pass-by-reference.
But it is true that my fantasy & parameter would probably interfere with the rules of C to do with reducing T[] parameter types to T*.
However I'm not allowed to use my language as an example. But the differences can be summarised here; This example passes an array A to the 3 finds of function:
                                  Call     Array access in callee
(1) C call-by-value              F(A)     A[i]
(2) C call-by-value
       emulating call-by-ref      G(&A)    (*A)[i]
(3)   true call-by-reference     H(A)     A[i]
What the user has to write is what's important, and here it is clear that they write the same thing in (1) and (3).

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