Bart <
bc@freeuk.com> writes:
On 19/08/2024 02:30, Ben Bacarisse wrote:
Bart <bc@freeuk.com> writes:
On 19/08/2024 01:01, Ben Bacarisse wrote:
Bart <bc@freeuk.com> writes:
>
>
You or he would have to go into more detail, such as an actual example, to
demonstrate whatever it is that you think is wrong about passing a pointer
argument by-reference.
No one has said any such thing, so I can't see how any more detail could
help. I suspect you've lost track of the point being made.
>
Probably, and perhaps not just me! But I'd still quite like to know exactly
what it is that is marked as 'disallowed'.
I don't know how to explain it any better. If you really want to know,
maybe you could say what /you/ think was incorrectly marked as
disallowed so I can see how you were interpreting the table.
>
I think I already said that several times!
>
But I need to use concrete code, and there is no existing mainstream
language with pass-by-reference that is simple enough to use for such
examples. Except for mine and that is not acceptable here.
>
So I'll use an extended C where a '&' in front of a parameter's base-type
marks it as pass-by-reference.
>
First an example written in standard C:
>
#include <stdio.h>
>
void F(int* p) {
printf("%d\n", *p);
++p;
}
>
int main(void) {
int A[] = {10,20,30};
int* p = &A[0];
>
F(p);
printf("%d\n", *p);
}
>
This passes a normal pointer. The output is 10 10 from both printfs,
because the ++p within F does not affect the original pointer in 'main'.
>
Now the same program, but using pass-by-reference for 'p'; I won't show the
whole thing, as the program looks exactly the same except for this line:
>
void F(&int* p) {
>
The output should now be 10 20 (as verified using my language; if
interested, that is shown below, and below that is the standard C that
might be generated by a transpiler from this fantasy C).
>
So this is passing a pointer, by reference, and it is allowed!
I can't imagine why you though this was what either Tim or I were
talking about, but if that is genuinely what you thought he was saying
was "disallowed" so be it. We could spend ages arguing about how you
could possibly have thought this (especially as you wrote the original
example), but nothing will be gained by doing that.
Or maybe, just maybe, Tim said it for good reason.
>
And yet, he continues to be cagey about it, and you're defending him. Just
give me a freakin' example!
>
WHAT EXACTLY IS IT THAT IS DISALLOWED?
Good grief! You gave the example yourself! I'm not being cagey -- it
never occurred to me that you could possibly be thinking about anything
other the code you yourself posted and talked about!
You: "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."
Your first and third functions:
int sum_byvalue(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;
}
and the caller uses:
enum {N = 10};
T A[N] = {10,20,30,40,50,60,70,80,90,100};
...
total += sum_byvalue (A, N);
...
total += sum_bytrueref(A, N)
A post later and you used shorter names to show the similarities you
wanted everyone to focus on -- the accesses in the functions. You used
F for the function with a T* parameter and H for the function with a
T(&)[10] parameter -- the true call-by-reference parameter that
"sum_bytrueref" has a post earlier.
Call Array access in callee
(1) C call-by-value F(A) A[i]
...
(3) true call-by-reference H(A) A[i]
Tim just extended this by showing more call differences and more
differences in access in the callee. The bit you so totally
misunderstood was:
C call-by-value call-by-reference
=============== =================
at call:
(array argument) F(A) H(A)
(pointer argument) F(p) (disallowed)
(null argument) F(0) (disallowed)
F (your name) can be called with an array argument (A) but not with a
pointer argument (including a null pointer constant). Do you agree now
that H(p) and H(0) are disallowed?
How you thought that those last two rows of (this part of) the table
could refer to a /different/ function (with a T*& parameter) -- a
function that no one had posted up to that point -- is beyond me, but
apparently you did. What's more you decided to put no effort in to try
to work out what was being said. Instead you just said Tim was wrong
and implied that I was defending the indefensible out of some sort of
blind obedience.
-- Ben.