Title: Class 9: Pointers in Rust
Date: 2014-02-18
Category: Classes
Tags: operating systems, Rust, Morris worm, memory management, NX bit
Author: David Evans

   <div class="todo">
**[Problem Set 3](|filename|../../pages/ps/ps3/ps3.md)** is due on Monday, 3 March.  Everyone should have a team formed for PS3 now and be making progress on it soon!

**Thursday's class will be a project work day.**  Take advantage of this opportunity to coordinate with your team and make progress on PS3.   (Dave is [out of town](http://research.microsoft.com/en-us/events/mpcworkshop/) Wednesday-Friday.  Several of the TAs will come to class Thursday and be available to help with PS3.)
   </div>


<center>
<iframe src="http://www.slideshare.net/slideshow/embed_code/31352780?rel=0" width="512" height="421" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe> <div style="margin-bottom:5px"> <strong> <a href="https://www.slideshare.net/DavidEvansUVa/what-the-lt-pointers-in-rust" title="What the &amp;~#@&amp;lt;!? (Pointers in Rust)" target="_blank">What the &amp;~#@&amp;lt;!? (Pointers in Rust)</a> </strong></div>
</center>

# Unmanaged vs. Managed Memory Management

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/3tSLcJBVL8w?list=PLvpsxlEF9cP1gddrE6oNTpOIr0SBY-rpa" frameborder="2" allowfullscreen></iframe>
</center>

What does it mean for a programming language to have unmanaged memory?
<div class="gap">

</div>

What are different ways to provide management memory?
<div class="gap">

</div>

# Type Safety and Garbage Collection

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/qz1yD-gH82M?list=PLvpsxlEF9cP1gddrE6oNTpOIr0SBY-rpa" frameborder="2" allowfullscreen></iframe>
</center>

Why is it hard to provide automatic memory management in a language that is not type-safe?
<div class="gap">

</div>

Garbage collection for C: Hans-J. Boehm, [_Simple
Garbage-Collector-Safety_](|filename|./pldi96-gc.pdf), PLDI 1996.   Latest version: [https://github.com/ivmai/bdwgc/](https://github.com/ivmai/bdwgc/).

What are the advantages and disadvantages of reference counting compare to mark-and-sweep garbage collection?
<div class="gap">

</div>

## Systematic Memory Management

Paper on willy-nilly memory management: [_Static Detection of Dynamic
Memory Errors_](http://www.cs.virginia.edu/~evans/pldi96-abstract.html)
(PLDI 1996).  The Splint tool is available from
[http://www.splint.org](http://www.split.org).

# Pointers in Rust

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/DnRFFUs3xbc?list=PLvpsxlEF9cP1gddrE6oNTpOIr0SBY-rpa" frameborder="2" allowfullscreen></iframe>
</center>

What does it mean to _borrow_ an object?
<div class="gap">

</div>

What restrictions should there be on what code can do with borrowed objects and why?
<div class="gap">

</div>

What is wrong with this code?

````Rust
fn main() {
   let ref stolen : ~str;

   {
      let mine: ~str = ~"Mine, all mine!";
      stolen = &mine;
   }    

   println!("Whose is it? {:s}", *stolen);
}

````

How does this code establish the lifetime of the result?
````Rust
fn bigger<'a>(s1: &'a str, s2: &'a str) -> &'a str {
    if s1.len() > s2.len() { s1 } else { s2 }
}
````

<div class="gap">

</div>


# Problem Set 3

You should have your team formed for PS3 now.  If you are having trouble forming a team, make sure I know about it by the end of today.

> Les noms zepto et zetta &eacute;voquent le chiffre sept (septi&egrave;me puissance de 10<sup>3</sup>) et la lettre <nobr>&#171; z &#187;</nobr> remplace la lettre <nobr>&#171; s &#187;</nobr> pour &eacute;viter le double emploi de la lettre <nobr>&#171; s &#187;</nobr> comme symbole. Les noms yocto et yotta sont d&eacute;riv&eacute;s de octo, qui &eacute;voque le chiffre huit (huiti&egrave;me puissance de <nobr>10<sup>3</sup>) ;</nobr> la lettre <nobr>&#171; y &#187;</nobr> est ajout&eacute;e pour &eacute;viter l'emploi de la lettre <nobr>&#171; o &#187;</nobr> comme symbole &agrave; cause de la confusion possible avec le chiffre z&eacute;ro.

> [_R&eacute;solution 4 de la 19<sup>e</sup> r&eacute;union de la CGPM_ (1991)](http://www.bipm.org/fr/CGPM/db/19/4/) ([Unofficial English version](http://www.bipm.org/en/CGPM/db/19/4/)).  (Note that I am leaving room for one of you to produce a <b><em>Yohtta</em></b> server.)

<div id="disqus_thread"></div>

<script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'rust-class'; // required: replace example with your forum shortname
	var disqus_url = 'http://www.rust-class.org/class-9-pointer-in-rust.html';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
