Liste des Groupes | Revenir à cl c |
On 13/07/2024 10:39, BGB wrote:I don't think that's the /only/ purpose of this group, but people like chatting about things that interest them even if they are not important. And something might seem pointless to one person and important to another.
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.Just to be clear - it is passing a pointer by value. The array is not passed in any way.
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-referenceThis is identical to (1). Both are passing a pointer by value, and both are emulating a limited type of pass by reference (limited in that the length of the array is not part of the passed parameter, but must be given independently).
(3) Using fantasy true call-by-reference as it might appear if C had theThe fantasy matches C++ pretty closely - except you are making a reference to an unbound array of type T, not an actual array.
feature
(I'd hoped C++ would run this, but it didn't even like the middle function.)It is fine in C++20 or C++23, but not in C++17 or before. (If you want to know why, ask in comp.lang.c++, because I don't know the answer!)
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.The generated code (for gcc -x c++ -std=c++20) is the same for both, so there is no difference there. Using the reference means you can't pass a null pointer, you can't pass a pointer to an individual int, and you can't use sizeof, so they are not entirely the same. And as I wrote above, "sum_bytrueref" is not actually passing the array at all.
---------------------------------------------It is correct that C does not need pass by reference for arrays. It does not need to be able to pass arrays at all. This is clearly demonstrated by the fact that C does not have pass by reference, and cannot pass arrays at all, and yet has been used happily (and sometimes unhappily) for half a century.
#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:No, better to be safe.
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!
Les messages affichés proviennent d'usenet.