[TUHS] v7 K&R C

Derek Fawcus dfawcus+lists-tuhs at employees.org
Mon May 18 02:34:37 AEST 2020

On Thu, May 14, 2020 at 10:21:07AM -0700, Larry McVoy wrote:
> On Wed, May 13, 2020 at 08:42:55PM -0400, John P. Linderman wrote:
> > I never liked call by reference. When I was trying to understand a chunk of
> > code, it was a great mental simplification to know that whatever a called
> > routine did, it couldn't have an effect on the code I was trying to
> > understand except through a returned value and (ghastly) global variables.

That has always been my issue with the C++ references, that one could not
read a piece of code in isolation, and know when a reference may be made.

I guess I'd be happy with references if the syntax always required one to
write '&x' when they're being created, then the called function can choose
if it either wishes to use a pointer or a reference, the only difference
being the syntax used to deref the reference.

As to Doug's point about new arithmetic types and overloading, I recall
a few years ago reading on the 9fans list about an extension there (in KenC?)
which supported them in C.  I've not managed to dig up the details again,
maybe someone else could.  As I recall it involved defining structs.

> Call by value is fine for things like a single integer or whatever.  When
> you have some giant array, you want to pass a pointer.
> And "const" helps a lot with indicating the subroutine isn't going to
> change it.

However that is simply the ABI, i.e. it should be possible for a sufficiently
clever compiler to implement such a call-by-value as a call-by-constish-reference.

i.e. this:
    somefn(struct s p) {...}
    struct s ss;  somefn(ss);

in effect becomes syntax sugar for:
    somefn(constish ref struct s p) {...}
    struct s ss;  somefn(&ss);

Where 'constish' does not allow 'ss' to be altered even if somefn() assigns
to 'p', because in that case it would do sufficient copying so as to
make things work.  There was a proposed MIPS ABI which stated this.

The current C semantics in effect do that, but with the ABIs always having
the caller make a copy and pass a reference to it, rather than allowing the
callee to make a copy if/when required.


More information about the TUHS mailing list