views:

236

answers:

6

So I'm teaching a 2nd semester freshman level C++ course at a university in an upcoming semester. The students have used arrays (though only statically allocated) and have some notion of references and pointers (but probably not much). In general, they have not done a whole lot of dealing with dynamic memory allocation and management. I'm trying to sort of harness the global intelligence of the Stack Overflow community to see, in your collective experience, what have been the most effective ways to teach things like pointers and memory management to young computer science students?

There are a lot of existing interesting StackOverflow posts on related topics:

http://stackoverflow.com/questions/670353/when-teaching-c-is-it-better-to-teach-arrays-before-or-after-pointers

http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome

http://stackoverflow.com/questions/92001/what-is-the-real-difference-between-pointers-and-references

http://stackoverflow.com/questions/660538/should-we-teach-pointers-in-a-fundamentals-of-programming-course

http://stackoverflow.com/questions/1255223/what-are-the-important-notions-in-c-that-you-did-not-learn-from-your-teachers

I certainly have my own set of opinions on how and what I teach, but I'm really interested in how my methodology differs from yours. Some sub-questions to consider (you're certainly not limited to these):

  • What order would you teach things in and how would you relate the topics? "Ordinary" stack variables, followed by references, followed by pointers? Where do arrays fit in? When do you introduce the "new" keyword? etc.

  • What visual aids have you seen used that best express these concepts? e.g. Drawing boxes for memory locations with values inside and variables/pointers as names with arrows pointing to the boxes? Are there any particular websites or textbooks you've read that just have outstanding descriptions?

  • Are there particular code examples (e.g. a "swap" function) that tend to get the information across better than others?

Teach on!

Edit

In an attempt to differentiate this from some of the links I've posted:

Most of the previous SO links I've posted focus themselves very directly on pointers. Though pointers are an integral part of understanding memory behavior, I'm interested in the more overarching themes of how students understand how memory works in general. How do we best illustrate the differences between normal, pointer, and reference declaration? How do we emphasize the differences between global, stack, and heap variables? I think even getting into pushing return addresses onto the call stack is fair game too. What do you think the most important aspects of memory management are, how do you tie them all together, and how do you get this across in a coherent fashion?

+3  A: 

I've seen the blinky video before. Its a bit odd but it does illustrate the point.

monksy
+2  A: 

The best analogy I have found for teaching the concept of pointers is that of the URL. The web page is the memory and the url is the pointer. Every student will be familiar with them and it is a "concrete" example of the indirection.

As for ordering, I would think static allocation followed by pointers and only then references. References, while simpler, are in many ways a special case of pointers and introducing them before requires teaching about most of the pointer concepts.

Steve Rowe
I agree with leaving references out to begin with. References are no more than syntactic sugar for const pointers, so no need to introduce a synonym at this point, you would run the risk to confuse students.
Matthieu M.
A: 

Build a microprocessor. Understanding that memory is laid out linearly (there is no such thing as a 2 dimensional array on most computer architectures and [][] is syntatic sugar), and addressed by a MUX is something I never understood until then. From there everything else builds on this, including how addresses get into the registers, processed by the ALU and put back into memory and/or pushed on the stack.

Jim Wallace
A: 

If someone would hire me to teach memory management, bless their naive hearts, I would start with the heap/stack difference.

Count variable? I'd stack that.
Reading an unknown amount of values? Don't know about you, but I'll throw them on the heap.

Things like that.

If you are teaching just about pointers, it's another game.
I think my teacher used this analogy: A pointer is an address(duh), the value is the house.
I guess anybody can understand at least that much.

I'm always trying to simplify stuff(too much sometimes). Maybe this helps.

Grim Serious
A: 

I'd suggest that you start by discussing object life cycles. How objects are created and how the programmer controls when they're deleted. This will instill an understanding of the the necessary abstractions before you get into the basics of pointers. Life cycles relate to and motivate all of the concepts you've mentioned above: stack based allocation, C++ references, and pointers.

If you think think your students are ready for it, you might discuss smart pointers, as well. If you can teach them to eschew raw pointers as much as possible, you will save them and their future coworkers a good deal of grief.

Rob deFriesse
A: 

Since I've always been a logical person I mostly would like to hear why you need dynamic allocation, not how it works and how the syntax is in a particular language. Start with static allocation, move on to cases when you hit the limitations of it, introduce dynamic allocations. Try to explain the different runtime scopes (compile time, runtime and the border cases). Explain which allocation techniques to use when. For heap allocation you could use shared memory situations and show that it is necessary to have a referencing mechanism, and introduce pointers through that. Show that you cannot determine the lifetime of the memory area easily with shared owner semantics and show different solutions (reference counting, manual deallocation etc) and describe why you need a tradeoff and not just one solution. Use other examples than only memory allocation (e.g RAII) to show that the techniques you show are not limited to memory but to any type of resources.

I think that the key is to build the knowledge from the ground up, and not treat people like idiots (Blinky), but to keep in mind that many people have limited experience in the field, and need concrete examples to grasp why all the features are in place. There are many factors in play, and clearly defining the constraints so the students understand the problem formulation is probably the most important aspect of teaching this type of things.

An example is worth a thousand words, and giving students concrete examples where the things they know so far doesn't cut it enables them to reason about the problem and eventually take the next step.

disown