up vote 8 down vote favorite
2
share [fb]

I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the better alternative and approach in C++?

link|improve this question

15  
please link to "somewhere". The context might be very relevant. – Thorbjørn Ravn Andersen Mar 11 at 6:53
This question is hopefully useful for you. – Garet Claborn Mar 18 at 8:00
feedback

7 Answers

up vote 27 down vote accepted

I think they mean you should use smart pointers instead of regular pointers.

In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the misuse of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.

The misuse of pointers is a major source of bugs: the constant allocation, deallocation and referencing that must be performed by a program written using pointers introduces the risk that memory leaks will occur. Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer (or the last in a series of pointers) to an object is destroyed, for example because it goes out of scope, the pointed object is destroyed too.

In C++ the emphasis would be on garbage collection and preventing memory leaks (just to name two). Pointers are a fundamental part of the language, so not using them is pretty much impossible except in the most trival of programs.

link|improve this answer
6  
typically its not strictly garbage collection in the classical sense, more reference counting. At least in the smart ptr I'm used to ([boost|std]::shared_ptr) – Doug T. Mar 11 at 14:37
feedback

Simply because there are abstractions available to your which hide the more temperamental aspects of using pointers, such as access to raw memory and cleaning up after your allocations. With smart pointers, container classes, and design patterns like RAII, the need for using raw pointers is diminished. That said, like any abstraction, you should understand how they actually work before moving beyond them.

link|improve this answer
feedback

One of reasons is too wide application of pointers. They can be used for iteration over containers, for avoiding copying large objects when passing to function, non-trivial life-time management, accessing to random places in memory, etc. And once you used them for one purpose, other their features become available immediately independently on intent.

Selection of a tool for exact purpose makes code simpler and intent more visible - iterators for iterations, smart pointers for life-time management, etc.

link|improve this answer
Thanks. Very good insights. – jpartogi Mar 11 at 7:15
feedback

Beside the risk of memory leaks stated by @jmquigley pointer and pointeraritmetics can be considered problematic because pointers can point everywhere in memory causing "hard to find bugs" and "security vulnerableties".

Thats wy they were nearly abendoned in c# and java.

link|improve this answer
feedback

Besides the reasons already listed, there is an obvious one: better optimisations. Aliasing analysis is far too complicated in presense of a pointer arithmetics, whereas references hints an optimiser, so a much deeper aliasing analysis is possible if only references are used.

link|improve this answer
feedback

You can perfectly use pointers in C++, but you shouldn't if your intend is to use object oriented programming (which is not always the case when you use C++), because pointers let you bypass all the object oriented features, like attribute or method privacy, and so you can use what ever object you want, which is bad in OOP. So if you use pointers, you're not doing OOP anymore.

link|improve this answer
4  
Pointer casting lets you bypass the object-oriented features. Pointers themselves are quite compatible with OOP. – dan04 Mar 11 at 13:20
5  
C++ polymorphism depends on pointers. There is absolutely no point in the keyword virtual without pointers. – David Thornley Mar 11 at 14:48
2  
How else do you plan on using object oriented programming? And how do pointers bypass object oriented features? All object oriented programming that I've ever seen relies on pointers (even if they're hidden). Also, #define private public undermines OOP more than any pointers I've seen. – jsternberg Mar 15 at 14:11
feedback

"C++" is "C" plus Objects and Classes. "C" already had pointers and other stuff. Pointer usage is difficult, its very easy to create errors / exceptions, and is difficult to teach and difficult to understand.

Many new programming languages pretend not to use pointers with objects, like Java, .NET, Delphi, Vala, PHP, Scala. But, pointers are still used, "behind the scenes".

Anyway, I consider "pointer (s)" as a Programming Pattern, as a valid way to solve certain problems, as well as Object Oriented Programming does.

Other developers may have a different opinion. But, I suggest students and programmers learn how to:

(1) Use pointers without objects

(2) objects without pointers

(3) explicit pointers to objects

(4) "hidden" pointers to objects ("references") ;-)

Even if is difficult to teach, and diffcult to learn. Object Pascal (not Delphi) and C++ (not Java or C#) can be used for those goals.

link|improve this answer
3  
C++ is a whole lot more than the "C with Classes" it started out as. – David Thornley Mar 11 at 20:45
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.