Jump to content
  • Advertisement
Sign in to follow this  
Flimflam

Pointers and References

This topic is 4649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question for everyone here, like a few other random questions I've asked in the past, this is just a question that's been bugging me off and on for a while. It's the use of References vs. Pointers. I've heard some swear by References, saying you should never use a pointer unless it's the only possible option (ie, being able to pass a NULL as a parameter, etc.) I've heard others also say that Pointers should be able to be used more freely than that. As I'm understanding it, it seems to be an issue of programmer preference. I see the benefits of References and Pointers alike, but what I'm wondering now, is there really much of a reason to use References over pointers entirely unless I absolutely need pointers? For example, a simple program I have written up, a device manager for DirecetX, GetDisplayMode() returns a pointer to a D3DDISPLAYMODE structure inside the class. Now, instead of that I could make it return a reference easily. I suppose what I'm asking is why asside from accidentally changing where a pointer points to (I could make the pointer const to achieve that anyway,) would I consider replacing pointers with references? Thank you for reading yet another "It's Time to Know(tm)" question :)

Share this post


Link to post
Share on other sites
Advertisement
I'm not religious in either of them, though most of the time I prefer references since:

  • Passing an object by reference means I don't have to check if the passed object is valid, while pointer I always have to check for nullity.

    However, pointers has its place also:
    1. It increases portability between C code and C++ code.
    2. Pointer itself can be a flag whether or not the target function can freely modify the memory it points to. For example:

      HDC BeginPaint(HWND hwnd, LPPAINTSTRUCT lpPaint) ;

      The function requires a pointer to be passed, it returns additional infos in the passed LPPAINTSTRUCT pointer, if the passed pointer is NULL, it returns nothing. If the second parameter was a reference to a PAINTSTRUCT, whether I want the additional info or not, I have to pass a valid variable, which can reduce performance unnecessarily.
    3. Sometimes you just can't use references over pointers.

    Share this post


    Link to post
    Share on other sites
    Quote:
    Original post by Flimflam
    is there really much of a reason to use References over pointers entirely unless I absolutely need pointers?


    The main advantage of references (ignoring how they make operator overloading possible in less obtuse manners) is that they should allways reference a valid object - a program exhibiting null references is not well formed, uninitialized references are not allowed - whereas a program exhibiting null or uninitialized pointers is fine and dandy (as long as you don't try to dereference them).

    Thus, it follows, one does not need to assert() that a reference is not null as one would with pointers, nor does one need to make a conditional with error handling code in the case that it is - any code that has no object to create a reference from when one is needed should throw, escaping the problem by escaping that stack frame. This reduces error-handling detrius from the main flow of your program when using references.

    Assuming your program is well formed, anyways. This usually means assert()ing pointers arn't 0 before turning them into references (or throwing if they are, or otherwise avoiding ever actually creating that monstrosity). Compilers are nice enough to generate compiler errors for uninitialized references, instead of /maybe/ a warning about using an uninitialized pointer, thus preventing that nasty, all too often frogotten step.

    Quote:
    I suppose what I'm asking is why asside from accidentally changing where a pointer points to (I could make the pointer const to achieve that anyway,) would I consider replacing pointers with references?


    Retroactively switching code from pointers to references is a good way to waste your time pretending to be productive, if that's what your asking. If you're asking why you might want to use references in the future, I've answered that above :).

    Share this post


    Link to post
    Share on other sites
    My rule is, if it's possible to use references, use 'em. If it's too awkward, though, don't feel bad about using pointers.

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    Quote:
    Original post by Flimflam
    I suppose what I'm asking is why asside from accidentally changing where a pointer points to (I could make the pointer const to achieve that anyway,) would I consider replacing pointers with references?)

    There is one difference between references and const pointers that you seem to have overlooked: The delete operator can be called on a const pointer.*

    *Even though it may (it's compiler-dependent) be possible to delete memory referenced by a reference, doing so is an unsafe practice.

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    ...and there's a trick involved. You can't really accidently do it.

    Share this post


    Link to post
    Share on other sites
    References or Smart Pointers (some have zero overhead), unless you need to interact with legacy C code. Even then you won't need C style pointers.

    Pointers can introduce a lot of errors and you will always end up doing more work in debugging and coding for exception saftey when you rely on pointers.

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!