memory leak help

When I run valgrind on my code, I receive one error on my insert method at operator new. I know this probably means that I have to delete node n somewhere, but I have been trying so many methods to delete it but they just give me more errors. Please someone show me how I can fix this memory leak. This is my code.


class key_value_sequences {
public:
struct node{
int key;
vector<int> values;
node* next;
node* prev;
};

key_value_sequences() {
}
~key_value_sequences() {
}
key_value_sequences(const key_value_sequences& A) {
n = A.n;
head = A.head;
tail = A.tail;
v = A.v;
}
key_value_sequences& operator=(const key_value_sequences& A) {
if (this == &A) return *this;
n = A.n;
head = A.head;
tail = A.tail;
v = A.v;
return *this;
}
// YOU SHOULD USE C++ CONTAINERS TO AVOID RAW POINTERS
// IF YOU DECIDE TO USE POINTERS, MAKE SURE THAT YOU MANAGE MEMORY PROPERLY

// IMPLEMENT ME: SHOULD RETURN SIZE OF A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN -1
int size(int key) const {
if (find(v.begin(), v.end(), key)!=v.end()) {
node* temp = head;
while(temp != NULL) {
if (temp->key == key) {
return temp->values.size();
}
else temp = temp->next;
}
}
else return -1;
}
// IMPLEMENT ME: SHOULD RETURN POINTER TO A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN nullptr
const int* data(int key) const {
if (find(v.begin(), v.end(), key)!=v.end()) {
node* temp = head;
while(temp != NULL) {
if (temp->key == key) {
return temp->values.data();
}
else temp = temp->next;
}
}
else return nullptr;
}
// IMPLEMENT ME: INSERT VALUE INTO A SEQUENCE IDENTIFIED BY GIVEN KEY
void insert(int key, int value) {
if(v.size() == 0) { //empty list
v.push_back(key);
n = new node;
n->prev = NULL;
n->key = key;
n->values.push_back(value);
head = n;
tail = n;
}
else if((find(v.begin(), v.end(), key)!=v.end())) { //if key exists already
node* temp = head;
while(temp != NULL) {
if (temp->key == key) {
temp->values.push_back(value);
break;
}
else temp = temp->next;
}
}
else { //if theres no existing key
v.push_back(key);
n = new node;
n->key = key;
n->values.push_back(value);
n->prev = tail;
tail->next = n;
tail = n;
tail->next = NULL;
}
}
private:
vector<int> v;
node* n;
node* head;
node* tail;
}; // class key_value_sequences
Last edited on
I can't quite see what you are trying to do (insufficient code and no code tags). However, surely the only place that you need a n = new node statement is in the constructor, not every insert statement?
i am trying to store values into keys
insert method(key, value) is supposed to add a value to the key.
I need to make a new node (for a new key and its vector of values) each time there is a new key.
When I put n = new node on constructor, I think I can get rid of n= new node; on the empty case in the insert method but then I get two errors instead
> but I have been trying so many methods to delete it but they just give me
> more errors
I don't see a single delete in the code that you've posted.

> I know this probably means that I have to delete node n somewhere
not somewhere. There's a specific place where you need to do it.

> but then I get two errors instead
not telling what you are doing, not telling what you get.
not bothering to help you.

> YOU SHOULD USE C++ CONTAINERS TO AVOID RAW POINTERS
perhaps you should follow that advice.
Topic archived. No new replies allowed.