J
jacob navia
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;
As always in this proposals, the problems hide in the details. For
instance, several days ago someone complained that a reference to
void would not compile, but a pointer to void would. This prompted
me to correct a problem in my implementation: I should have forbidden to
build references to void!
The problem is when defining a reference but when you pass a reference
to a function:
int fn(void &ref);
In this case there is no initialization (that would explode immediately)
but just a definition without any initialization. The compiler must
check, then, that the reference is not to a void pointer.
Since the object pointed to by a reference can't change, many people
think that the value can't never change but that is wrong. Consider:
#include <stdio.h>
int main(void)
{
int i = 23;
int &pi = i;
i = 3;
printf("%d\n",pi);
}
The reference is initialized to an integer whose value is 23.
If the integer changes its value, the reference (like all other
pointers) changes its value also.
The advantages of using references are that they are never NULL,
and that they always point to the same object, two important
things less to test. The only (orthodox) way of declaring a non-NULL
pointer in an argument list is now:
int fn(int arg[static 1]);
This declares that the "arg" argument will have at least one integer
Few people know this, and it wasn't much explained when C99 introduced
it.
jacob
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;
As always in this proposals, the problems hide in the details. For
instance, several days ago someone complained that a reference to
void would not compile, but a pointer to void would. This prompted
me to correct a problem in my implementation: I should have forbidden to
build references to void!
The problem is when defining a reference but when you pass a reference
to a function:
int fn(void &ref);
In this case there is no initialization (that would explode immediately)
but just a definition without any initialization. The compiler must
check, then, that the reference is not to a void pointer.
Since the object pointed to by a reference can't change, many people
think that the value can't never change but that is wrong. Consider:
#include <stdio.h>
int main(void)
{
int i = 23;
int &pi = i;
i = 3;
printf("%d\n",pi);
}
The reference is initialized to an integer whose value is 23.
If the integer changes its value, the reference (like all other
pointers) changes its value also.
The advantages of using references are that they are never NULL,
and that they always point to the same object, two important
things less to test. The only (orthodox) way of declaring a non-NULL
pointer in an argument list is now:
int fn(int arg[static 1]);
This declares that the "arg" argument will have at least one integer
Few people know this, and it wasn't much explained when C99 introduced
it.
jacob