J
jacob navia
Le 27/06/11 02:53, Tim Rentsch a écrit :
In C it would allow to avoid the famous "array decay" problem where
arrays are converted into pointers. Using references this works as expected:
#include <stdio.h>
#define ELEMENTS 5
#define COUNTOF(array) \
(sizeof (array) / sizeof *(array))
typedef double a_five_doubles[ELEMENTS];
// Change the following line to
// void foo(a_five_doubles & array)
// and it works as expected
//
void foo(a_five_doubles array)
{
size_t i;
printf("sizeof a_five_doubles=%ld\n",sizeof(array));
for (i = 0; i < COUNTOF(array); ++i) {
array = 3.14159;
}
}
int main(void) {
a_five_doubles array;
for (int i = 0; i < COUNTOF(array); ++i) {
array = i;
}
printf("sizeof a_five_doubles=%ld\n",sizeof(array));
foo(array);
return 0;
}
Other advantages are that you can be sure that they point always to the
same object, and that they are not NULL. This improves safety of
programs
The one quality that
You have greatly exaggerated the completely minor problems of learning
by newcomers, and minimized the safety improvements to the language.
jacob navia said:The lcc-win compiler is an experimental compiler to promote the
development of C as a language. Contrary to the main trends of
language design this days, lcc-win considers C not a dead language but
a language that can evolve and propose new features.
In this context, one of the extensions that lcc-win supports is
references, where the design is largely inspired from the C++ design.
What are references?
In fact, a reference is just a pointer. It is different from plain
pointers in two ways:
(1) It is always assigned to a concrete object when it is defined.
(2) Once defined, it will always point to that same object. It can't
be changed to point into another object.
(3) It is used with the '.' notation, as if it weren't a pointer.
(4) Instead of "*' the symbol "&" is used:
int i = 23;
int&IntReference = i;
[snip elaboration]
Let's look at the plusses and minuses.
Plus:
1. Reduced syntax - calls don't need '&' to take address,
uses don't need '*' to dereference, don't need 'const'
in declarations. . Frequency: common. New capability
offered: none, all these can be done currently using
small amounts of local syntax. Benefit: minor.
2. Null values avoided. Frequency: common. New capability
offered: none in the language (no additional semantics),
some in program checking (eg, like 'lint'). Benefit:
minor for program development, positive for program
verification. (See additional comments in Orthogonal.)
Orthogonal:
1. Null values avoided. I think this item more properly
belongs to program verification than programming language.
Avoiding null values doesn't add any semantics, it only
makes certain conditions impossible (presumably there
would be compiler errors). Obviously doing this has
value, but it doesn't have to be a language feature to
get the value. The proposed language feature is tied
to references, but it would be nice if a similar test
could be made for some pointer parameters. Or how about
other kinds of tests, like integer values being positive?
There are lots of useful and potentially useful kinds of
checking that could be done, but these should not be part
of the programming language, because they do not add any
language semantics, they only facilitate program checking.
A better model is either a lint-like separate checker, or
extra-linguistic progrm annotation ala __attribute__ in
gcc.
Minus:
1. Language is bigger. There is more to read and understand,
and it complicates the type system (as anyone who has
tried to read through the C++ standard can attest).
Frequency: ubiquitous for new developers, presumably low
for experienced developers. Cost: hard to quantify, but
not trivial.
2. Unobvious effects on program semantics. Because of how
non-lvalues are dealt with, calls that look similar may
have very different semantics; eg, if foo() accepts a
reference argument, 'foo( x )' and 'foo( x++ )' behave
very differently (and unobviously so as far as the call
site goes). Frequency: presumably low. Downside: for
cases where it matters, potentially large. Cost: low
frequency times large downside gives non-negligible cost.
3. Hidden side effects. Without references, when calling a
function it's immediately obvious if argument values can
be modified -- if there is an '&' (or an array is used),
then the argument might be modified, otherwise it can't
be. With references, any lvalue argument is potentially
modifiable; the only way to know is check the function
prototype. Frequency: high. Downside: additional effort
spent in program development. Cost: extra work on every
function call gives significant cost.
Summary:
In C++, the justification for references usually involves
struct's or class'es with member functions, and perhaps
the additional rules related to type conversions in the
presense of C++ 'base'/'derived' types.
In C, these factors simply aren't present. References
add essentially no additional semantic power, and bring
with them a significant burden.
In C it would allow to avoid the famous "array decay" problem where
arrays are converted into pointers. Using references this works as expected:
#include <stdio.h>
#define ELEMENTS 5
#define COUNTOF(array) \
(sizeof (array) / sizeof *(array))
typedef double a_five_doubles[ELEMENTS];
// Change the following line to
// void foo(a_five_doubles & array)
// and it works as expected
//
void foo(a_five_doubles array)
{
size_t i;
printf("sizeof a_five_doubles=%ld\n",sizeof(array));
for (i = 0; i < COUNTOF(array); ++i) {
array = 3.14159;
}
}
int main(void) {
a_five_doubles array;
for (int i = 0; i < COUNTOF(array); ++i) {
array = i;
}
printf("sizeof a_five_doubles=%ld\n",sizeof(array));
foo(array);
return 0;
}
Other advantages are that you can be sure that they point always to the
same object, and that they are not NULL. This improves safety of
programs
The one quality that
may offer value doesn't belong in the language, any
more than gcc's compiler-option style rules belong in
the language. Adding references to C gives a net result
that is overall a big minus.
You have greatly exaggerated the completely minor problems of learning
by newcomers, and minimized the safety improvements to the language.