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, ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    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.

    So, I perused Stack Overflow and I found this post:

    Difference between pointer variable and reference variable in C++ - Stack Overflow

    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.

    Source: null (C# Reference)

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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).

    Furthermore, new returns a pointer in C++, not a reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    I know that, I was just wondering how it would be done in those languages since it's allowed in C# and Java to have a null reference, but in C++ it isn't.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    What exactly do you mean by "how it would be done"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    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.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    I think this is a null reference, although the behaviour should be undefined according to the ISO definition.
    Code:
            int *pointer = NULL;
    	int &null_ref = *pointer;
            cout<<&null_ref;
    A reference is just an alias of a variable.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by jshowa View Post
    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.
    If it is not permitted to have a NULL reference, then a huge class of potential problems simply evaporates.

    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++.
    If you want something that can refer to an object, or to nothing at all, then use a pointer. It's not like there's missing functionality in the language.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Thumbs up

    Quote Originally Posted by jshowa View Post
    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
    2B OR !2B? That is the question!

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by like_no_other View Post
    I think this is a null reference, although the behaviour should be undefined according to the ISO definition.
    Code:
            int *pointer = NULL;
    	int &null_ref = *pointer;
            cout<<&null_ref;
    A reference is just an alias of a variable.
    In any platform I've used, that would blow up as soon as it evaluates *pointer, which is the same as *NULL.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,317
    Quote Originally Posted by User Name:
    Despite the fact x feature isn't possible in C/C++
    In this case, the feature exists in both C and C++, except that we call them pointers*, and they have different syntax and are more powerful than the references of those other languages.

    * that said, Java has this thing named NullPointerException, so they arguably recognise references as pointers without pointer syntax and pointer arithmetic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yeah, that always makes me laugh. Why would they have a NullPointerException if they're so against calling their crippled pointers 'pointers'? Shouldn't they call them NullReferenceExceptions?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Quote Originally Posted by Dante Wingates View Post

    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.

    You can also overload operators in C#: C# Operator Overloading..|..C# Help C# Tutorials and Resources

    Quote Originally Posted by laserlight View Post
    * 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?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    References in java are glorified pointers. The only difference is that the "->" syntax is replaced with ".".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,317
    Quote Originally Posted by jshowa
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 01:29 PM
  2. Pointers and references?!? Help!
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2008, 10:23 AM
  3. Pointers v References
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 06-30-2008, 01:29 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21