CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38
  1. #1
    Join Date
    Apr 2009
    Posts
    21

    pointers--when to use them

    I am a self-taught c++ (beginner?) I have been programming for a couple of months.
    I have seen, in tutorials and the such, many uses of pointers. I understand basically what they do, etc. but do you really NEED them?

    Can someone give me examples of cases in which pointers are much preferred to any other type of variable access? As far as I can tell, you might as well just use the variable names.

    Also, I have seen code like this:
    Code:
    int * my_int;
    Why is the * used?

    Thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: pointers--when to use them

    Primarily when you want the object you're creating to exist outside of the scope where it was created and to take advantage of polymorphism.

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: pointers--when to use them

    This:

    int my_int;

    Declares an integer, called my_int.

    This:

    int * my_int;

    Declares a pointer to an integer, called my_int. The * in this context indicates the type created will be a pointer to the type expression that precedes the *.

    Frankly, pointers are often avoidable as raw pointers in modern C++.

    However, the reason they're in the language is the fact that C, the root of C++, was originally implemented to be a kind of assembler, working very close to the level of the underlying CPU, and for that machine, pointers are a fact of it's operation. Languages that don't have pointers, like Java, aren't able to work at that level. Sometimes that's a benefit.

    In the beginning phases of study you can just about ignore the pointer.

    If you're going to handle images, audio, video or other forms of data like them, you're dealing with raw collections of data held in arrays. You can perform that work entirely in containers, and languages without pointers use analogous containers to operate with that data. In C++, however, you can achieve certain levels of performance that approaches the fastest that the underlying hardware can perform precisely because of constructs that work closely at the CPU's level, and in the case of image, audio and video data, that means pointers.

    Another context, already pointed out, where pointers are important is in polymorphism. Java handles this through it's implementation as a reference, but in C++ we can use something called smart pointers to do something very similar. Still, the nature of polymorphic objects is that the type of an object may be more than at first is visible.

    Say I have an object B, derived from A. If a function required an A, but I've created a B which has more functionality, it can still operate as an A. If the function took a COPY of an a, as in:

    void function( A );

    B myb;

    Then:

    function( myb );

    would imply a copy is passed to function. But what about the part of the object that is a B? It can get lopped off.

    If I pass a reference instead:

    void function( A & );

    then

    function( myb );

    would not pass a copy, is passes a reference - which is implemented, under the hood, as a pointer.

    I could also pass a pointer:

    void function( A * );

    function( &myb );

    This time I have to pass the address of myb, but still it's not a copy - and the function gets use of my entire object, including virtual functions in B that may offer unique behavior.

    There's a lot more to it than just that, but this is one example.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    Languages that don't have pointers, like Java, aren't able to work at that level.
    It should be noted that from a certain point of view, all non-primitive objects in Java are referred to via pointers. They shouldn't be all that foreign to a Java programmer once they get over the fear of something different.

    supahamster, if you have 4 or 10 or 15 objects you need to manage, you can get away with individual variable names for each. If you have a thousand objects, you can't.

    One common way of dealing with that many is to throw them in an array. That works, but it has limitations. Fancier data structures may be more applicable. But no matter whether you use an array or something else....you're now referring to 1000 objects in your code with a single variable name.

    Obviously there's no magic about it. That container, whatever it is, simply has a way to refer to all of the various objects internally. Not by individual names----same problem----but via a pointer or series of pointers which are arranged to form the data structure.

    Read up on linked lists as a good example. Or simply realize that the name of an array is a pointer to the start of that array.

  5. #5
    Join Date
    Aug 2007
    Posts
    858

    Re: pointers--when to use them

    Can someone give me examples of cases in which pointers are much preferred to any other type of variable access? As far as I can tell, you might as well just use the variable names.
    The main reasons I can think of offhand:

    A. You need to create an object that can last beyond the local scope.
    B. You need to create a dynamic number of something - you don't know the number until the program is actually running*.
    C. You want to use polymorphism, which essentially requires the use of pointers.

    * You can -and should- make use of things like std::vector in those situations. Ultimately, though, you're still making use of the capabilities of pointers, you just have vector doing all the dirty work for you.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: pointers--when to use them

    Quote Originally Posted by supahamster View Post
    I have seen, in tutorials and the such, many uses of pointers. I understand basically what they do, etc. but do you really NEED them?
    There are many algorithms, data structures and language mechanisms you simply cannot implement efficiently without the use of pointers. One important example is recursive data structures such as lists and trees. Another is parameter passing in functions.

    The two most important uses of pointers are,

    - to avoid extensive shuffling of data back and forth in memory, and
    - to refer to memory allocated on the heap.

    A different question is whether pointers must be an explicit feature of a programming language? Not necessarily. In C++ it is but in Java it is not.
    Last edited by nuzzle; May 28th, 2009 at 01:41 AM.

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Try this - http://learningcppisfun.blogspot.com...-function.html

    It does not cover cross-language viewpoints but just scoped to C++ use of pointers and references - their step brothers - who people think that they hate each other.

    Pointer is just a way to implement a concept - which is "address of an object". How that concept gets implemented in a language depends on what things were considered during the language design. Some have it implemented as references and some using pointers. There may be other ways to implement that concept that I might not be aware of. If you ask for advantages of reference way of doing it as compared to pointers, well, references can go bad as well.

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by nuzzle View Post
    There are many algorithms, data structures and language mechanisms you simply cannot implement efficiently without the use of pointers. One important example is recursive data structures such as lists and trees.
    That is the biggest mental block that I have had for some time in past too and I see that to be a very common one. You ask someone to write a linked list and they will start with a Node structure with a data member and a pointer to next node.

    Pointers are not at all necessary to do that. If you consider the context of my last post above, you can do it using references as well. All you need is a way to refer-to/point-to the next object or to 2 or more nodes in case of tress. Pointers are one way, references are another but well yeah, there has to be a possibility of null references or a way to distinguish that. That is the USP of pointers, I guess, in C++. In C# or Java, the nullable-ness of references overcomes that.
    Quote Originally Posted by nuzzle
    The two most important uses of pointers are,

    - to avoid extensive shuffling of data back and forth in memory, and
    - to refer to memory allocated on the heap.
    Not at all necessary, unless I misunderstood you and if you are willing to let go the USP of pointers as I mentioned above. Here is a way to point/refer to an object on the heap:
    Code:
    int& ref = *(new int(0));
    If geniune null references were possible in C++, who knows, you might not have needed to the derefence I did above. new would have returned a reference directly, instead of a pointer. Interesting thought.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    In addition to NULLability, pointers have one other advantage over references: They can be reassigned.

  10. #10
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: pointers--when to use them

    Quote Originally Posted by Lindley View Post
    In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
    Not to mention pointer arithmetic
    Old Unix programmers never die, they just mv to /dev/null

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: pointers--when to use them

    Quote Originally Posted by exterminator
    In C# or Java, the nullable-ness of references overcomes that.
    I regard references in C# and Java as pointers without pointer syntax. If null C++ references existed, then one benefit of C++ references over pointers would be gone... and references would become pointers with a different syntax. (Okay, and without pointer arithmetic, which admittedly is one difference between C# and Java references and pointers other than syntax.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by Lindley View Post
    In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
    Point taken, but if a reference could be NULL at one time and not NULL at one time, I thought that it would be implicit that references are reassignable. Anyways, that is what I had in mind when I was comparing with Java/C# references and it is just a hypothetical case of talking of changing the way C++ references work, which would be a non-trivial task, I feel.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by HighCommander4 View Post
    Not to mention pointer arithmetic
    Ok.

    Do you mean to say that what can be achieved in C++ with pointer arithematic cannot be achieved in say C#/Java where you don't have pointers and hence pointer arithematic? Any example?

  14. #14
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by laserlight View Post
    I regard references in C# and Java as pointers without pointer syntax. If null C++ references existed, then one benefit of C++ references over pointers would be gone... and references would become pointers with a different syntax. (Okay, and without pointer arithmetic, which admittedly is one difference between C# and Java references and pointers other than syntax.)
    Pointer arithematic only seems a necessity because of the way pointers are modelled in C or C++. When you talk of objectives being achieved with that in C++, are you saying something similar can't be done in say Java or C#? I don't think so. There might be a different way to do it, but surely it would be possible to do it. If you think no, then it would be nice to have an example.

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: pointers--when to use them

    Quote Originally Posted by exterminator
    When you talk of objectives being achieved with that in C++, are you saying something similar can't be done in say Java or C#? I don't think so.
    I did not talk about objectives. Rather, I talked about pointer arithmetic. You simply cannot "increment" or "advance" a reference, or "subtract" references in C# and Java (and C++). Just because you can say, use iterators instead does not change that fact with respect to my assertion that references in C# and Java are pointers without pointer syntax. What I have in mind is that effect where you can pass an object reference to a function, and assigning to that object reference would not change the object reference in the caller since the reference itself is passed by value.
    Last edited by laserlight; May 28th, 2009 at 11:10 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 3 123 LastLast

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