C++ --- how to write the correct code to release the allocated memory for this example?
<p>Based on this <a href="http://sourcemaking.com/design_patterns/command/cpp/1" rel="nofollow">http://sourcemaking.com/design_patterns/command/cpp/1</a>,
I think the posted code has memory leak b/c the allocated memory stored in the array is never released. I made one modification as follows,</p>

<pre><code>#include "stdafx.h"
#include &lt;iostream&gt;
using namespace std;

class Giant
{
public:
    Giant()
    {
        m_id = s_next++;
    }
    void fee()
    {
        cout &lt;&lt; m_id &lt;&lt; "-fee  ";
    }
    void phi()
    {
        cout &lt;&lt; m_id &lt;&lt; "-phi  ";
    }
    void pheaux()
    {
        cout &lt;&lt; m_id &lt;&lt; "-pheaux  ";
    }
private:
    int m_id;
    static int s_next;
};
int Giant::s_next = 0;

// Define a Command interface with a method signature like execute().
class Command
{
public:
    typedef void(Giant:: *Action)();
    Command(Giant *object, Action method)
    {
        m_object = object;
        m_method = method;
    }
    void execute()
    {
        (m_object-&gt;*m_method)();
    }
private:
    Giant *m_object;
    Action m_method;
};

template &lt;typename T&gt; class Queue
{
public:
    Queue()
    {
        m_add = m_remove = 0;
    }
    void enque(T *c)
    {
        m_array[m_add] = c;
        m_add = (m_add + 1) % SIZE;
    }
    T *deque()
    {
        int temp = m_remove;
        m_remove = (m_remove + 1) % SIZE;
        return m_array[temp];
    }
private:
    enum
    {
        SIZE = 8
    };
    T *m_array[SIZE];
    int m_add, m_remove;
};

int main()
{
    Queue&lt;Command&gt; que;
    Command *input[] = 
    {
        new Command(new Giant, &amp;Giant::fee), 
        new Command(new Giant, &amp;Giant::phi),
        new Command(new Giant, &amp;Giant::pheaux), 
        new Command(new Giant, &amp;Giant::fee), 
        new Command(new Giant, &amp;Giant::phi), 
        new Command(new Giant, &amp;Giant::pheaux)
    };

    // 24 / 4 = 6
    size_t arrSize = sizeof(input)/sizeof(input[0]);

    for (int i = 0; i &lt; 6; i++)
        que.enque(input[i]);

    for (int i = 0; i &lt; 6; i++)
        que.deque()-&gt;execute();
    cout &lt;&lt; '\n';

    // I add the following code to release the memory, however I am not sure
    // whether o not this is enough
    // There are two memory allocations in this code
    // 1-&gt; new command and 2-&gt;new Giant
    // I think my code only release the memory allocated by new command.
    // Is this correct?
    for (int i = 0; i &lt; 6; i++)
    {
        delete input[i];
        input[i] = NULL;
    }
}
</code></pre>

<p>Please my question above.</p>

<p>Thank you</p>
