List Pointer not deleting

1
2
3
4
5
6
7
8
9
10
11
LinkedList* listPtr = new LinkedList;

listPtr->addNode(3);
listPtr->addNode(4);
listPtr->addNode(6);
listPtr->print(std::cout);

delete listPtr;
    
listPtr->addNode(5);
listPtr->print(std::cout);


When I run the program it outputs
{ 3, 4, 6 }
{ 5 }

Why does the LinkedList object that listPtr is pointing to not get destroyed??
Last edited on
Why does the LinkedList object that listPtr is pointing to not get destroyed??

It does get destroyed. You have undefined behavior here. The fact that it seems to work is purely incidental.
So it is nothing that I did wrong??
Of course you did something wrong. Lines 10 and 11 result in undefined behavior. Undefined behavior is something you absolutely do not want in your program.
Cire, I really appreciate your help!! I'm not entirely sure what is causing the problem. Here is the full code I have so far. I was merely doing my own tests on my implementation when I ran into the undefined behavior. I hope it is easy enough for you to read.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <limits>

class Node {
public:
    int value;
    Node* next;
    Node(int x = 0, Node* p = 0);
};

Node::Node(int x, Node* p)
{
    value = x;
    next = p;
}

class LinkedList {
public:
    Node* head;
    Node* end;
    Node* iterator;
    LinkedList();
    ~LinkedList();
    void addNode(int value = 0);
    void deleteNode();
    void print(std::ostream& stream);
};

LinkedList::LinkedList()
{
    head = 0;
    end = 0;
    iterator = 0;
}

LinkedList::~LinkedList()
{
    iterator = head;
    end = 0;
    
    while (iterator != 0)
    {
        head = iterator->next;
        delete iterator;
        iterator = head;
    }
}

void LinkedList::addNode(int value)
{
    if (head == 0)
    {
        head = new Node(value);
        end = head;
    }
    else
    {
        Node* tempNode = new Node(value);
        end->next = tempNode;
        end = tempNode;
    }
        
}

void LinkedList::deleteNode()
{
    
}

void LinkedList::print(std::ostream& stream)
{
    if (head != 0)
    {
        iterator = head;
        
        stream << "{ ";
        while (iterator != 0)
        {
            stream << iterator->value;
            if (iterator->next != 0)
                stream << ", ";
            iterator = iterator->next;
        }
        stream << " }\n";
    }
}

void clearScreen();
void clearBuffer();
int validateEntry();


int main()
{
    LinkedList* listPtr = new LinkedList;
    int createNum, tempValue;
    
    clearScreen();
    std::cout << "Enter Number of Nodes to Create: ";
    createNum = validateEntry();
    
    for (int i = 0; i < createNum; i++)
    {
        clearScreen();
        std::cout << "Enter Value for Node " << (i + 1) << ": ";
        tempValue = validateEntry();
        
        listPtr->addNode(tempValue);
    }
    
    clearScreen();
    
    listPtr->print(std::cout);
    
    delete listPtr;
    //listPtr = 0;
    
    listPtr->addNode(5);
    listPtr->print(std::cout);
        
    return 0;
}

void clearScreen()
{
    std::cout << "\033[2J\033[1;1H";
}

void clearBuffer()
{
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}

int validateEntry()
{
    int tempValue;
    std::cin >> tempValue;
    
    if (!std::cin)
    {
        do
        {
            clearBuffer();
            clearScreen();
            std::cout << "Enter Valid Integer: ";
            std::cin >> tempValue;
        } while (!std::cin);
    }
    
    return tempValue;
}
1
2
3
4
5
    delete listPtr;
    //listPtr = 0;
    
    listPtr->addNode(5);
    listPtr->print(std::cout);


What's so hard about this? listPtr no longer points to valid memory, so don't use it as if it does.
Nothing is hard about it. I'm not sure why you are being so terse.

I am teaching myself from books and was doing my own bit of testing. I guess I had the misconception that delete actually cleared the memory location. Since apparently it does not, it seems that it just releases it to be used at some other point later on. Therefore, since nothing else has happened the object is still in memory at that location and the program added a node to it, which would not always happen (incidental). I did not plan on trying to use the object again, it was more of just a "what happens if I put this code here". When it actually worked it threw me for a what the fortran moment.

On a side note, what do you think of everything else?? Comments/Critiques

I appreciate your help!!!!

Cheers,
Scott

Last edited on
On a side note, what do you think of everything else?? Comments/Critiques


The data members of your linked list class should not be public. iterator should not be a member at all. You need a copy constructor and copy assignment operator. Client code should not be worried about how you're storing the data internally so member names like addNode would better off without the Node reference. There is no reason for you to use dynamic memory in main. You should get into the habit of using initialization lists in constructors:

1
2
3
LinkedList::LinkedList() : head(0), end(0), iterator(0)
{
}


The implementation/logic is fairly solid thus far, but it's lacking much of the functionality one would expect of a linked list. You've also done of a fair job of keeping functions small and focused, which I don't see too much of at your level.

Overall, I'd say you've done a good job and are on the right track.
Last edited on
Thanks. I was aware I still needed a copy constructor and copy assignment operator along with the ability to sort, etc..., I just haven't gotten there yet (I took a break to watch the Red Wings game).

As for using dynamic memory in main(), I was merely just doing it for testing purposes. The whole program was more about setting up the class structure instead of actually doing something. The main() was merely just to utilize it and by using dynamic memory I could call delete to make sure my destructor was deleting each node.

I did not know about initialization lists in C++...THANK YOU!!

My plan is to setup the linked list just using an integer data type in the node for now and then my next project is to convert it to a template.

Cheers,
Scott
Topic archived. No new replies allowed.