| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
General - Help Memory Error- Linked List C++
Hi, I am getting a memory error and I cannot figure out why, the message itself does not give much information, so I will list my code, for those interested the message reads
"Unhandled exception at 0x00411b09 in MapClass.exe: 0xC0000005: Access violation reading location 0xcdcdd071." Anyways, I have got a Hash Table class, and a Linked List class, the Linked list class is not mine, I am using a provided one from the following source http://sig9.com/node/312 (The single one) I have created an array of these linked lists in my hash table class Code:
LinkedList *Table = new LinkedList[Size]; the function I have a problem with is the following: Code:
bool CHashTable::Add(char* data)
{
int KeyValue = Hash(data);
Table[KeyValue].insert(data);
count++;
return true;
}
The Hash function just returns a value (93) to be precise and then we use this as to access that specific array element in Table, however when i call the insert function from the LinkedList (see http://sig9.com/node/312), when it runs the SetData(See http://sig9.com/node/312) function I get a memory leak? Ive tested the LinkedList class and there is no problems with it, I dont see why as I have passed a char pointer to it like it asks, it works if I just use the Linked List on its own and send in a char pointer, it inserts it fine. Please let me know if you need to see more code, thanks |
|
#2
|
||||
|
||||
|
how big is the Table? is 93 past the end of it?
what is the smallest possible value to be returned from Hash()? 1 or 0? if its 1, you may have to do Table[KeyValue - 1] to prevent going over the end of the array. is it throwing the error at compile time or run time? compile time doesnt make sense as the code for Add() seems fine, and if its at run time, you'll need to post the code of the data you're actually passing to Add() and what you expect to be more helpful. iono, hope this is helpful, i just kinda glazed over the problem... time for sleep...
__________________
|
|
#3
|
|||
|
|||
|
The error is at runtime, it compiles fine.
Everything seems in range, the add function is called using: Code:
HashTable->Add("Lolipops");
so i send in "Lolipops" as a char pointer. The Hash() returns the value 42 which is easily in the range as the max array size for Table is 101. Code:
Table[KeyValue].insert(data); The line above gives the problem, as it runs the insert function in the linked list it throws an error in the SetData function, you can look at this linked list in the link I specified above, the data passed into it is a char pointer called data and contains "Lolipops" Below is some of the code from the LinkedList that is causing giving me the error, ps I have tested the linked list and it seems correct and works when i use it on its own Code:
void LinkedList::insert(char* x) {
m_curr->setData(x);
m_curr->setNext(new LinkedList(m_start));
m_curr = m_curr->getNext();
}
void LinkedList::setData(char* el) {
m_data = el;
}
|
|
#4
|
|||
|
|||
|
Thanks for the post bobidy bob but I have just fixed it, it was a stupid copy error grrr.
in my private members I had: Code:
LinkedList *Table; then is my constructor I had Code:
LinkedList *Table = new LinkedList[Size]; which is stupid as its supposed to be Code:
Table = new LinkedList[Size]; not creating a brand new linkedlist pointer lol |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - Help Memory Error- Linked List C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|