Sujet : Re: technology discussion → does the world need a "new" C ?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 16. Aug 2024, 01:46:38
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9m7hf$15903$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
User-Agent : Mozilla Thunderbird
On 16/08/2024 00:29, Kaz Kylheku wrote:
On 2024-08-15, Bart <bc@freeuk.com> wrote:
On 15/08/2024 22:36, Tim Rentsch wrote:
I see. So your point is, if we ignore all the ways that the two
modes are different then they are exactly the same.
>
Brilliant deduction, Dr. Watson.
>
So your approach is to totally ignore all the ways that the two modes
are identical.
Almost any two different things have some attributes that are identical.
If we consider a bicycle and a fish, we can probably come up with common
attributes.
Passing pointers by value similar to call-by-reference, but also
different.
I'm going to show a demo in my language because C doesn't support both by-value and by-reference.
However, here is the F function written in C (one point of difference is that my 'int' is 64 bits):
void F(int* A) {
int i;
A[i]=0; // same as (A+i)=0
}
The above uses the C idiom for passing and using arrays, which I will try and emulate here:
proc F(ref int A) = # equivalent to C func above
int i
(A+i-1)^:=0 # ptr-relative must be 0-based
end
proc H([]int &A) = # pass-by-reference
int i
A[i]:=0
end
proc main =
[10]int A
F(cast(&A)) # Pass A to F, C-style
H(A) # Pass A to H, by-reference
end
(What I can't emulate is the call to F which in C looks like F(A), exactly like the call H.)
So Tim, and apparently you, claim there are no points of similarity, or that they don't have any bearing on anyway. Well here are the bodies of F and H when translated to x64 assembly, excluding entry/exit code:
mov u64 [D10+D3*8-8], 0 # body of F ('i' resides in D3)
mov u64 [D10+D3*8-8], 0 # body of H
You can see that they are identical. And here the two calls:
lea D10, [D14+t.main.a]
call t.f
lea D10, [D14+t.main.a]
call t.h
That's quite remarkable: the guts of a call-by-value function and its call, being identical to that of a call-by-reference function and /its/ call.
A bit like comparing the insides of a bicycle and a fish, and finding they are the same!
The program can calculate a bad pointer and pass that by value.
Call-by-reference implementations can take measures to ensure that
a a bad reference is not passed.
Yes, both my G and H examples have better type-checking than the F function which was idiomatic C (you can see I had to use an explicit ... I mean just cast, above).
That doesn't really affect the substance of the discussion that what is actually passed in registers is not the value of the array elements. But C people are cagey about exactly how F works if it's not really by-value and not really by-reference.
If your point of view is that pointers are what is "real under the
hood", then call-by-reference is just "syntactic sugar" for pointers.
No, my point of view, first expressed on 9-Jul-24, was in response to a claim that everything in C was passed by value, and that was that arrays are passed by reference. But I left out the work 'effectively'.
Of course, everybody know that is actually the case, but everybody also wanted to argue otherwise.