difference between pointers and references

This is a discussion on difference between pointers and references within the C++ Programming forums, part of the General Programming Boards category; I was reading up on the differences between pointers and references because even though they operate similarly, i.e. through indirection, ...

difference between pointers and references

I was reading up on the differences between pointers and references because even though they operate similarly, i.e. through indirection, they have very different mechanics and semantics.

I couldn't find a direct answer searching the forums because I think the keyword null in modern OOP languages (i.e. C#, Java) is implemented differently then the NULL macro in C++, C which is defined as 0.

And what caught my eye was point number 4 in the answer to the post given by Brian R. Bondy.

"4) Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain NULL."

Code:

int *p = NULL;
int &r = NULL; <--- compiling error

Now this confused me a little because I was weaned on OOP languages such as Java and C# and from what I read in books, when an object is created using the new operator, memory is allocated for the object on the heap and a reference is returned and stored in the object variable.

If this is the case then how come the following code in C# is acceptable as well as Java?

Code:

Square square = new Square();
square = null;

If anyone can provide and explanation, it would ease my mind greatly because I'll be thinking about it all the time if I don't get closure.

And for further evidence, according to the MSDN C# reference:

The null keyword is a literal that represents a null reference, one that does not refer to any object.

Java and C# are not C++!
References MUST be bound to something (they cannot be NULL or rather nullptr, as it should be today). They cannot be reassigned either.
Java mixes pointers and references into one concept called a reference. Dunno about C#.
Pointers can be null, in which case it is a null pointer (nullptr_t).

I'm just wondering why references can't be assigned a null value in C++, where in modern OOP languages, you can assign a null reference to an object variable.

I mean it seems strange to make it possible in languages like C# or Java, but not possible (unless you try really hard) in C++.

I guess it's not really in scope here. I was just curious to see if someone could provide a conjecture.

Code:

int *ptr = NULL, &ref = *ptr;

do you want a reference to reference nothing? What is the point of having such a reference? Oo

If that is the case, a null pointer should be more than enough.

Also, are you saying that C++ is incomplete compared to your so called "modern languages"? I would never say such a thing while using "Java" as an argument, where you can't even overload an operator or use pointer arithmetics

The whole concept is pointless. Anything declared in source has a predefined area to hold it's value, whether that space be in the x segment or the stack. Using a different, non-predetermined area would break globals, the temporary nature of locals, and any form of referencing done through linking.

Despite the fact x feature isn't possible in C/C++, I'll bet that if you dig deep enough, you'll find C/C++ at the core of whatever language has x feature. Assuming it wasn't coded in asm, in that case, you should be expecting endless incompatibilities.

Also, are you saying that C++ is incomplete compared to your so called "modern languages"? I would never say such a thing while using "Java" as an argument, where you can't even overload an operator or use pointer arithmetics

I never said I was comparing anything, I'm just confused on why a null reference is consider unacceptable in C++, but acceptable in C# and Java.

* that said, Java has this thing named NullPointerException, so they arguably recognize references as pointers without pointer syntax and pointer arithmetic.

Does this mean that the whole concept of a reference doesn't really exist in Java? The only time I've got these is with objects that have no established reference. If this is the case, then are pointers really returned when the new keyword is used instead of a reference?

Does this mean that the whole concept of a reference doesn't really exist in Java? The only time I've got these is with objects that have no established reference. If this is the case, then are pointers really returned when the new keyword is used instead of a reference?

What is the "whole concept of a reference"? References in Java are indeed reference types in that a reference variable refers to some object. Yet, they are different and in a way more limited than references in C++, yet in a way more powerful than references in C++. They behave like pointers, yet they lack pointer arithmetic and do not need to be dereferenced like a pointer. Does the new keyword in Java return a pointer? Conceptually, I'd say no, yet in a way it does, insofar as references in Java are pointers without pointer syntax.