<p>In my view, C++ isn't a toy. It is a professional language that should be approached in a professional way. With that in mind, the latest versions are a vast improvement in safety over the earlier versions. </p>

<p>Bjarne Stroustrup once said something like: In C you can shoot yourself in the foot. In C++ you can blow your leg off. </p>

<p>That said, use C++:17 or later and use smart pointers. There is no reason to teach students techniques that are considered obsolete. Find a good book that is compatible with that philosophy, not one that uses a "historical" approach. The past was left behind for a reason. </p>

<p>Alternatively, teach C if you want to teach lower level concepts or Java for higher ones. </p>

<p>I'll note that many of us had to take an historical approach to C++ since we started when it was only a preprocessor for C itself. It has come a long way since then and modern developments have made it safer. </p>

<p>An anecdote might be instructive. I once spent 24 hours finding an allocation bug in a C++ program. The correction required a single character change in the source. Not very productive. Smart pointers would have made such things much better. I now much prefer languages with good garbage collectors to having to solve the memory management problem again (and again) in every program. </p>

CC BY-SA 4.0
2019-09-20T17:24:26.220Z
This would be good answer if the OP's goal was to teach C++, but the OP is teaching data structures. On most days, a modern, professional, C++ programmer has no business messing with data structures. The C++ programmer should be using structures provided by the C++ standard library or, by other common libraries. Programmers who roll their own, most likely are wasting their employer's time and money... Unless they are employed to _implement_ common C++ support libraries.  And, in _that_ case, they probably had better know how to work with raw pointers.
Solomon Slow
CC BY-SA 4.0
2019-09-20T17:32:27.373Z
@SolomonSlow, the course seems more basic than you suggest. data types, control flows, etc don't get taught in a proper data structures course.
Buffy
CC BY-SA 4.0
2019-09-20T17:51:34.077Z
Hmm. Good point. Although, OP also says "circularly-linked lists," which is kind of an advanced topic for programming 101.
Solomon Slow
CC BY-SA 4.0
2019-09-19T19:05:13.740Z
2019-09-20T12:14:04.303Z
2019-09-20T12:14:04.303Z
Buffy
<p>Be careful that you don't create a needless gateway topic. Students should have to wrestle with certain primary issues during every lab experience, but there is little reason to make things besides these primary issues any harder than they need to be.</p>

<p>Thus I largely agree with Buffy, though I would add that if you are specifically teaching a lesson about memory management, it would be perfectly reasonable to switch back to raw pointers <em>for the lab associate with that part of the course.</em> 
 After all, at that point, <code>free</code> is a prime learning target.  </p>

<p>I see little benefit to simply wrestling with memory when the learning targets actually lie elsewhere, and I would not want to put other key concepts at risk for students who have trouble with this one topic.   If they struggle with pointers, but can still master other topics, then that is a positive.  Students can always circle back later to shore up a single weakness if they are ever in a position to directly manage memory, but I wouldn't want to compound this problem more than is necessary.  And, of course, there is a very good chance that they will never need to directly manage memory, as most modern languages do this for you.</p>

CC BY-SA 4.0
2019-09-22T08:07:36.170Z
Question is, what is the learning target for that particular segment of the course? Also, I've never seen a text intended to beginners explaining C++ various types of smart pointers without referring to raw pointers.
Michel Billaud
CC BY-SA 4.0
2019-09-19T22:01:51.310Z
2019-09-19T22:01:51.310Z
Ben I.
<p>I teach an intro to programming course which covers mostly the basics (data types, control flows, functions, arrays, pointers, singly linked lists, circular linked lists, doubly linked lists, without any OOP concepts). The course is taught using C++ but the focus is not on C++ but on the core concepts (the language is just there for implementation, so we only cover the very basics of it).</p>

<p>So far we've been using raw pointers to build linked lists. But I was wondering if students would benefit from switching to smart pointers. I know there are many pros to smart pointers, but I also fear students (who are novice to programming) would "get used" to leaving the cleanup to the compiler, as they wouldn't need to worry about dangling pointers or memory leak problems.</p>

<p>I think the benefits of raw pointers are that students acquire good practices (same reason they don't learn about high-level structures like vectors or maps yet, until they go on the next course and start with OOP). Also I think raw pointers could be a more "standard" way to learn linked lists, since the course doesn't focus on C++ but on the programming fundamentals, and the more C++ specific it gets, the harder it might be to transfer these concepts to a different language.</p>

<p>So for now I've always believed it a good idea to stick to raw pointers, but I don't want to trust my (limited) knowledge on the matter and would like to hear other opinions :)</p>

