Skip to content

Commit d413a4e

Browse files
authored
Update ConcurrentHashMap.h
Potential memory leak has been solved when a key exists using the insert function as suggested on #1
1 parent 8f6d86d commit d413a4e

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

ConcurrentHashMap.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ class ConcurrentHashMap {
4646
std::size_t hashValue = hashFunction(key);
4747
std::size_t index = hashValue % buckets.size();
4848

49+
Node* current = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node*
50+
while (current != nullptr) {
51+
if (current->key == key) {
52+
current->value = value; // Update the value
53+
return; // Found the key
54+
}
55+
current = current->next; // No need to cast here
56+
}
57+
4958
Node* newNode = new Node(key, value);
50-
newNode->next = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node*
59+
newNode->next = buckets[index].empty() ? nullptr : buckets[index][0]; // Cast to Node*
5160
buckets[index] = {newNode};
5261
}
5362

0 commit comments

Comments
 (0)