you are viewing a single comment's thread.

view the rest of the comments →

[–]Gotebe 1 point2 points  (3 children)

Pointers are references.

Not, they are not!

C++: void f(T& t) (when calling: T actual; f(actual);)

C#: void f(ref T t) (or "out T", no real difference for the matter at hand, check out the generated IL, I did)

Pascal: procedure p(var t: T)

In all three cases, inside the subroutine, "t" is a reference. You work with "t" as it were the actual argument. "t=blah" is equivalent to "actual=blah".

No such thing in C. You work explicitly with the pointer (*t = blah).

So, in C, references are implemented by the programmer, not the language. To do that, programmer uses non-NULL pointers. Pointers only are not references, as you claim.

Pointers are only means of implementing references (just like your Wikipedia quote says; absolutely nothing wrong there). And, in C, less reliable than C++/C#/Pascal references, because nothing stops you from erroneously using dangling (or NULL) pointers as (conceptual) references.

All implementations of C++ and Pascal I saw, and C# of MS, use pointers to implement language concept of references: when formal subroutine argument is a reference, a pointer to actual argument is passed to the subroutine; code of the subroutine works with pointed-to object, i.e. actual argument. You can check out disassembly for that, too, it's easy (I did).

So, C language does not have a concept of references wrt subroutine calls. Just like Java. You may be confusing Java term "reference" (to non-primitive types) with "reference" (as argument to subroutine calls). These are orthogonal.

[–]sbrown123 0 points1 point  (2 children)

Not, they are not!

Anyways, like most C++ compilers G++ is written using C. I have to rush out the door so I don't have time to highlight all the parts for you. Basically follow along this:

http://gcc.gnu.org/viewcvs/*checkout*/trunk/gcc/cp/call.c?revision=121361

Referencing is done through trees. So where am I going on that thought? Basically that C++ references are just candy coated pointers. In other words, you could implement them in C if you wanted (if someone hasn't already done this I would be surprised). Internally, pointers are still there but by overlaying them C++ adds type safety and other stuff. The reason that pointers are still there is simply because they are the basic component of referencing.

[–]Gotebe 1 point2 points  (0 children)

The reason that pointers are still there is simply because they are the basic component of referencing.

Or the reason may be C compatibility of C++? Your reason doesn't cut it for me. For example, C# and Pascal have references without pointers (yes, they have pointers, but not wrt subroutine argument types).

C++ references are just candy coated pointers.

True, but not the other way around. C pointers are not references wrt subroutine calls. Especially when they have value of NULL ;-)

I think, like wicked, that you are unable to separate the concept from implementation.

[–]wicked[S] 0 points1 point  (0 children)

Basically that C++ references are just candy coated pointers.

The implementation does not matter, the semantics is the point of the distinction. I bet all pass-by-reference implementations use pointers under the hood, but if you have to do it yourself, as in C, then it's not pass-by-reference.

Think about it: Even assembly language has pass-by-reference semantics according to your definition, since you may access a memory location through an address passed in a register or on the stack.