Skip to content

Commit 7ff9237

Browse files
authored
Merge pull request #22 from nkaaf/fix_get_operation_on_immutable_tables_creating_a_new_object_instead_fo_referencing_it
Fix getting values of immutable lists containting pointers (like String)
2 parents 66bbbaf + 757dd16 commit 7ff9237

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/AbstractList.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ template <typename T> class AbstractList {
4848
/// Create a final Value from the given Value
4949
#define createFinalValue(value, finalValue, T) \
5050
finalValue = (T *)malloc(sizeof(T)); \
51-
memcpy(finalValue, &(value), sizeof(T));
51+
memcpy(finalValue, &value, sizeof(T));
5252

5353
/**
5454
* Class representing an abstract entry in the list.
@@ -65,6 +65,11 @@ template <typename T> class AbstractList {
6565
*/
6666
explicit AbstractEntry(T *value) : value(value) {}
6767

68+
/*!
69+
* @brief Destructor of an AbstractEntry Object.
70+
*/
71+
~AbstractEntry() { value = nullptr; }
72+
6873
/*!
6974
* @brief Free the memory of the value to prevent memory leaks.
7075
*/

src/DoubleLinkedList.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
5151
*/
5252
explicit Entry(T *value) : AbstractList<T>::AbstractEntry(value) {}
5353

54+
/*!
55+
* @brief Destructor of an Entry Object.
56+
*/
57+
~Entry() {
58+
next = nullptr;
59+
prev = nullptr;
60+
}
61+
5462
/*!
5563
* @brief Get the next entry of the list.
5664
*
@@ -109,9 +117,9 @@ template <typename T> class DoubleLinkedList : public AbstractList<T> {
109117
if (this->isMutable()) {
110118
return (T *)current->getValue();
111119
} else {
112-
T val = *current->getValue();
120+
T *val = current->getValue();
113121
T *finalValue;
114-
createFinalValue(val, finalValue, T);
122+
createFinalValue(*val, finalValue, T);
115123
return finalValue;
116124
}
117125
}

src/SingleLinkedList.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
5050
*/
5151
explicit Entry(T *value) : AbstractList<T>::AbstractEntry(value) {}
5252

53+
/*!
54+
* @brief Destructor of an Entry Object.
55+
*/
56+
~Entry() { next = nullptr; }
57+
5358
/*!
5459
* @brief Get the next entry of the list.
5560
*
@@ -87,9 +92,9 @@ template <typename T> class SingleLinkedList : public AbstractList<T> {
8792
if (this->isMutable()) {
8893
return (T *)current->getValue();
8994
} else {
90-
T val = *current->getValue();
95+
T *val = current->getValue();
9196
T *finalValue;
92-
createFinalValue(val, finalValue, T);
97+
createFinalValue(*val, finalValue, T);
9398
return finalValue;
9499
}
95100
}

0 commit comments

Comments
 (0)