CC BY-SA 4.0
2019-09-20T17:49:33.797Z
You won't be able to properly implement circularly-linked lists using "smart" pointers. They aren't smart enough for that. You'll either have to use a language with garbage collection, or you'll have to use raw pointers and explicitly `delete` whatever needs to be returned to the heap.
Solomon Slow
CC BY-SA 4.0
2019-09-20T20:31:02.160Z
If you think what you say, then you should have your students program in machine code (not assembler). Higher level languages, will get them into the bad habit of relying on the compiler (to do a better job, than they could ever do). An alternative is to change your belief and use a high level language.
ctrl-alt-delor
<pointers>
Teaching data structures with C++: should I go with smart instead of raw pointers?
# Teaching data structures with C++: should I go with smart instead of raw pointers?

(Asked by: username_0 on 2019-09-19)

I teach an intro to programming course which covers mostly the basics (data types, control flows, functions, arrays, pointers, singly linked lists, circular linked lists, doubly linked lists, without any OOP concepts). The course is taught using C++ but the focus is not on C++ but on the core concepts (the language is just there for implementation, so we only cover the very basics of it).

So far we've been using raw pointers to build linked lists. But I was wondering if students would benefit from switching to smart pointers. I know there are many pros to smart pointers, but I also fear students (who are novice to programming) would "get used" to leaving the cleanup to the compiler, as they wouldn't need to worry about dangling pointers or memory leak problems.

I think the benefits of raw pointers are that students acquire good practices (same reason they don't learn about high-level structures like vectors or maps yet, until they go on the next course and start with OOP). Also I think raw pointers could be a more "standard" way to learn linked lists, since the course doesn't focus on C++ but on the programming fundamentals, and the more C++ specific it gets, the harder it might be to transfer these concepts to a different language.

So for now I've always believed it a good idea to stick to raw pointers, but I don't want to trust my (limited) knowledge on the matter and would like to hear other opinions :)

**Tags:** pointers

> **Comments:**
> **username_1**: You won't be able to properly implement circularly-linked lists using "smart" pointers. They aren't smart enough for that. You'll either have to use a language with garbage collection, or you'll have to use raw pointers and explicitly `delete` whatever needs to be returned to the heap.  
> **username_2**: If you think what you say, then you should have your students program in machine code (not assembler). Higher level languages, will get them into the bad habit of relying on the compiler (to do a better job, than they could ever do). An alternative is to change your belief and use a high level language.  

---

## 2 answers

### Answer by username_3 on 2019-09-19. Score: 2

In my view, C++ isn't a toy. It is a professional language that should be approached in a professional way. With that in mind, the latest versions are a vast improvement in safety over the earlier versions.

Bjarne Stroustrup once said something like: In C you can shoot yourself in the foot. In C++ you can blow your leg off.

That said, use C++:17 or later and use smart pointers. There is no reason to teach students techniques that are considered obsolete. Find a good book that is compatible with that philosophy, not one that uses a "historical" approach. The past was left behind for a reason.

Alternatively, teach C if you want to teach lower level concepts or Java for higher ones.

I'll note that many of us had to take an historical approach to C++ since we started when it was only a preprocessor for C itself. It has come a long way since then and modern developments have made it safer.

An anecdote might be instructive. I once spent 24 hours finding an allocation bug in a C++ program. The correction required a single character change in the source. Not very productive. Smart pointers would have made such things much better. I now much prefer languages with good garbage collectors to having to solve the memory management problem again (and again) in every program.

> **Comments:**
> **username_1**: This would be good answer if the OP's goal was to teach C++, but the OP is teaching data structures. On most days, a modern, professional, C++ programmer has no business messing with data structures. The C++ programmer should be using structures provided by the C++ standard library or, by other common libraries. Programmers who roll their own, most likely are wasting their employer's time and money... Unless they are employed to _implement_ common C++ support libraries.  And, in _that_ case, they probably had better know how to work with raw pointers.  
> **username_3**: @SolomonSlow, the course seems more basic than you suggest. data types, control flows, etc don't get taught in a proper data structures course.  
> **username_1**: Hmm. Good point. Although, OP also says "circularly-linked lists," which is kind of an advanced topic for programming 101.  

---

### Answer by username_4 on 2019-09-19. Score: 2

Be careful that you don't create a needless gateway topic. Students should have to wrestle with certain primary issues during every lab experience, but there is little reason to make things besides these primary issues any harder than they need to be.

Thus I largely agree with username_3, though I would add that if you are specifically teaching a lesson about memory management, it would be perfectly reasonable to switch back to raw pointers *for the lab associate with that part of the course.*
After all, at that point, `free` is a prime learning target.

I see little benefit to simply wrestling with memory when the learning targets actually lie elsewhere, and I would not want to put other key concepts at risk for students who have trouble with this one topic. If they struggle with pointers, but can still master other topics, then that is a positive. Students can always circle back later to shore up a single weakness if they are ever in a position to directly manage memory, but I wouldn't want to compound this problem more than is necessary. And, of course, there is a very good chance that they will never need to directly manage memory, as most modern languages do this for you.

> **Comments:**
> **username_5**: Question is, what is the learning target for that particular segment of the course? Also, I've never seen a text intended to beginners explaining C++ various types of smart pointers without referring to raw pointers.  

---

