C++: Why to use pointers?

Started by
28 comments, last by BeanDog 18 years, 4 months ago
Hello there. I sat down the other day wondering, WHY to use pointers. I know how to code with pointers, and my code works the same way without them. Can somebody provide a practical example where it's necissary to use a pointer? :)
Advertisement
Obvious example - call by reference and call by value when passing parameters to functions.

Say you've got a hefty object that needs to be accessed by a function. If you use call by value, you're effectively creating a new copy of that object, wasting memory (and even then you'll only be accessing the copied object, and not the original).
By using call by reference, and passing a pointer to the object, not only can you access the original object, but you're saving, potentially, a massive chunk of memory as well. :)

James
In C++, pointers are useful mainly in three situations:


  1. To interface with C code which uses them (strings, pass-by-reference, arrays) and with dynamic memory allocation (operator new and operator delete).

  2. To implement reseatable references (or optional content represented by a NULL) pointer.

  3. For low-level arithmetic operations on memory addresses (computing offsets, moving to offsets, and similar tasks).



Please note that in case 1 pointers serve only as an interface (and should be quickly converted to a more adapted type), and that in case 2, pointers can be replaced by smart pointers. Case 3 should be limited to only a small portion of any application, and not allowed to permeate outside a single, well-encapsulated module.

However, clean C++ code should not use plain pointers for something other than the above. For instance, call-by-reference should be implemented using references, not pointers, except in case 1 above.
Quote:Original post by BeatOne
Hello there. I sat down the other day wondering, WHY to use pointers. I know how to code with pointers, and my code works the same way without them. Can somebody provide a practical example where it's necissary to use a pointer? :)


Functions, classes... anything, really! It's not always a good idea to invoke an object's copy constructor, especially if the object is big... Oh, and also - linked lists ;).

C++
Not only does it waste memory, but it wastes CPU time (which can quickly add up) as that extra memory has to be allocated and then deallocated. You want to minimize memory allocations as much as possible as they can take a serious toll on performance.

Also, I understand that you are from Sweden, but: "C++: Why should I use pointers?", "C++: Why would I use pointers?", "C++: Why would you use pointers?", "C++: Why should you use pointers?" would all be improvements. Happy posting [smile]
Programming since 1995.
if you use any std container, you are indirectly using pointers. same with polymorphism. i find plenty of uses for pointers...
Let me see if I got that right.. Instead of making a new variable(for example), I pass the adress of the original variable, to use that instead?
Quote:Original post by uncle albert
Obvious example - call by reference and call by value when passing parameters to functions.

Say you've got a hefty object that needs to be accessed by a function. If you use call by value, you're effectively creating a new copy of that object, wasting memory (and even then you'll only be accessing the copied object, and not the original).
By using call by reference, and passing a pointer to the object, not only can you access the original object, but you're saving, potentially, a massive chunk of memory as well. :)

James


He's using C++, in which you can pass by reference without using a pointer.

If your code works fine without pointers, then you don't need them. One out of the many examples where pointers(either raw C++ pointers or STL constructs like smart pointers) have a use is when you want to store a reference to an object. Say you are making an FPS, and you want to be able to "lock" a specific target and kill him when you shoot:

class CPlayer{private:  CMonster *target;public:  CPlayer(){target=NULL;}  void LockTarget(CMonster &monster){target=&monster;}  void Shoot()  {    if (target!=NULL)      {       target->Die();       target=NULL;     }  }};
T1Oracle: Sorry about my bad spelling, I am busy chasing polarbears down the streets.. :)

No seriously, I still don't understand the major advantage using pointers, other than memoryefficient..?
mikeman: Thanks, that example was good. :)

This topic is closed to new replies.

Advertisement