Memory leak in stack I'm implementing a stack by C++ and here I think it has memory leakage problem: in Stack::peek() and Stack::pop(), where I created heap space and returned pointer to function caller.

Users who call these two functions may perform deletion on returned pointer, which will recycle heap space once done. I am wondering what is a better approach to my current leakage prone approach.

Header file:

// this is the header file of stack data structure.

#ifndef MY_STACK_H
#define MY_STACK_H

class Node 
{
private:
    int number;
    Node * next;

public:
    Node(){number = 0; next = NULL;}

    Node(int initialNumber, Node * initialNext = NULL)
    {
        number = initialNumber;
        next = initialNext;
    }

    // copy constructor
    Node(Node &amp; copyFromNode) 
    {
        this-&gt;number    =   copyFromNode.getNumber();
        this-&gt;next      =   copyFromNode.getNext();
    }


    // setters &amp; getters
    int getNumber() {return number;}
    Node * getNext()    {return next;}
    void setNumber(int newNumber)   {number = newNumber;}
    void setNext(Node * newNext)    {next = newNext;}
};

class LinkedList
{
private:
    Node * head;

public:
    LinkedList(){head = NULL;}

    // very similar to Stack::push()
    void addFirst(Node *newNode)
    {
        newNode-&gt;setNext(head);
        head = newNode;
    }

    // somehow similar to Stack::pop()
    void deleteFirst()
    {
        if (head == NULL)
            return;
        else
        {
            Node * temp = head;
            head = head-&gt;getNext();
            delete temp;
        }
    }

    bool isEmpty()      {return (head == NULL);}
    Node * getHead()    {return head;}

    // there is no setter to head 
    // since head should be maintained by addFirst() &amp; deleteFirst() only.
};

class Stack 
{
private:
    Node * top;
    LinkedList ll;
public:
    Stack(){top = NULL; }

    // push new node to the stack
    // new node will become the new top of stack
    void push(Node * newNode);

    // it checks if the stack is empty or not. 
    bool isEmpty();

    // it copies the top node on stack,
    // without deleting it. 
    Node * peek();

    // it copies the top node on stack, 
    // and deletes the node from stack. 
    Node * pop();
};

#endif


.cpp file:

// implementation for the stack


#include &lt;iostream&gt;
#include "stack.h"

using namespace std;

void Stack::push(Node * newNode)
{
    ll.addFirst(newNode);
    top = ll.getHead();
}

bool Stack::isEmpty()
{
    if(top == NULL)
        return true;
    else 
        return false;
}

Node * Stack::peek()
{
    // make a copy, erase next link and return 
    // copy is made by calling copy construtor
    Node * copy = new Node(*top);   // ?? asks for reference but pointer is given

    return copy;
}

Node * Stack::pop()
{
    if (isEmpty())
        return NULL;
    else
    {
        Node * copy = new Node(*top);
        ll.deleteFirst();
        top = ll.getHead();
        return copy;
    }
}

int main()
{
    Stack stack;

    Node * nd1 = new Node(100);
    Node * nd2 = new Node(200);

    // test push()
    stack.push(nd1);
    stack.push(nd2);

    // test isEmpty()
    cout&lt;&lt;"Test: isEmpty(): "&lt;&lt; stack.isEmpty()&lt;&lt;endl;


    // test peek()
    cout&lt;&lt;"Test: peek(): "&lt;&lt;stack.peek()-&gt;getNumber()&lt;&lt;endl;


    // test pop()
    cout&lt;&lt;"Test: pop()"&lt;&lt;stack.pop()-&gt;getNumber()&lt;&lt;endl;
    cout&lt;&lt;"Test: pop()"&lt;&lt;stack.pop()-&gt;getNumber()&lt;&lt;endl;

    cout&lt;&lt;"Test: isEmpty(): "&lt;&lt;stack.isEmpty()&lt;&lt;endl;

    return 0;
}
Don't use NULL unless you have to

Some IDEs will refuse to compile your code unless you include &lt;cstdlib&gt; so that NULL is available. If you compile using a C++ compiler using latest standards, you could use nullptr instead, since it is a language construct in those versions and does not require including anything.

Minor improvements

//bool Stack::isEmpty()
//{
//    if(top == NULL)
//        return true;
//    else
//        return false;
//}

bool Stack::isEmpty()
{
    return top == nullptr;
}


and

//Node * Stack::pop()
//{
//
//    if (isEmpty())
//        return NULL;
//    else
//    {
//        Node * copy = new Node(*top);
//        ll.deleteFirst();
//        top = ll.getHead();
//        return copy;
//    }
//}

Node * Stack::pop()
{
    if (isEmpty())
    {
        return nullptr;
    }

    Node * copy = new Node(*top);
    ll.deleteFirst();
    top = ll.getHead();
    return copy;
}


Alternative implementation

It's not difficult to devise a generic stack:

my_stack.h

#ifndef MY_STACK_H
#define MY_STACK_H

#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;vector&gt;

template&lt;typename T&gt;
class MyStack {

    std::vector&lt;T&gt; storage;
    void check_not_empty()
    {
        if (storage.empty())
        {
            throw std::runtime_error{"Peeking from an empty stack."};
        }
    }

public:
    void push(const T&amp; element)
    {
        storage.push_back(element);
    }

    void pop()
    {
        check_not_empty();
        storage.pop_back();
    }

    const T&amp; peek() const
    {
        check_not_empty();
        return storage[storage.size() - 1];
    }

    size_t size()
    {
        return storage.size();
    }

    bool is_empty()
    {
        return storage.empty();
    }

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const MyStack&lt;T&gt;&amp; stack)
    {
        os &lt;&lt; "[";
        std::string separator = "";

        for (size_t i = 0; i != stack.storage.size(); ++i)
        {
            os &lt;&lt; separator &lt;&lt; stack.storage[i];
            separator = ", ";
        }

        return os &lt;&lt; "]";
    }
};

#endif


main.cpp

#include "my_stack.h"
#include &lt;iostream&gt;

using namespace std;

int main()
{
    MyStack&lt;int&gt; stack;

    cout &lt;&lt; stack &lt;&lt; endl;

    for (int i = 1; i &lt;= 5; ++i)
    {
        stack.push(i);
        cout &lt;&lt; stack &lt;&lt; endl;
    }

    while (!stack.is_empty())
    {
        stack.pop();
        cout &lt;&lt; stack &lt;&lt; endl;
    }
}