Memory Leak question

I'm working on finding memory leaks currently with a program I'm new to. I'm running valgrind/gdb and they are showing either a memory leak int the overloaded [] operator or in the insert() function. Below are both my functions, but I can't figure out where the leak is at!

Also note, I don't think my insert function is properly working yet to insert a new node into a linked list (so if you see a blatant error, please do point that out too). Thanks in advance for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
node & list::operator [](int idx)
{
        //if given a "bad" value, exit
        if (idx < 0)
                exit(1);

        int counter = 0;
        node * current = head;
        while (counter < idx) //go up until the position spot
        {
                counter++;
                current = current->get_next();
        }
        return *current;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void list::insert(node *target, int position)
{
             node * curr = head;
             node * prev = nullptr;
             node * added = nullptr;
             int       idx  = 0;

             // Empty list - add first element
             if (head == nullptr)
                 {
                  head = new node(*target); //create a new node of target
                 return;
                 }

             // List Not Empty.  Find the spot where we need to insert
             while(curr != nullptr && idx < position)
             {
                  prev = curr; //set prev to point to curr
                  curr = curr->get_next(); //set curr to point to next pointer
                  idx++;
             }

            // Add element after prev and before curr
            added = new node(*target); //create new node of target, called added
            prev->set_next(added); //set prev to point to added
            added->set_next(curr); //set added to point to curr
};


*note that "head" is a pointer in this list class to a node class, and set_next() and whatnot are functions in the node class.
> showing either a memory leak int the overloaded [] operator or in the insert() function
¿how did you arrive at such conclusion?
operator[] doesn't allocate memory
insert() creates one node, which I suppose is what you want
¿do you ever delete those nodes? ¿where do you think it should be done?

it's kind of weird that the user is exposed to a `node' structure, ¿what does your list store?
Here's where I delete memory:

1
2
3
4
5
6
7
8
//Destructor
list::~list()
{
        if (head != nullptr) //if head is not already empty, delete head
            delete [] head;

        head = nullptr; //set head to nullptr;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void list::remove(int pos)
{
        node * current = head;
        node * previous = nullptr;
        node * p_previous = nullptr;
        int idx = 0;

        if (current == nullptr) //if list is empty, do nothing
                return;
        else if (pos == 0) //delete first spot
        {
                current = head->get_next(); //assign current the head->next pointer
                delete head; //delete the info in head
                head = current; //reset head to the current pointer
                return;
        }

        //get to position to be deleted
        while (current != nullptr && idx < pos)
        {
                p_previous = previous;
                previous = current;
                current = current->get_next();
                idx++;
        }

        if (current == nullptr) //if deleting last element
        {
                delete previous;
                p_previous->set_next(nullptr);
        }
        else    //set previous to point to the current->next
                //then delete current
        {
                previous = current->get_next();
                delete current;
        }
        return;
};


Perhaps it's in here that I'm missing delete somewhere?
1
2
3
        //if given a "bad" value, exit
        if (idx < 0)
                exit(1);

What about the case when list has 7 elements and I access [42]?


1
2
3
4
5
6
list::~list()
{
        if (head != nullptr)
            delete [] head;
        head = nullptr;
};

You delete the first node. What does happen to all the other nodes?
EDIT: That is a "delete an array", but you don't allocate arrays!
Last edited on
1
2
3
4
5
        if (current == nullptr) //if deleting last element
        {
                delete previous;
                p_previous->set_next(nullptr); //you just delete it, you can't access it here
        }
Topic archived. No new replies allowed.