3

Yesterday my teacher told me not to use pointers except if I want to program some really low level stuff. He said the garbage-collector would do everything for me so pointers are basically unefficient and dangerous. I'm irritated because I heard the difference before.

So what is right and what is wrong? Should I use pointers or not? If not, why are they even implemented if nobody uses it?

  • 23
    If you have garbage collection, then you probably don't program in C++. – Some programmer dude Apr 05 '13 at 14:00
  • 3
    So your school teaches the use of C++ with a garbage collector? That can be done, but is not the usual environment you will find yourself working with in the future, I'm afraid. – Frédéric Hamidi Apr 05 '13 at 14:00
  • 3
    "He said the garbage-collector" is this C++? – mfontanini Apr 05 '13 at 14:00
  • 1
    @Joachim, http://www.hpl.hp.com/personal/Hans_Boehm/gc/. – Frédéric Hamidi Apr 05 '13 at 14:01
  • http://stackoverflow.com/questions/2144698/common-uses-for-pointers – DCoder Apr 05 '13 at 14:01
  • @mfontanini I never heard of a garbage collector in c++ but he reassured me that it has one. – Normal People Scare Me Apr 05 '13 at 14:02
  • 2
    Then he's either full of shit/incompetent, or did not communicate a rather important bit of information (that something about your school's setup is nonstandard) well enough. Edit: The latest standard (C++11) actually makes amends for implementation featuring a GC, so "nonstandard" may be technically incorrect, but it's entirely optional and I'm not aware of any implementation doing it. –  Apr 05 '13 at 14:04
  • 2
    C++ does **not** have a garbage collector (at least not a standard one); it can be implemented in it, though. C++/CLI does have one, AFAIK, but that's a separate language. – Angew is no longer proud of SO Apr 05 '13 at 14:04
  • 1
    *sigh* - either you go c++ and use pointers/references as you wish/need or your idom tells you to... or you learn other crappy languages like c# (hint: those are for the weak, and this comment is 100% serious) – Najzero Apr 05 '13 at 14:04
  • 1
    Well I wanna use pointers it's just my teacher, who studied computer science, told me not to use them. – Normal People Scare Me Apr 05 '13 at 14:08
  • 2
    @Najzero No need to even allude to language flame wars, let alone start one. –  Apr 05 '13 at 14:08
  • 6
    It's possible that the teacher was using the term "garbage collection" to refer to the automatic destruction of auto variables. – RichieHindle Apr 05 '13 at 14:08
  • 1
    @RichieHindle He probably did mean that, but it's 100% wrong. – Alex Apr 05 '13 at 14:14
  • 1
    I would suggest you ask your teacher to elaborate on that point, to make sure he was referring to situations where STL containers can be used (e.g. using instead of char*) where memory management is handled for you. Also, you might consider reading [this](http://www.parashift.com/c++-faq-lite/index.html). Lots of good info, including stuff on pointers, references, etc. – quandrei Apr 05 '13 at 14:22
  • 4
    There's certainly a misunderstanding somewhere. If you don't use pointers, garbage collection is irrelevant: there's no garbage to collect. – Pete Becker Apr 05 '13 at 14:52
  • @PeteBecker What about references? Both the C++ kind and the abstract-mutable-handle-for-an-object that other languages call reference. –  Apr 05 '13 at 15:52
  • 2
    @delnan - in order to create garbage you have to allocate from the free store. Functions that allocate from the free store return pointers. If you don't use pointers you can't create garbage. – Pete Becker Apr 05 '13 at 15:55
  • @PeteBecker You're right, good point. –  Apr 05 '13 at 16:09

6 Answers6

16

Using a garbage collector with C++ requires strong discipline and, well, pretty competent programmers, even with the added support for garbage collection in C++11.

So possibly your teacher meant something else.

Like, possibly he/she meant that you should preferentially use standard library containers (like vector, and string), and smart pointers to handle ownership where you have to deal with pointers. That's good advice.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
8

What your teacher said falls into the “almost right” category. This probably means either of two things:

  1. He hasn’t got a clue what he’s talking about and is just repeating catch phrases
  2. He meant the right thing and there was a miscommunication somewhere.

I’m leaning strongly towards option (2) (although clueless teachers are not unheard of):

As others have said, C++ doesn’t usually come with a GC, and while it’s entirely possible to use one in C++ it’s even more unconventional to teach with one.

On the other hand, your teacher is right about the avoidance of pointers. There’s a broad consensus among members of the C++ standardisation committee, users on this very site, and other vocal experts on the internet that modern C++ makes the use of raw pointers (and in particular of manual memory management) largely unnecessary.

Traditionally, most C++ projects were littered with pointers. But raw pointers and manual memory management are error-prone, potentially inefficient (due to the introduction of indirection and cache misses) and, most importantly, they are unnecessary in modern C++.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
6

If you really want to be a good C or C++ programmer, you need to learn and use pointers. In fact, is a very powerful feature with may be risky, yes, but they are absolutely necessary to get some kind of tasks done.

Miguel Prz
  • 13,718
  • 29
  • 42
  • I think the idea is that in C++ most tasks that were only possible using pointers can be done in a different safer way. Such as STL containers instead of arrays, string instead of char*, and pass by reference instead of passing pointer values. – Bee Apr 05 '13 at 15:03
2

Your teacher is right if you are a beginner, but if you get more advanced you will use pointers someday because it makes your program really fast.

The point is that you can easily make misktakes with pointers that have bad concequences (crashing your program for example).

Pointers also have other advantages, for example if you store a raw Object in a Vector and you edit that vector, you need to iterate through that vector each time to find the element you search (slow), or you store pointers to the objects in that vector and use the pointer to that object itself (fast).

There are countless other examples where pointers rule.

Quonux
  • 2,975
  • 1
  • 24
  • 32
  • 1
    Well that's what I answered. – Normal People Scare Me Apr 05 '13 at 14:21
  • 1
    Actually if anything pointers make programs slower: they cause indirection and cache misses. Modern C++ makes manually using pointers almost completely unnecessary. I almost never use them. – Konrad Rudolph Apr 05 '13 at 14:24
  • im sorry, but do you have any small testcode/benchmarks/proofs for that? – Quonux Apr 05 '13 at 14:27
  • @KonradRudolph There are many good reasons to avoid pointers in modern C++, but performance is not one of them. There are plenty other causes of indirection that are perfectly idiomatic (smart pointers are pointers too; several standard containers do lots of pointer-chasing; references that aren't optimized away are also pointers under the hood). Cache misses are often caused by indirection, but pointers aren't the problem, it's data structures where you jump all over memory, which is also possible with via other means, and not the case for some important uses of pointers/indirection. –  Apr 05 '13 at 14:33
  • 1
    @Quonux No – but it’s common knowledge and also actually common sense if you think about it. Do *you* have a proof for your assertion? You’re probably comparing apples and oranges, namely passing pointers versus copying large objects. Of course then pointers would be more expensive, but that’s not a relevant comparison. But take a look at this related discussion: [Want speed? Pass by value](http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/) – Konrad Rudolph Apr 05 '13 at 14:34
  • 2
    @delnan I didn’t say that it was a reason to avoid pointers, merely that it’s *wrong* to say “pointers are more efficient”. I don’t know where this trope started (probably because pointers are “low level” and everybody knows that low-level code is more efficient …) but it has no basis. Simply comparing direct object access and access via a pointer shows that pointer access *must* be slower. That doesn’t make a point about other forms of indirection, nor about their necessity. – Konrad Rudolph Apr 05 '13 at 14:36
  • @KonradRudolph *That* is a very good point, and I agree that the "pointers are more efficient" meme is nonsense. However, that's not what your first comment said. It said: "[I]f anything pointers make programs slower" - which is meaningless without further context - and "[T]hey cause indirection and cache misses" - which has a true core but presents it rather badly. "Don't shoot the message," as they say ;-) –  Apr 05 '13 at 14:46
0

Why are you using managed C++? That seems counter productive, least in my mind. If I'm going to pay the overhead for gc I'll just use C#.

In regards to pointers, they are awesome and shouldn't be overlooked ever. However references are just as useful and you don't have to null check against.

Depending on your compiler look into std::shared_ptr / std::weak_ptr / std::unique_ptr.

They introduce basic retain counting that can be found in most books/languages

Nico
  • 3,826
  • 1
  • 21
  • 31
0

You can write programs in C++ without using pointers. Passing objects by reference is a lot safer.

Many of the containers will require copying objects since you're not allowed to put pointers into the containers. This may be an issue when using large data items or many indices to items.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154