Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:53:21+00:00 2026-05-17T23:53:21+00:00

Based on this http://sourcemaking.com/design_patterns/command/cpp/1 , I think the posted code has memory leak b/c

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:53:22+00:00Added an answer on May 17, 2026 at 11:53 pm

    I think my code only release the memory allocated by new command. Is this correct?

    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.

    Giant giant;
    std::vector<Command> input;
    input.push_back(Command(&giant, &Giant::fee));
    input.push_back(Command(&giant, &Giant::phi));
    input.push_back(Command(&giant, &Giant::pheaux));
    input.push_back(Command(&giant, &Giant::fee));
    input.push_back(Command(&giant, &Giant::phi));
    input.push_back(Command(&giant, &Giant::pheaux));
    
    // ...
    
    for (int i = 0; i < input.size(); i++)
        que.enque(&input[i]);
    

    You should look up the RAII idiom. It’s a bad practice in C++ to put a new anywhere outside a constructor (unless you pass it straight to a smart pointer), and it’s bad practice to put a delete anywhere 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::queue instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.