What are the best strategies and examples for teaching C++ memory management to early college students?
<p>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?</p>

<p>There are a lot of existing interesting StackOverflow posts on related topics:</p>

<p><a href="http://stackoverflow.com/questions/670353/when-teaching-c-is-it-better-to-teach-arrays-before-or-after-pointers">http://stackoverflow.com/questions/670353/when-teaching-c-is-it-better-to-teach-arrays-before-or-after-pointers</a></p>

<p><a href="http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome">http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome</a></p>

<p><a href="http://stackoverflow.com/questions/92001/what-is-the-real-difference-between-pointers-and-references">http://stackoverflow.com/questions/92001/what-is-the-real-difference-between-pointers-and-references</a></p>

<p><a href="http://stackoverflow.com/questions/660538/should-we-teach-pointers-in-a-fundamentals-of-programming-course">http://stackoverflow.com/questions/660538/should-we-teach-pointers-in-a-fundamentals-of-programming-course</a></p>

<p><a href="http://stackoverflow.com/questions/1255223/what-are-the-important-notions-in-c-that-you-did-not-learn-from-your-teachers">http://stackoverflow.com/questions/1255223/what-are-the-important-notions-in-c-that-you-did-not-learn-from-your-teachers</a></p>

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

<ul>
<li><p>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.</p></li>
<li><p>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?</p></li>
<li><p>Are there particular code examples (e.g. a "swap" function) that tend to get the information across better than others?</p></li>
</ul>

<p>Teach on!</p>

<h3>Edit</h3>

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

<p>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?</p>
