CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2013
    Posts
    19

    uses of pointers

    i am reading jumping to C++ book.where now i am going through pointers chapter.
    there i seen pointer are used as reffernce variable to pass into the function,like so.but what are the other uses of pointers.while you list what are all the other way that we can use it .

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: uses of pointers

    There can be two reasons to use a pointer instead of a reference:
    1. A pointer can point to NULL (i.e. no object), whereas a reference must refer to a valid object. So you can use a pointer e.g. to pass an optional parameter to a function.
    2. You can change what a pointer points to. This is particularly useful when storing references to other objects as members of a class. When you use references, the class becomes non-assignable, but when you use pointers, it doesn't.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: uses of pointers

    Quote Originally Posted by D_Drmmr View Post
    1. A pointer can point to NULL
    Or preferrably nullptr.

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: uses of pointers

    Quote Originally Posted by bhuvaneshnick View Post
    there i seen pointer are used as reffernce variable to pass into the function,
    .
    That's a usage that shouldn't be confused with a parameter passing mechanisms. There are only two different ways to pass a parameter and that's by value or by reference. Also a pointer can be passed either by value or by reference as can any parameter.

    Today maybe the most common use of pointers is in association with object oriented programming to refer to objects stored on the heap. But even then pointers are often held by so called smart pointers such as shared_ptr.

    I think it's fair to say that pointers are more and more restricted to use in low-level code and avoided elsewhere. The reason is they're bug prone. It was in fact problems related to pointer usage in C++ that paved the way for newer languages such as Java.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: uses of pointers

    Quote Originally Posted by razzle View Post
    I think it's fair to say that pointers are more and more restricted to use in low-level code and avoided elsewhere. The reason is they're bug prone. It was in fact problems related to pointer usage in C++ that paved the way for newer languages such as Java.
    Using raw pointers is not inherently bug-prone. Doing manual memory management is bug-prone and should be avoided whenever possible (e.g. using vector, unique_ptr and shared_ptr). However, simply replacing the use of raw pointers with smart pointers does not solve all problems related to object lifetime management, which seems to be a misconception that floats around and may be supporting the erroneous idea that "you shouldn't use raw pointers, except in low-level code".

    E.g. take a program that keeps a bunch of shapes modelled as a class hierarchy. You could have a bug where e.g. drawing code is accessing a shape object that has been deleted (i.e. you have a dead pointer). Using shared_ptrs instead of raw pointers would avoid accessing a deleted object, but would not fix the bug: you are still drawing an object that shouldn't be drawn (because it should be deleted). Such bugs may be benign, such as only wasting processing time, or they may affect the outcome of your program. Now the problem is that you have to figure out where in your program the wrong result originates. Had you used raw pointers, it would be likely that you would have had a crash somewhere, making the bug easier to detect and locate.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Jul 2013
    Posts
    576

    Re: uses of pointers

    Quote Originally Posted by D_Drmmr View Post
    Using raw pointers is not inherently bug-prone.
    That's exactly what they are. They're inherently bug-prone and that's why Java banned them.

    Your claim that raw pointers are safer because they crash harder than smart pointers must be the worst case of denial I've heard in a long time.

    I'm not saying you should use raw pointers in low level code. I'm saying it's a trend among developers who's got the message to do so. As confined as possible. In general code there shouldn't be any raw pointers in sight at all.
    Last edited by razzle; January 13th, 2014 at 06:42 AM.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: uses of pointers

    Quote Originally Posted by razzle View Post
    Your claim that raw pointers are safer because they crash harder than smart pointers must be the worst case of denial I've heard in a long time.
    I did not say raw pointers are safer than smart pointers, quite the opposite. I said that blindly replacing the use of raw pointers with shared pointers can hide bugs in your program or make them harder to spot. I prefer having my program crash over having it silently produce wrong results.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: uses of pointers

    Quote Originally Posted by razzle View Post
    In general code there shouldn't be any raw pointers in sight at all.
    Thumbs up for this.


    it's ok to use pointers inside the implementation of a class (soemtimes you can't do without, such as in a dynamic container class)
    Exposing pointers outwards is asking for trouble/confusion down the road somewhere because there's always the questions like
    "who owns the data the pointer points to"
    "do I need to free the pointer? and if yes, by what means?"
    "can I safely change the pointer to anything?" the answer can be no, even if it points to valid storage of the right type.
    ...

    Avoiding exposed pointers from your class interfaces will make your classes a lot easier to use by third parties (and yourself X years later).

  9. #9
    Join Date
    Jul 2013
    Posts
    576

    Re: uses of pointers

    Quote Originally Posted by D_Drmmr View Post
    blindly replacing the use of raw pointers with shared pointers
    I still think you're in denial. You're inventing hurdles. Who on earth would blindly replace one construct with another in C++ and expect it to work?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured