Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
34  
please link to "somewhere". The context might be very relevant. –  user1249 Mar 11 '11 at 6:53
    
This question is hopefully useful for you. –  Garet Claborn Mar 18 '11 at 8:00
    
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. –  Dunk Nov 13 at 22:12

10 Answers 10

up vote 45 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.

share|improve this answer
20  
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 '11 at 14:37
3  
This answer is very limited to smart pointer, which is only a small aspect of the problem. Additionally, smart pointer are not garbage collection. –  deadalnix Jun 7 '12 at 12:37
3  
Also RAII in general. –  Klaim Jun 7 '12 at 13:49
2  
No, this is not what’s meant. It is only one aspect, and not the most important one. –  Konrad Rudolph Sep 1 '12 at 13:20
    
I think smart pointers are the most important aspect, but I agree there are many others. –  DeadMG Sep 1 '12 at 13:36

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.

And, later, novice programmers, can move to "hidden pointers to objects" programming languages like: Java, C#, Object Oriented PHP, and others.

share|improve this answer
17  
C++ is a whole lot more than the "C with Classes" it started out as. –  David Thornley Mar 11 '11 at 20:45
    
Why are you wrapping C++ and C into air quotes? And "hidden", "references" and everything else? Are you a "salesman" and not participating in "programming"? –  phresnel Nov 13 at 10:43
    
I should put them in bold. Quotes can be used to highlight but also to the opposite –  umlcat Nov 13 at 20:06
1  
Don't use backticks for highlighting, they're for inline code. Either use bold or italique. –  CodesInChaos Nov 14 at 10:54

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.

Example:

class MyClass1 {
    public:
        void A (void);
        void B (void);
        void C (void);
    private:
        DWORD dwclass;
};

class MyClass2 {
    public:
        int C (int i);
};

void MyClass1::A (void) {
    MyClass2 *myclass= new MyClass2;
    dwclass=(DWORD)myclass;
}

void MyClass1::B (void) {
    MyClass2 *myclass= (MyClass2 *)dwclass;
    int i = myclass->C(0); // or int i=((MyClass2 *)dwclass)->C(0);
}

void MyClass1::B (void) {
    MyClass2 *myclass= (MyClass2 *)dwclass;
    delete myclass;
}

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.

//-------------------------------------
class CSRecordSet : public CSObject
//-------------------------------------
{
public:
    CSRecordSet();
    virtual ~CSRecordSet();
    // Constructor
    bool Create(CSDataBase* pDataBase,CSQueryDef* pQueryDef);
    //Open, find, close
    int OpenRst(bool bReadBlanks=0,bool bCheckLastSql=0,bool bForceLoad=0,bool bMessage=1);     // for a given SQL
    int FindRecord(bool bNext);         // for a given SQL
    // TABLE must be ordered by the same fields that will be seek
    bool SeekRecord(int nFieldIndex, char *key, int length=0);  // CRT bsearch
    bool SeekRecord(int nFieldIndex, long key);     
    bool SeekRecord(int nFieldIndex, double key, int decimals);     
    bool SeekRecord(XSEK *SEK);     
    bool DeleteRecord(void);
    bool Close(void);
    // Record Position:
    bool IsEOF(void);           // pointer out of bound
    bool IsLAST(void);          // TRUE if last record
    bool IsBOF(void);           // pointer out of bound
    bool IsOpen(void);
    bool Move(long lRows);      // returns FALSE if out of bound
    void MoveNextNotEof(void);  // eof is tested
    void MoveNext(void);        // eof is not tested
    void MovePrev(void);        // bof is tested
    void MoveLast(void);
    void MoveFirst(void);
    void SetAbsolutePosition(long lRows);
    long GetAbsolutePosition(void);
    void GoToLast(void); // Restore position after a Filter
    // Table info
    long GetRecordCount(void);
    int GetRstTableNumber(void);
    int GetRecordLength(void); //includes stamp (sizeof char)
    int GetTableType(void);
    // Field info
    int GetFieldCount(void);
    void GetFieldName(int nFieldIndex, char *pbuffer);
    int GetFieldIndex(const char *sFieldName);
    int GetFieldSize(int nFieldIndex);
    int GetFieldDGSize(int nFieldIndex); // String size (i.e. dg_Boolean)
    long GetRecordID(void);
    int GetStandardFieldCount(void);
    bool IsMemoFileTable(void);
    bool IsNumberField(int nFieldIndex);
    int GetFieldType(int nFieldIndex);
    // Read Field value
    bool GetFieldValue(int nFieldIndex, XdbVar& var);
    bool GetFieldValueIntoBuffer(int nFieldIndex,char *pbuffer);
    char *GetMemoField(int nMemoFieldIndex, char *pbuffer, int buf_size);
    bool GetBinaryField(unsigned char *buffer,long *buf_size);
    // Write Field value
    void Edit(void); // required
    bool SetFieldValue(int nFieldIndex, XdbVar& var);
    bool SetFieldValueFromBuffer(int nFieldIndex,const char *pbuffer);
    bool Update(void); // required
    // pointer to the same lpSql
    LPXSQL GetSQL(void);
};

//---------------------------------------------------
CSRecordSet::CSRecordSet(){
//---------------------------------------------------
    pClass |= (CS_bAttach);
}
CSRecordSet::~CSRecordSet(){
    if(pObject) delete (CXdbRecordset*)pObject;
}
bool CSRecordSet::Create(CSDataBase* pDataBase,CSQueryDef* pQueryDef){
    CXdbQueryDef *qr=(CXdbQueryDef*)pQueryDef->GetObject();
    CXdbTables *db=(CXdbTables*)pDataBase->GetObject();
    CXdbRecordset *rst = new CXdbRecordset(db,qr);
    if(rst==NULL) return 0;
    pObject = (unsigned long) rst;
    return 1;
}
bool CSRecordSet::Close(void){
    return ((CXdbRecordset*)pObject)->Close();
}
int CSRecordSet::OpenRst(bool bReadBlanks,bool bCheckLastSql,bool bForceLoad, bool bMessage){
    unsigned long dw=0L;
    if(bReadBlanks) dw|=SQL_bReadBlanks;
    if(bCheckLastSql) dw|=SQL_bCheckLastSql;
    if(bMessage) dw|=SQL_bRstMessage;
    if(bForceLoad) dw|=SQL_bForceLoad;

    return ((CXdbRecordset*)pObject)->OpenEx(dw);
}
int CSRecordSet::FindRecord(bool bNext){
    return ((CXdbRecordset*)pObject)->FindRecordEx(bNext);
}
bool CSRecordSet::DeleteRecord(void){
    return ((CXdbRecordset*)pObject)->DeleteEx();
}
bool CSRecordSet::IsEOF(void){
    return ((CXdbRecordset*)pObject)->IsEOF();
}
bool CSRecordSet::IsLAST(void){
    return ((CXdbRecordset*)pObject)->IsLAST();
}
bool CSRecordSet::IsBOF(void){
    return ((CXdbRecordset*)pObject)->IsBOF();
}
bool CSRecordSet::IsOpen(void){
    return ((CXdbRecordset*)pObject)->IsOpen();
}
bool CSRecordSet::Move(long lRows){
    return ((CXdbRecordset*)pObject)->MoveEx(lRows);
}
void CSRecordSet::MoveNextNotEof(void){
    ((CXdbRecordset*)pObject)->MoveNextNotEof();
}
void CSRecordSet::MoveNext(void){
    ((CXdbRecordset*)pObject)->MoveNext();
}
void CSRecordSet::MovePrev(void){
    ((CXdbRecordset*)pObject)->MovePrev();
}
void CSRecordSet::MoveLast(void){
    ((CXdbRecordset*)pObject)->MoveLast();
}
void CSRecordSet::MoveFirst(void){
    ((CXdbRecordset*)pObject)->MoveFirst();
}
void CSRecordSet::SetAbsolutePosition(long lRows){
    ((CXdbRecordset*)pObject)->SetAbsolutePosition(lRows);
}
long CSRecordSet::GetAbsolutePosition(void){
    return ((CXdbRecordset*)pObject)->m_AbsolutePosition;
}
long CSRecordSet::GetRecordCount(void){
    return ((CXdbRecordset*)pObject)->GetRecordCount();
}
int CSRecordSet::GetFieldCount(void){
    return ((CXdbRecordset*)pObject)->GetFieldCount();
}
int CSRecordSet::GetRstTableNumber(void){
    return ((CXdbRecordset*)pObject)->GetRstTableNumber();
}
void CSRecordSet::GetFieldName(int nFieldIndex, char *pbuffer){
    ((CXdbRecordset*)pObject)->GetFieldName(nFieldIndex,pbuffer);
}
int CSRecordSet::GetFieldIndex(const char *sFieldName){
    return ((CXdbRecordset*)pObject)->GetFieldIndex(sFieldName);
}
bool CSRecordSet::IsMemoFileTable(void){
    return ((CXdbRecordset*)pObject)->IsMemoFileTable();
}
bool CSRecordSet::IsNumberField(int nFieldIndex){
    return ((CXdbRecordset*)pObject)->IsNumberField(nFieldIndex);
}
bool CSRecordSet::GetFieldValueIntoBuffer(int nFieldIndex,char *pbuffer){
    return ((CXdbRecordset*)pObject)->GetFieldValueIntoBuffer(nFieldIndex,pbuffer);
}
void CSRecordSet::Edit(void){
    ((CXdbRecordset*)pObject)->Edit();
}
bool CSRecordSet::Update(void){
    return ((CXdbRecordset*)pObject)->Update();
}
bool CSRecordSet::SetFieldValue(int nFieldIndex, XdbVar& var){
    return ((CXdbRecordset*)pObject)->SetFieldValue(nFieldIndex,var);
}
bool CSRecordSet::SetFieldValueFromBuffer(int nFieldIndex,const char *pbuffer){
    return ((CXdbRecordset*)pObject)->SetFieldValueFromBuffer(nFieldIndex,pbuffer);
}
bool CSRecordSet::GetFieldValue(int nFieldIndex, XdbVar& var){
    return ((CXdbRecordset*)pObject)->GetFieldValue(nFieldIndex,var);
}
bool CSRecordSet::SeekRecord(XSEK *SEK){
    return ((CXdbRecordset*)pObject)->TableSeek(SEK);
}
bool CSRecordSet::SeekRecord(int nFieldIndex,char *key, int length){
    return ((CXdbRecordset*)pObject)->TableSeek(nFieldIndex,key,length);
}
bool CSRecordSet::SeekRecord(int nFieldIndex,long i){
    return ((CXdbRecordset*)pObject)->TableSeek(nFieldIndex,i);
}
bool CSRecordSet::SeekRecord(int nFieldIndex, double d, int decimals)
{
    return ((CXdbRecordset*)pObject)->TableSeek(nFieldIndex,d,decimals);
}
int CSRecordSet::GetRecordLength(void){
    return ((CXdbRecordset*)pObject)->GetRecordLength();
}
char *CSRecordSet::GetMemoField(int nMemoFieldIndex,char *pbuffer, int BUFFER_SIZE){
    return ((CXdbRecordset*)pObject)->GetMemoField(nMemoFieldIndex,pbuffer,BUFFER_SIZE);
}
bool CSRecordSet::GetBinaryField(unsigned char *buffer,long *buf_size){
    return ((CXdbRecordset*)pObject)->GetBinaryField(buffer,buf_size);
}
LPXSQL CSRecordSet::GetSQL(void){
    return ((CXdbRecordset*)pObject)->GetSQL();
}
void CSRecordSet::GoToLast(void){
    ((CXdbRecordset*)pObject)->GoToLast();
}
long CSRecordSet::GetRecordID(void){
    return ((CXdbRecordset*)pObject)->GetRecordID();
}
int CSRecordSet::GetStandardFieldCount(void){
    return ((CXdbRecordset*)pObject)->GetStandardFieldCount();
}
int CSRecordSet::GetTableType(void){
    return ((CXdbRecordset*)pObject)->GetTableType();
}
int CSRecordSet::GetFieldType(int nFieldIndex){
    return ((CXdbRecordset*)pObject)->GetFieldType(nFieldIndex);
}
int CSRecordSet::GetFieldDGSize(int nFieldIndex){
    return ((CXdbRecordset*)pObject)->GetFieldDGSize(nFieldIndex);
}
int CSRecordSet::GetFieldSize(int nFieldIndex){
    return ((CXdbRecordset*)pObject)->GetFieldSize(nFieldIndex);
}

