Pointers Help

This is a discussion on Pointers Help within the C++ Programming forums, part of the General Programming Boards category; What are they used for? I know how to use them, but dont quite know what sort of practical uses ...

  1. #1
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86

    Question Pointers Help

    What are they used for? I know how to use them, but dont quite know what sort of practical uses there are for them.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Dynamic data structures like linked lists and binary trees are best made with pointers. You can use pointers to pass big objects around without worrying about copying them. You can use pointers to get to the hardware directly and work with it. With the STL you can use pointers to avoid code bloat by having containers of pointers instead of objects. There's a lot of practical use for pointers, most of them are harder to see because C++ tries to give you abstractions which hide them.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There are tons of uses for pointers!

    Lets say you have a struct or class that is very large. Maybe say, 50 bytes. What if you need to pass this to a function? You may do it like this..

    Code:
    int myFunction( myClass food )
    {
      // .. blah
      return 0;
    }
    Well this has a lot of overhead. It actually creates a copy of the class to let the function use! So it copies a 50 byte structure or class then if the function changes anything it doesn't effect the original copy! A much better way to do it would be to pass a pointer to the original class..

    Code:
    int myFunction( myClass *pFood )
    {
      // ..blah
      return 0;
    }
    This way you only put 4bytes for the pointer on the stack and don't waste time with a copy constructor and so on.

    Thats just a single example. What if you need to allocate space at run-time to hold information? You might want to use a pointer for that. The more advanced you get the more and more you will use pointers. Lots of the Win32 API functions require you to pass a pointer to a variable into the function and the function will fill in the information for you.

    What if you wanted to write your own string class. Well say you wanted to write a function to determine the length of a string. Simple, get a pointer to the character array and walk through it until you find the end of string character. Pointer arithmetic is great.

    What about linked lists? Inside a "node" you store a pointer to another node.

    Well hopefully that will give you a little idea of a few uses for pointers.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: Pointers Help

    Originally posted by LordVirusXXP
    What are they used for? I know how to use them, but dont quite know what sort of practical uses there are for them.
    It's great that you know how to use them.
    Just wait untill you learn, some new data structured, like linked list, trees, etc...
    none...

  5. #5
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    Hmm... I think I'm starting to see what they can do.
    They can point to variables and classes and things, and also tell where they're located. Other than that, I dont know exactly what the do, but I'm pretty certain that they have things to do with memory allocation.
    - Dean

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pointers are variables that holds the representation of an address in memory similar to an int that holds the representation of an integer in memory or a char which holds the representation of a character in memory, or whatever. The compiler knows how much memory is being pointed to by the pointer because you declared the pointer with a type name, but the pointer doesn't know how much memory the object uses, just where the memory block for the object starts. Likewise the pointer doesn't know what the value of the oject stored at that location is, although the compiler has kept track of it and can tell you if you dereference the pointer.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Return more than one value

    I'd say that the most common use of pointers is to "get around" the fact that a function can only retrn one value. With pointers your functin can affect as many variables as you want!

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    For me, in order, the uses are:

    1)Inheritance and polymorphism -- Being able to refer to objects of a common base type on like terms and still being able to use the unique capabilities of each. I list this use above the rest primarily because I use it in conjunction with all of the others almost all of the time

    2)Dynamic memory allocation -- Storing the location of objects created dynamically at runtime. This also goes along with data structures

    3)Being able to create "connections" between objects -- Often times the functionality of an object or many objects depend on the state of another object. You usually need pointers to make that relationship and be able to change it at runtime

    4)Passing by reference -- More of a necessity than the previous three and I probaqbly do even use it more (IE everytime a member function is celled, heh), but I still hold the others of higher value, mainly because they are less fundamental
    Last edited by Polymorphic OOP; 12-23-2002 at 04:15 PM.

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, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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