Based on this http://sourcemaking.com/design_patterns/command/cpp/1,
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,
#include "stdafx.h"
#include <iostream>
using namespace std;
class Giant
{
public:
Giant()
{
m_id = s_next++;
}
void fee()
{
cout << m_id << "-fee ";
}
void phi()
{
cout << m_id << "-phi ";
}
void pheaux()
{
cout << m_id << "-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->*m_method)();
}
private:
Giant *m_object;
Action m_method;
};
template <typename T> 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<Command> que;
Command *input[] =
{
new Command(new Giant, &Giant::fee),
new Command(new Giant, &Giant::phi),
new Command(new Giant, &Giant::pheaux),
new Command(new Giant, &Giant::fee),
new Command(new Giant, &Giant::phi),
new Command(new Giant, &Giant::pheaux)
};
// 24 / 4 = 6
size_t arrSize = sizeof(input)/sizeof(input[0]);
for (int i = 0; i < 6; i++)
que.enque(input[i]);
for (int i = 0; i < 6; i++)
que.deque()->execute();
cout << '\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-> new command and 2->new Giant
// I think my code only release the memory allocated by new command.
// Is this correct?
for (int i = 0; i < 6; i++)
{
delete input[i];
input[i] = NULL;
}
}
Please my question above.
Thank you
Yes it is correct. But…
Why would you even bother allocating those on the heap? Just create them on the stack, and pass pointers in where needed. Then you will have zero chance of forgetting to deallocate them.
You should look up the RAII idiom. It’s a bad practice in C++ to put a
newanywhere outside a constructor (unless you pass it straight to a smart pointer), and it’s bad practice to put adeleteanywhere outside a destructor. It’s also bad to try to allocate more than one thing at a time, without wrapping each one separately in RAII or a smart pointer.You can make code that will work correctly that doesn’t use RAII/smart pointers. But it will be extremely difficult to do while guaranteeing no memory leaks under any circumstances, especially if an exception is thrown anywhere in your code.
Also, you can use an existing queue data structure rather than rolling your own. I highly recommend that you avoid rolling your own data structures unless you really have to, or you’re specifically trying to learn how a data structure works internally (and you’re not putting that code into production). This code seems to be a demo of member function pointers, so I think the queue implementation is incidental complexity.
Use
std::queueinstead.