Memory Leak problem
Oct 2, 2015 at 3:55pm UTC
I seem to have a memory leak that I have been trying to find but cannot locate in this templated linked list class. Am I missing something? I am fairly new to C++ so thank you in advance for any help.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
#ifndef LLIST_H
#define LLIST_H
/*
Templated Linked List class.
September 2015
*/
#include <ostream>
#include <stdexcept>
using namespace std;
// This is a templated struct. You don't need to change this.
template <class T>
struct node_t {
T data;
node_t* next;
};
// Change this class!
template <class T>
class LList{
public :
LList(){
head = NULL;
s = 0;
}
~LList(){
node_t<T>* temp = head;
while (temp){
node_t<T>* to_delete = temp;
temp = temp->next;
delete to_delete;
}
head = NULL;
s = 0;
}
LList(const LList& other){
node_t<T>* temp = other.head;
s = 0;
head = NULL;
while (temp){
this ->push_back(temp->data);
temp = temp->next;
}
}
LList<T>& operator = (const LList& other){
if (this == &other){
return *this ;
}
//this = NULL;
//head = NULL;
clear();
s = 0;
node_t<T>* temp = other.head;
s++;
while (temp){
this ->push_back(temp->data);
temp = temp->next;
s++;
}
return *this ;
}
T getAt(int pos) const {
node_t<T>* temp = head;
T ret;
if (pos < 0) {
pos = size() + pos;
if (pos < 0) {
throw logic_error("Position not in list" );
}
}
if (pos < size()){
for (int i = 0; i < pos; i++){
temp = temp->next;
}
ret = temp->data;
}
else {
throw logic_error("Index not within linked list" );
}
return ret;
}
int size() const {
node_t<T>* temp = head;
int i = 0;
while (temp){
i++;
temp = temp->next;
}
return i;
}
void push_back(T value){
// Is the list empty?
if (head == NULL){
//push_front(thing); this will work fine ..or
node_t<T>* new_node = new node_t<T>;
new_node->data = value;
new_node->next = NULL;
head = new_node;
return ;
}
node_t<T>* temp = head;
while (temp->next){
temp = temp->next;
}
// temp is...... a valid node with a null next pointer
node_t<T>* new_node = new node_t<T>;
new_node->data = value;
new_node->next = NULL;
temp->next = new_node;
s++;
}
void push_front(T value){
node_t<T>* temp = new node_t<T>;
temp->next = head;
temp->data = value;
head = temp;
s++;
}
void setAt(T value, int pos){
node_t<T>* temp = head;
if (pos < 0){
pos = size() + pos;
if (pos < 0){
throw logic_error("Position not in list" );
}
}
if (pos < size()){
for (int i = 0; i < pos; i++){
temp = temp->next;
}
temp->data = value;
} else {
throw logic_error("Inputted position larger than list" );
}
}
void remove(int pos){
if (pos >= size()){
throw logic_error("Position not in list" );
}
if (pos < 0){
pos = size() + pos;
if (pos < 0){
throw logic_error("Position not in list" );
}
}
if (pos == 0){ // first element
if (size() == 0){
throw logic_error("List empty" );
}
node_t<T>* temp = head;
head = head->next;
delete temp;
return ;
}
node_t<T>* temp = head;
while (temp && pos > 1){
pos--;
temp = temp->next;
}
if (!temp){
throw logic_error("Bad index!" );
return ;
}
node_t<T>* to_delete = temp->next;
temp->next = temp->next->next;
delete to_delete;
s--;
}
LList<T> reverse() const {
LList<T> ret;
for (int i = size()-1; i >= 0; i--){
ret.push_back(getAt(i));
}
return ret;
}
LList<T> operator +(const LList<T>& other) const {
LList<T> ret;
node_t<T>* temp = head;
while (temp){
ret.push_back(temp->data);
temp = temp->next;
}
temp = other.head;
while (temp){
ret.push_back(temp->data);
temp = temp->next;
}
return ret;
}
bool operator ==(const LList<T>& other) const {
node_t<T>* temp = head;
node_t<T>* temp2 = other.head;
int i = 0;
if (size() == other.size()){
if (size() == 0){
return true ;
}
while (temp){
if (temp->data == temp2->data){
i++;
temp = temp->next;
temp2 = temp2->next;
}
}
}else {
return false ;
}
if (i == size()){
return true ;
}else {
return false ;
}
}
bool operator !=(const LList<T>& other) const {
node_t<T>* temp = head;
node_t<T>* temp2 = other.head;
int i = 0;
if (size() != other.size()){
if (size() == 0 || other.size() == 0){
return true ;
}
while (temp){
if (temp->data != temp2->data){
i++;
temp = temp->next;
temp2 = temp2->next;
}
}
}
else {
return false ;
}
if (i == size()){
return true ;
}
else {
return false ;
}
}
void clear(){
node_t<T>* temp = head;
while (temp){
node_t<T>* to_delete = temp;
temp = temp->next;
delete to_delete;
}
head = NULL;
s = 0;
}
private :
node_t<T>* head;
int s;
// Fill in stuff here!
};
template <class T>
ostream& operator <<(ostream& out, const LList<T> other){
out << "[" ;
for (int i = 0; i < other.size(); i++){
out << i << ": " << other.getAt(i);
i++;
}
out << "]\n" ;
return out;
}
#endif
Last edited on Oct 2, 2015 at 4:00pm UTC
Oct 2, 2015 at 8:41pm UTC
> I seem to have a memory leak
¿why do you think that?
> Am I missing something?
main()
please give me something that I could run.
Oct 2, 2015 at 10:00pm UTC
I noticed your
push_back function updates size with
s++ and your
operator= function also updates size with
s++ . Unless I am missing something, this would double the size of the current list instance if
s is for size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
LList<T>& operator = (const LList& other){
if (this == &other){
return *this ;
}
clear();
s = 0;
node_t<T>* temp = other.head;
s++;
while (temp){
this ->push_back(temp->data);//size update here
temp = temp->next;
s++; //size update here
}
return *this ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void push_back(T value){
if (head == NULL){
node_t<T>* new_node = new node_t<T>;
new_node->data = value;
new_node->next = NULL;
head = new_node;
return ;
}
node_t<T>* temp = head;
while (temp->next){
temp = temp->next;
}
node_t<T>* new_node = new node_t<T>;
new_node->data = value;
new_node->next = NULL;
temp->next = new_node;
s++; //size update here
}
Last edited on Oct 2, 2015 at 10:03pm UTC
Topic archived. No new replies allowed.