2
$\begingroup$

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 :)

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Solomon Slow Sep 20 '19 at 17:49
  • 1
    $\begingroup$ 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. $\endgroup$ – ctrl-alt-delor Sep 20 '19 at 20:31
2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Michel Billaud Sep 22 '19 at 8:07
2
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Solomon Slow Sep 20 '19 at 17:24
  • $\begingroup$ @SolomonSlow, the course seems more basic than you suggest. data types, control flows, etc don't get taught in a proper data structures course. $\endgroup$ – Buffy Sep 20 '19 at 17:32
  • $\begingroup$ Hmm. Good point. Although, OP also says "circularly-linked lists," which is kind of an advanced topic for programming 101. $\endgroup$ – Solomon Slow Sep 20 '19 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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