Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; I can't tell when I actually need it, and when I can only think of a pointer is when it ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Pointers

    I can't tell when I actually need it, and when I can only think of a pointer is when it just needs a custom value ( through memory ), to be accessed or modified, and I know its like slot holders of memory and your just adding it onto those slot holders.

    but it just doesnt seem much sensible to me.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    If you have not already done so, then perhaps you would like to read the tutorial on pointers. It has a section with some explanation on pointer usage.
    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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    The biggest advantages of pointers are (from my perspective, anyway):
    - Having multiple points in your program using the same data, and not copies of the same data. It's important in both maintenance and speed, so you don't have to update all the data when one of them changes.
    - Avoiding unnecessary copying (copying a pointer is much, much faster rather than copying an object).
    - Can be reassigned (if you compare to references). Means you can dynamically update pointers as situations where they should point to changes (such as new data is added/updated).
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    141
    So a pointer holds unlimited data, and can modify which I was right about.

    And thinking more about the concept of deposit boxes, so like.. a memory addess is the deposit boxes number ( labeled probably on the front of the box ), and inside holds unlimited storage? eh?

    Hmm, but why do you hold unlimited data, I dont understand about that, why not just use a vector to hold each element? :P like vector<datatype>::iterator or something?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    The biggest advantages of pointers are (from my perspective, anyway):
    - Having multiple points in your program using the same data, and not copies of the same data. It's important in both maintenance and speed, so you don't have to update all the data when one of them changes.
    - Avoiding unnecessary copying (copying a pointer is much, much faster rather than copying an object).
    - Can be reassigned (if you compare to references). Means you can dynamically update pointers as situations where they should point to changes (such as new data is added/updated).
    Didn't see your post, :P also thanks that helped some more. actually, this means that pointers are just dynamic variables yes?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    I suppose it means pointers are dynamic variables and holds an address to a storage box, ya
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    So a pointer holds unlimited data, and can modify which I was right about.
    A pointer contains an address. This address can be the starting address of an arbitrarily large object, but the pointer itself does not hold any data other than the address.

    And thinking more about the concept of deposit boxes, so like.. a memory addess is the deposit boxes number ( labeled probably on the front of the box ), and inside holds unlimited storage? eh?
    Of course, just as there is no deposit box that is infinitely large, there is no (man-made) computer with an unlimited amount of memory. But yes, this analogy is more appropriate than just saying that "a pointer holds unlimited data".

    why not just use a vector to hold each element? :P like vector<datatype>::iterator or something?
    A vector is typically implemented with a pointer. Iterators are a generalisation of pointers.
    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

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    A pointer contains an address. This address can be the starting address of an arbitrarily large object, but the pointer itself does not hold any data other than the address.


    Of course, just as there is no deposit box that is infinitely large, there is no (man-made) computer with an unlimited amount of memory. But yes, this analogy is more appropriate than just saying that "a pointer holds unlimited data".


    A vector is typically implemented with a pointer. Iterators are a generalisation of pointers.
    Awesome! C++ seems more and more easier.. Although I haven't messed with the more advanced parts, I have done PHP ( advanced ) and Python ( intermediary ) before though, does that help me with C++?

    So to double check thoroughly cause Pointers seem to range everywhere, Pointers just hold a memory address which that memory address links back to memory storage which holds unlimited data inside there? Thats why it has the deposit concept.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    To be general, pointers just contain a memory address that can point anywhere. That is why a lot of crashes stems for pointers, as they can hold an invalid memory address.
    However, the use of pointers is typically to point them at a storage somewhere. A storage box can be created in a number of different ways.

    But you do have the general idea of what a pointer is for.
    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.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    To be general, pointers just contain a memory address that can point anywhere. That is why a lot of crashes stems for pointers, as they can hold an invalid memory address.
    However, the use of pointers is typically to point them at a storage somewhere. A storage box can be created in a number of different ways.

    But you do have the general idea of what a pointer is for.
    Yes, and if not, I will just read over that tutorial and these posts a dozen times.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    I have done PHP ( advanced ) and Python ( intermediary ) before though, does that help me with C++?
    Yes and no. The problem solving skills that you acquired when using those programming languages will help you with C++. Likewise, your understanding of some programming concepts will also help. On the other hand, you may carry with you ideas that are not so suitable in C++, so you would need to adapt.

    So to double check thoroughly cause Pointers seem to range everywhere, Pointers just hold a memory address which that memory address links back to memory storage which holds unlimited data inside there? Thats why it has the deposit concept.
    Yes, though I say again: do not get too stuck with the idea of "unlimited data". Storage is limited, whether it be cache, main memory, or the hard disk.
    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
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by laserlight View Post
    Yes and no. The problem solving skills that you acquired when using those programming languages will help you with C++. Likewise, your understanding of some programming concepts will also help. On the other hand, you may carry with you ideas that are not so suitable in C++, so you would need to adapt.


    Yes, though I say again: do not get too stuck with the idea of "unlimited data". Storage is limited, whether it be cache, main memory, or the hard disk.
    Thanks, I'll just study Pointers more, but one more thing, can it get into complexity with Pointers? Cause I have heard of Function Pointers, though I haven't really heard much..

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    can it get into complexity with Pointers?
    If you are asking whether working with pointers can be complex, then the answer is yes, which is why we avoid using them by using containers and smart pointers instead.

    Cause I have heard of Function Pointers, though I haven't really heard much..
    You might want to read these tutorials on function pointers.
    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

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    Functions and code, just like anything else is stored in memory and is therefore "addressable."
    As such, you can get pointers that points to functions, aka function pointers. Commonly used in C. In C++, there's also a more complex type of pointer for class member functions.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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