Thread: When do I specifically use Pointers?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, I come from PHP where objects are just variables to classes.
    PHP references are like C++ references. PHP5 objects are like Java objects, which behave like pointers without the pointer syntax, e.g., when you pass a PHP5 object as an argument, it is passed as if you are passing a pointer to the object. PHP4 objects are like C++ objects in that they are passed by value by default.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    PHP references are like C++ references. PHP5 objects are like Java objects, which behave like pointers without the pointer syntax, e.g., when you pass a PHP5 object as an argument, it is passed as if you are passing a pointer to the object. PHP4 objects are like C++ objects in that they are passed by value by default.
    makes sense.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobbelPoP View Post
    Another example is, what if you had a game physics engine, and you were trying to define skeleton bone structures ( :P ) for ragdolls, would you store any of that into a pointer?
    Possibly, but you may also find that there are better ways, such as using a std::vector<bone> that contains the bones of that ragdoll. I presume, in this instance, a bone is defined as two end-points in x,y,z [,w] format, and the "meat/cloth" around the bone is built up by the code.

    The biggest point (no pun intended) about pointers is that they can change what the refer to. Yes, you can use pointers to handle dynamic content [that is, hold a variable amount of data], but the generally accepted statement is that "if you can do it with a vector, you should" - and if you think you can't, you'd better have a solid motivation for that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    Possibly, but you may also find that there are better ways, such as using a std::vector<bone> that contains the bones of that ragdoll. I presume, in this instance, a bone is defined as two end-points in x,y,z [,w] format, and the "meat/cloth" around the bone is built up by the code.

    The biggest point (no pun intended) about pointers is that they can change what the refer to. Yes, you can use pointers to handle dynamic content [that is, hold a variable amount of data], but the generally accepted statement is that "if you can do it with a vector, you should" - and if you think you can't, you'd better have a solid motivation for that.

    --
    Mats
    Yeah, but a vector is basically a pointer, so I guess so. :P either way.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobbelPoP View Post
    Yeah, but a vector is basically a pointer, so I guess so. :P either way.
    No it isn't. A vector is a storage class (container class) that allows you to add, remove, store and retrieve data. A pointer is a variable holding an address to something in memory. Whilst that can also allow you to add, remove, store and retrieve data, all of those mechanisms would have to be implemented by you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    No it isn't. A vector is a storage class (container class) that allows you to add, remove, store and retrieve data. A pointer is a variable holding an address to something in memory. Whilst that can also allow you to add, remove, store and retrieve data, all of those mechanisms would have to be implemented by you.

    --
    Mats
    generalization of a pointer. :P

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A generalization of a pointer is an iterator.
    A vector is not a pointer and I would keep them separate.
    A vector is an array, if anything.
    And an array, though similar to a pointer is not a pointer.
    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.

  8. #23

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    References "refer to objects", just like PHP references.
    Pointers just store numeric memory addresses (which can even be 0, aka NULL), and you must use the dereference operator (*) and the address-of operator (&) with pointers, but not with references (it's done implicitly to make the syntax nicer).

    In terms of usage, pointers are really only used in C++ for managing memory. When you want a memory address, consider passing a pointer, when you want an actual object (not just a copy, either), consider passing by reference.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Pointers just store numeric memory addresses (which can even be 0, aka NULL)

    Pointers are not numeric, but you can think of them that way, since they are usually mapped to a numeric addressing method.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or in C++ terms: consider passing by reference if you can, and if not, pass by pointer.
    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.

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    141
    BUMP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Could I just make all the variables/functions, pointers? :P

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Could I just make all the variables/functions, pointers?
    I suppose you could, but that would needlessly increase the complexity of your program with respect to programming and maintenance. Of course, functions are functions, except when they are converted to function pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    I suppose you could, but that would needlessly increase the complexity of your program with respect to programming and maintenance. Of course, functions are functions, except when they are converted to function pointers.
    One of my biggest problems, which probably sounds kinda noobish, even though I understand the concept of WHY it's memory from other programs. :P But why memory?

    Why do pointers use memory?

    :S

    By the way, are classes/structs pointers? cause I remember getting a compiler error that showed it was default by pointer.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do pointers use memory?
    What do you mean by "use"? Pointers "use" memory in the sense that variables use memory. Variables use memory because they store values. Values are stored in memory by design.

    By the way, are classes/structs pointers?
    No, though you can create pointers to class/struct types.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    What do you mean by "use"? Pointers "use" memory in the sense that variables use memory. Variables use memory because they store values. Values are stored in memory by design.


    No, though you can create pointers to class/struct types.
    So basically, non-pointer variables are limited by memory, and pointers are free to use as much memory? so to speak?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  2. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  3. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM