0

I have been learning and playing around C++ (mostly, pointers and dynamic memory allocation) for few days and I tried to create a generic class for linked list.

The classes

#include <cstdint>

#define _LINKEDLIST_DEFAULT_MAX_SIZE 2147483647L

template <typename T>
class LinkedList;

template <typename T>
class LinkedListNode;

template <typename T>
class LinkedListNode final
{
private:
    LinkedListNode<T> *nextNode{nullptr};
    friend LinkedList<T>;

public:
    T data{};
};

template <typename T>
class LinkedList final
{
private:
    LinkedListNode<T> *firstNode{nullptr};
    std::int32_t maxLength{};
    std::int32_t currentLength{};

public:
    LinkedList(std::int32_t max_size = _LINKEDLIST_DEFAULT_MAX_SIZE)
    {
        maxLength = max_size;
    }

    void addFirst(LinkedListNode<T> *nodePtr)
    {
        if (firstNode == nullptr)
        {
            firstNode = nodePtr;
            return;
        }
        nodePtr->nextNode = firstNode;
        firstNode = nodePtr;
    }

    void clerList()
    {
        // code of releasing occupied heap memory back
    }
}

Main method

int main()
{    
    LinkedList<short> *head{new LinkedList<short>()};
    LinkedListNode<short> *node1{new LinkedListNode<short>()};
    LinkedListNode<short> *node2{new LinkedListNode<short>()};

    node1->data = 1;
    node2->data = 2;

    head->addFirst(node1);
    head->addFirst(node2);

    return 0;
}

And this works as properly so far as variables in my debugger shows expected results. But my issue is how could I write my clearList() method on LinkedList<T> class? I can traverse through LinkedListNode<T> objects and release their memory back calling delete(), but calling delete(this) from clearList() to release back the memory of LinkedList<T> object at first sounds like suiciding since it tries to delete the object which it belongs to. (Note that some simple validation logics have not yet been put into the code)

Do you have any ideas to make this happen :)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 3
    `delete this` can be done under certain circumstances but does require some care. See, for example, [this question and its answers](https://stackoverflow.com/q/3150942/1424875). – nanofarad Oct 22 '22 at 18:19
  • 2
    Just a quick note, use `size_t` for sizes and lengths. It is just more self-explanatory. – Kaiyakha Oct 22 '22 at 18:27
  • @Kaiyakha does size_t has any platform dependent min and max values? – Yashoja Lakmith Oct 22 '22 at 18:30
  • 2
    If you want to delete the list why not just call `delete head` then let the list destructor clean up the contained nodes. To me at least, a method called `clear` deleting the list itself would be quite unexpected – Alan Birtles Oct 22 '22 at 18:31
  • 2
    `_L` in `_LINKEDLIST_DEFAULT_MAX_SIZE` is reserved, do not name your variable like that. – apple apple Oct 22 '22 at 19:11
  • 1
    and you probably want to use `const(expr)` here instead of macro. – apple apple Oct 22 '22 at 19:13
  • 1
    _I have been learning and playing around C++ (mostly, pointers and dynamic memory allocation)_- That's not really the most profitable thing to be looking into. I would encourage you to learn about STL containers instead (specifically, `std::string` and `std::vector`) – Paul Sanders Oct 22 '22 at 21:08
  • 1
    A method is just yet another function. It has a “hidden” `this` argument. You can `delete` `this` just as if you `delete`d anything else that your function’s arguments point at. Presumably, similar (if not the same) rules and constraints apply. For example, you cannot touch the object’s members or do anything that would “dereference `this`” after the deletion. – Andrej Podzimek Oct 22 '22 at 21:18

0 Answers0