about this smart pointer class
<p>I run into this code in a book, a simple smart pointer, have a few questions about it:</p>

<pre><code>    template &lt;class T&gt;
class SmartPointer {
 public:
    SmartPointer(T * ptr) {
        ref = ptr;
        ref_count = malloc(sizeof(unsigned));
        *ref_count = 1;
    }
    SmartPointer(SmartPointer&lt;T&gt; &amp; sptr) {
        ref = sptr.ref;
        ref_count = sptr.ref_count;
        ++*ref_count;
    }
    SmartPointer&lt;T&gt; &amp; operator=(SmartPointer&lt;T&gt; &amp; sptr) {
        if (this != &amp;sptr) {
           ref = sptr.ref;
           ref_count = sptr.ref_count;
           ++*ref_count;
        }
        return *this;
    }
    ~SmartPointer() {
        --*ref_count;
        if (*ref_count == 0) {
           delete ref;
           free(ref_count);
           ref = ref_count = NULL;
        }
    }
    T* operator-&gt;() { return ref; }
    T&amp; operator*() { return *ref; }
 protected:
    T * ref;
    unsigned * ref_count; 
};
</code></pre>

<p>here are my questions:
 1. why is the ref_count initialized by using malloc? why can't it be ref_count = new unsigned();
 2. the = operator function, doesn't it need to clean up the old value? this code seems to cause a ref counting error.</p>

<p>Thanks,</p>