EDIT: requested by DeadMG:

void nimportequoidumomentquecaroule(void) {

    short i = -4;
    unsigned short j=(unsigned short)i;

}
share|improve this answer
1  
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. –  DeveloperDon Aug 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. –  Mat Aug 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. –  DeveloperDon Aug 25 '12 at 5:38
    
@Mat That example is for a 32 bit OS and the VC6++ compiler. –  Salvador Sep 1 '12 at 10:45
2  
Talking about bad code in an absolutely ancient compiler? No thanks. –  DeadMG Sep 1 '12 at 13:40

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.

share|improve this answer

Since I’m the one who published the polemic “don’t use f*cking pointers” I feel that I should comment here.

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.

share|improve this answer
3  
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 Salvia Sep 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 Beckett Sep 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 Rudolph Sep 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. –  VF1 Nov 13 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 Rudolph Nov 13 at 21:04

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".

That is why they were nearly abandoned in C# and Java.

share|improve this answer
    
Expect they were not "abendoned" in C#. This answer is poor, has horrible spelling, and a horrible inaccurate statement. –  Ramhound Aug 21 '12 at 13:01
1  
They were nearly abandoned. I apologize for not beeing a native speaker. –  k3b Aug 21 '12 at 14:36
    
Hey, we can help with the spelling. Did you mean to say expect or except? –  DeveloperDon Aug 25 '12 at 5:46
1  
Nearly abandoned in C#, you can still enable pointer by specifying unsafe keyword –  linquize Aug 27 '13 at 4:42

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.

share|improve this answer
10  
Ah, finally an everyday example we can all relate to. Killing people. –  Rotem Jun 7 '12 at 12:52

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.

share|improve this answer

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.

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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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