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++?

Most of these answers refer to avoiding memory leaks as the primary reason. I can't remember the last time one of our apps had a memory leak issue despite using pointers. If you have memory leak issues then you aren't using the right tools or you don't know what you are doing. Most development environments have a way to automatically check for leaks built in. I think memory leak issues are far more difficult to track down in garbage collected languages because their occurrence is far more subtle and you frequently need a 3rd party tool to track down the culprit.
–
DunkNov 13 '14 at 22:12

1

Adding to @Dunk 's comment, sometimes built-in garbage collectors in higher-level languages simply do not work right. ActionScript 3's garbage collector doesn't, for instance. There's a bug in it right now that has to do with NetConnection instances disconnecting from the server (stackoverflow.com/questions/14780456/…), as well as an issue with there being multiple objects in a program that it will specifically refuse to ever collect ...
–
PanzercrisisJan 7 at 13:54

... (adobe.com/devnet/actionscript/learning/as3-fundamentals/… - search for GCRoots are never garbage collected. and the paragraph started by The MMgc is considered a conservative collector for mark/sweep.). Technically this is a problem in Adobe Virtual Machine 2, not AS3 itself, but when you have problems like this in higher-level languages, which have garbage collection essentially built in, you often don't have any true way within the language to debug these issues completely out of the program. ...
–
PanzercrisisJan 7 at 13:59

10 Answers
10

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.

First of all, as a polemic it obviously represents an extreme viewpoint. There are definitely legitimate uses of (raw) pointers. But I (and many professional C++ programmers) maintain that these cases are exceedingly rare. But what we really mean is the following:

First:

Raw pointers must under no circumstances own memory.

Here, “own memory” essentially means that at some point delete is called on that pointer (but it’s more general than that). This statement can safely be taken as an absolute. The only exception is when implementing your own smart pointer (or other memory management strategy). And even there you should normally still use a smart pointer at low level.

The rationale for this is quite simple: raw pointers which own memory introduce a source of error. And these errors are prolific in existing software: memory leaks and double deletion – both a direct consequence of unclear resource ownership (but going in opposite direction).

This problem can be entirely eliminated, at virtually no cost, by simply using smart pointers instead of raw pointers (caveat: this still requires thinking, of course; shared pointers can lead to cycles and thus once again to memory leaks – but this is easily avoidable).

Second:

Most uses of pointers in C++ are unnecessary.

Unlike other languages, C++ has very strong support for value semantics and simply doesn’t need the indirection of pointers. This wasn’t immediately realised – historically, C++ was invented to facilitate easy object orientation in C, and relied heavily on constructing object graphs which were connected by pointers. But in modern C++, this paradigm is rarely the best choice, and modern C++ idioms often don’t need pointers at all. They operate on values rather than pointers.

Unfortunately, this message has still not caught on in large parts of the C++ user community. As a result, most of the C++ code that is written is still littered with superfluous pointers which make the code complex, slow and faulty / unreliable.

For somebody who knows modern C++, it’s clear that you very rarely need any pointers (either smart or raw; except when using them as iterators). The resulting code is shorter, less complex, more readable, often more efficient and more reliable.

Sigh... this should really be the answer with 30+ upvotes... especially for the second point. Pointers are just rarely even necessary in modern C++.
–
Charles SalviaSep 1 '12 at 14:21

1

what about eg. Gui object owns a bunch of doc objects. It has these as pointers both so the class can be forward declared (avoids recompiles) and so that the object can be initialised when created (with new) rather than being created on the stack in some empty state and then filed-in later? This seems a perfectly valid use of pointers - shouldn't all encapsulated objects be on the heap?
–
Martin BeckettSep 1 '12 at 16:42

3

@Martin GUI objects are one case where pointer object graphs are indeed the best solution. But the edict against memory-owning raw pointers still stands. Either use shared pointers throughout or develop a proper ownership model and have only weak (= non-owning) relations between the controls via raw pointers.
–
Konrad RudolphSep 1 '12 at 17:11

What about PODs of moderate size which need to be stored in a std container, without ptr_vec? I've been in a situation where the container (vector for a heap) needed fast inter-element swaps. This was also in a hot section, so shared ptrs wouldn't do.
–
VF1Nov 13 '14 at 20:58

@VF1 std::unique_ptr. Also, why not ptr_vec? But usually a value vector with will still swap faster (especially with move semantics).
–
Konrad RudolphNov 13 '14 at 21:04

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.

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.

Relatively simply, the C mentality is "Got a problem? Use a pointer". You can see this in C strings, function pointers, pointers-as-iterators, pointer-to-pointer, void pointer- even in the early days of C++ with member pointers.

But in C++ you can use values for many or all of these tasks. Need a function abstraction? std::function. It's a value that's a function. std::string? It's a value, that's a string. You can see similar approaches all over C++. This makes analyzing the code vastly easier for both humans and compilers.

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.

If you were a soldier you would have, say, an assault rifle and a knife.

It is easier and safer to kill people with the assault rifle than it is with the knife so people will tell you to use that when you can.

However, sometimes you have to use the knife. If you are careful you can sneak up, kill your target and sneak away again safely but the potential for things going wrong is much higher.

So use the rifle when you can and use the knife when you have to.

The same is true of references (rifle) and pointers (the knife).

Disclaimer:
This analogy is presented as is with no warranties of any kind. It is not guaranteed to by logically proof against absurdities nor can is necessarily be sensibly extended to cover anything other than the author's original intent.

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

C++ supports most of C, features, plus Objects and Classes. C already had pointers and other stuff.

Pointers are a very useful technique, that can be combined with Object Orientation, and C++ supports them. But, this technique, is difficult to teach and difficult to understand, and, its very easy to cause unwanted errors.

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". These "hidden pointer" techniques are called "references".

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 ( A.K.A. reference) ;-)

In that order.

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

Talking about VC6, when you cast a pointer of a class (that you instantiate) into a variable (e.g. DWORD), even if this pointer is local you can access the class over all the functions that use the same heap. The instantiated class is defined as local but in fact it is not.
As far as I know, any address of a heap variable, structure or class is unique along all the life of the hosting class.

EDIT Thats a very little part of the original code. The CSRecodset class is only a casting class of CXdbRecordset, where all the real code is. Doing so I can let the user take benefit of what I wrote without loosing my rights. I do not pretend to demonstrate that my database engine is professional but it really works.

This description could be enhanced significantly by some code to illustrate what you are describing. I have a sense that it related to the original question, but if you warned us of a danger in this scenario, it would help elaborate the questioner's topic.
–
DeveloperDonAug 20 '12 at 17:59

That cast to DWORD is abusive and possibly incorrect (DWORD isn't necessarily wide enough to hold a pointer). If you need an untyped pointer, use void* - but when you find yourself needing that in C++, you've often got a design problem in your code you should fix.
–
MatAug 21 '12 at 3:41

Salvador, I think you are trying to say something about VC6 and how it has unusual and unexpected pointer handling. The example could benefit by comments, and if you are seeing warnings from the compiler, they might be informative with regard to relating your answer to the question.
–
DeveloperDonAug 25 '12 at 5:38

@Mat That example is for a 32 bit OS and the VC6++ compiler.
–
SalvadorSep 1 '12 at 10:45