C++ Linked List Assignment Operator Problem
I am writing an Ordered Linked List class definition (OLList). I have written the assignment operator function, but when I try to test it by chaining assignment operations, the program gets caught in the while loop of the OLList::copy
function. I know this because I tested using console prints.
//OLList.h
struct Node {
ListItem item;
Node *next;
};
class OLList {
public:
OLList& OLList::operator =(const OLList& rhs)
{
if (this != &rhs) {
destroy();
copy(rhs);
}
return *this;
}
void OLList::destroy()
{
Node *current_node = this->headM;
Node *next_node;
while(current_node->next != nullptr)
{
next_node = current_node->next;
delete(current_node);
current_node = next_node;
}
return;
}
void OLList::copy(const OLList& source)
{
Node *new_node, *current_node;
Node *current_source_node = source.headM;
this->headM->item = source.headM->item;
current_node = this->headM;
while(current_source_node->next != nullptr)
{
new_node = new(Node);
current_node->next = new_node;
current_node = current_node->next;
current_source_node = current_source_node->next;
current_node->item = current_source_node->item;
}
return;
}
}
Below is the code used to test the class. I have made sure that the print() function works fine so that's definitely not an issue.
//main.cpp
int main()
{
OLList the_list;
the_list.insert(1);
the_list.insert(2);
OLList second_list;
second_list.insert(3);
second_list.insert(4);
OLList third_list;
third_list.insert(5);
third_list.insert(6);
third_list = second_list = the_list;
third_list.print();
}
When it is compiled and run, the program never terminates as it is caught in the loop mentioned above.
c++ class
add a comment |
I am writing an Ordered Linked List class definition (OLList). I have written the assignment operator function, but when I try to test it by chaining assignment operations, the program gets caught in the while loop of the OLList::copy
function. I know this because I tested using console prints.
//OLList.h
struct Node {
ListItem item;
Node *next;
};
class OLList {
public:
OLList& OLList::operator =(const OLList& rhs)
{
if (this != &rhs) {
destroy();
copy(rhs);
}
return *this;
}
void OLList::destroy()
{
Node *current_node = this->headM;
Node *next_node;
while(current_node->next != nullptr)
{
next_node = current_node->next;
delete(current_node);
current_node = next_node;
}
return;
}
void OLList::copy(const OLList& source)
{
Node *new_node, *current_node;
Node *current_source_node = source.headM;
this->headM->item = source.headM->item;
current_node = this->headM;
while(current_source_node->next != nullptr)
{
new_node = new(Node);
current_node->next = new_node;
current_node = current_node->next;
current_source_node = current_source_node->next;
current_node->item = current_source_node->item;
}
return;
}
}
Below is the code used to test the class. I have made sure that the print() function works fine so that's definitely not an issue.
//main.cpp
int main()
{
OLList the_list;
the_list.insert(1);
the_list.insert(2);
OLList second_list;
second_list.insert(3);
second_list.insert(4);
OLList third_list;
third_list.insert(5);
third_list.insert(6);
third_list = second_list = the_list;
third_list.print();
}
When it is compiled and run, the program never terminates as it is caught in the loop mentioned above.
c++ class
2
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead ofcopy
?
– PaulMcKenzie
Nov 20 '18 at 1:57
add a comment |
I am writing an Ordered Linked List class definition (OLList). I have written the assignment operator function, but when I try to test it by chaining assignment operations, the program gets caught in the while loop of the OLList::copy
function. I know this because I tested using console prints.
//OLList.h
struct Node {
ListItem item;
Node *next;
};
class OLList {
public:
OLList& OLList::operator =(const OLList& rhs)
{
if (this != &rhs) {
destroy();
copy(rhs);
}
return *this;
}
void OLList::destroy()
{
Node *current_node = this->headM;
Node *next_node;
while(current_node->next != nullptr)
{
next_node = current_node->next;
delete(current_node);
current_node = next_node;
}
return;
}
void OLList::copy(const OLList& source)
{
Node *new_node, *current_node;
Node *current_source_node = source.headM;
this->headM->item = source.headM->item;
current_node = this->headM;
while(current_source_node->next != nullptr)
{
new_node = new(Node);
current_node->next = new_node;
current_node = current_node->next;
current_source_node = current_source_node->next;
current_node->item = current_source_node->item;
}
return;
}
}
Below is the code used to test the class. I have made sure that the print() function works fine so that's definitely not an issue.
//main.cpp
int main()
{
OLList the_list;
the_list.insert(1);
the_list.insert(2);
OLList second_list;
second_list.insert(3);
second_list.insert(4);
OLList third_list;
third_list.insert(5);
third_list.insert(6);
third_list = second_list = the_list;
third_list.print();
}
When it is compiled and run, the program never terminates as it is caught in the loop mentioned above.
c++ class
I am writing an Ordered Linked List class definition (OLList). I have written the assignment operator function, but when I try to test it by chaining assignment operations, the program gets caught in the while loop of the OLList::copy
function. I know this because I tested using console prints.
//OLList.h
struct Node {
ListItem item;
Node *next;
};
class OLList {
public:
OLList& OLList::operator =(const OLList& rhs)
{
if (this != &rhs) {
destroy();
copy(rhs);
}
return *this;
}
void OLList::destroy()
{
Node *current_node = this->headM;
Node *next_node;
while(current_node->next != nullptr)
{
next_node = current_node->next;
delete(current_node);
current_node = next_node;
}
return;
}
void OLList::copy(const OLList& source)
{
Node *new_node, *current_node;
Node *current_source_node = source.headM;
this->headM->item = source.headM->item;
current_node = this->headM;
while(current_source_node->next != nullptr)
{
new_node = new(Node);
current_node->next = new_node;
current_node = current_node->next;
current_source_node = current_source_node->next;
current_node->item = current_source_node->item;
}
return;
}
}
Below is the code used to test the class. I have made sure that the print() function works fine so that's definitely not an issue.
//main.cpp
int main()
{
OLList the_list;
the_list.insert(1);
the_list.insert(2);
OLList second_list;
second_list.insert(3);
second_list.insert(4);
OLList third_list;
third_list.insert(5);
third_list.insert(6);
third_list = second_list = the_list;
third_list.print();
}
When it is compiled and run, the program never terminates as it is caught in the loop mentioned above.
c++ class
c++ class
edited Nov 20 '18 at 1:24
Jedediah
asked Nov 20 '18 at 0:52
JedediahJedediah
335
335
2
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead ofcopy
?
– PaulMcKenzie
Nov 20 '18 at 1:57
add a comment |
2
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead ofcopy
?
– PaulMcKenzie
Nov 20 '18 at 1:57
2
2
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead of copy
?– PaulMcKenzie
Nov 20 '18 at 1:57
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead of copy
?– PaulMcKenzie
Nov 20 '18 at 1:57
add a comment |
1 Answer
1
active
oldest
votes
Your destroy()
method will fail if headM
is nullptr
. You should be using while(current_node != nullptr)
instead of while(current_node->next != nullptr)
. But more importantly, it doesn't reset headM
to nullptr
after destroying the list. So after operator=
calls destroy()
, headM
is no longer in a valid state for copy()
to use.
Your copy()
method is similarly not checking if either source or target headM
are nullptr
. But more importantly, it assumes the target list is empty beforehand, otherwise it leaks memory, if it does not crash outright (per above). And frankly, it simply is not coded correctly in general to copy one list to another.
So, your code is invoking undefined behavior, this anything could happen.
Like @PaulMcKenzie stated in comments, you really should be using a proper copy constructor instead (and a destructor - and since you are clearly using C++11 or later, a move constructor and move assignment operator, too - see the Rule of 5). Your assignment operator can then be implemented using your copy constructor (and likewise for move assignment).
Try something more like this:
struct Node {
ListItem item;
Node *next = nullptr;
Node(const ListItem &value) : item(value) {}
};
class OLList {
private:
Node *headM = nullptr;
public:
OLList() = default;
OLList(const OLList &src)
{
Node *current_source_node = src.headM;
Node **current_node = &headM;
while (current_source_node)
{
*current_node = new Node(current_source_node->item);
current_node = &((*current_node)->next);
current_source_node = current_source_node->next;
}
/* alternatively:
Node *current_source_node = src.headM;
while (current_source_node) {
insert(current_source_node->item);
}
*/
}
OLList(OLList&& src)
{
src.swap(*this);
}
~OLList()
{
Node *next_node;
while (headM)
{
next_node = headM->next;
delete headM;
headM = next_node;
}
}
void clear() {
OLList().swap(*this);
}
OLList& operator=(const OLList& rhs)
{
if (this != &rhs) {
OLList(rhs).swap(*this);
}
return *this;
}
OLList& OLList::operator=(OLList&& rhs)
{
OLList(std::move(rhs)).swap(*this);
return *this;
}
void swap(OLList &other) {
std::swap(headM, other.headM);
}
void insert(const ListItem &value) {
...
}
void print() const {
...
}
...
};
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when youroperator=
callsdestroy()
,headM
is left in an invalid state, and thencopy()
tries to useheadM
, causing undefined behavior.
– Remy Lebeau
Nov 20 '18 at 7:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384717%2fc-linked-list-assignment-operator-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your destroy()
method will fail if headM
is nullptr
. You should be using while(current_node != nullptr)
instead of while(current_node->next != nullptr)
. But more importantly, it doesn't reset headM
to nullptr
after destroying the list. So after operator=
calls destroy()
, headM
is no longer in a valid state for copy()
to use.
Your copy()
method is similarly not checking if either source or target headM
are nullptr
. But more importantly, it assumes the target list is empty beforehand, otherwise it leaks memory, if it does not crash outright (per above). And frankly, it simply is not coded correctly in general to copy one list to another.
So, your code is invoking undefined behavior, this anything could happen.
Like @PaulMcKenzie stated in comments, you really should be using a proper copy constructor instead (and a destructor - and since you are clearly using C++11 or later, a move constructor and move assignment operator, too - see the Rule of 5). Your assignment operator can then be implemented using your copy constructor (and likewise for move assignment).
Try something more like this:
struct Node {
ListItem item;
Node *next = nullptr;
Node(const ListItem &value) : item(value) {}
};
class OLList {
private:
Node *headM = nullptr;
public:
OLList() = default;
OLList(const OLList &src)
{
Node *current_source_node = src.headM;
Node **current_node = &headM;
while (current_source_node)
{
*current_node = new Node(current_source_node->item);
current_node = &((*current_node)->next);
current_source_node = current_source_node->next;
}
/* alternatively:
Node *current_source_node = src.headM;
while (current_source_node) {
insert(current_source_node->item);
}
*/
}
OLList(OLList&& src)
{
src.swap(*this);
}
~OLList()
{
Node *next_node;
while (headM)
{
next_node = headM->next;
delete headM;
headM = next_node;
}
}
void clear() {
OLList().swap(*this);
}
OLList& operator=(const OLList& rhs)
{
if (this != &rhs) {
OLList(rhs).swap(*this);
}
return *this;
}
OLList& OLList::operator=(OLList&& rhs)
{
OLList(std::move(rhs)).swap(*this);
return *this;
}
void swap(OLList &other) {
std::swap(headM, other.headM);
}
void insert(const ListItem &value) {
...
}
void print() const {
...
}
...
};
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when youroperator=
callsdestroy()
,headM
is left in an invalid state, and thencopy()
tries to useheadM
, causing undefined behavior.
– Remy Lebeau
Nov 20 '18 at 7:10
add a comment |
Your destroy()
method will fail if headM
is nullptr
. You should be using while(current_node != nullptr)
instead of while(current_node->next != nullptr)
. But more importantly, it doesn't reset headM
to nullptr
after destroying the list. So after operator=
calls destroy()
, headM
is no longer in a valid state for copy()
to use.
Your copy()
method is similarly not checking if either source or target headM
are nullptr
. But more importantly, it assumes the target list is empty beforehand, otherwise it leaks memory, if it does not crash outright (per above). And frankly, it simply is not coded correctly in general to copy one list to another.
So, your code is invoking undefined behavior, this anything could happen.
Like @PaulMcKenzie stated in comments, you really should be using a proper copy constructor instead (and a destructor - and since you are clearly using C++11 or later, a move constructor and move assignment operator, too - see the Rule of 5). Your assignment operator can then be implemented using your copy constructor (and likewise for move assignment).
Try something more like this:
struct Node {
ListItem item;
Node *next = nullptr;
Node(const ListItem &value) : item(value) {}
};
class OLList {
private:
Node *headM = nullptr;
public:
OLList() = default;
OLList(const OLList &src)
{
Node *current_source_node = src.headM;
Node **current_node = &headM;
while (current_source_node)
{
*current_node = new Node(current_source_node->item);
current_node = &((*current_node)->next);
current_source_node = current_source_node->next;
}
/* alternatively:
Node *current_source_node = src.headM;
while (current_source_node) {
insert(current_source_node->item);
}
*/
}
OLList(OLList&& src)
{
src.swap(*this);
}
~OLList()
{
Node *next_node;
while (headM)
{
next_node = headM->next;
delete headM;
headM = next_node;
}
}
void clear() {
OLList().swap(*this);
}
OLList& operator=(const OLList& rhs)
{
if (this != &rhs) {
OLList(rhs).swap(*this);
}
return *this;
}
OLList& OLList::operator=(OLList&& rhs)
{
OLList(std::move(rhs)).swap(*this);
return *this;
}
void swap(OLList &other) {
std::swap(headM, other.headM);
}
void insert(const ListItem &value) {
...
}
void print() const {
...
}
...
};
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when youroperator=
callsdestroy()
,headM
is left in an invalid state, and thencopy()
tries to useheadM
, causing undefined behavior.
– Remy Lebeau
Nov 20 '18 at 7:10
add a comment |
Your destroy()
method will fail if headM
is nullptr
. You should be using while(current_node != nullptr)
instead of while(current_node->next != nullptr)
. But more importantly, it doesn't reset headM
to nullptr
after destroying the list. So after operator=
calls destroy()
, headM
is no longer in a valid state for copy()
to use.
Your copy()
method is similarly not checking if either source or target headM
are nullptr
. But more importantly, it assumes the target list is empty beforehand, otherwise it leaks memory, if it does not crash outright (per above). And frankly, it simply is not coded correctly in general to copy one list to another.
So, your code is invoking undefined behavior, this anything could happen.
Like @PaulMcKenzie stated in comments, you really should be using a proper copy constructor instead (and a destructor - and since you are clearly using C++11 or later, a move constructor and move assignment operator, too - see the Rule of 5). Your assignment operator can then be implemented using your copy constructor (and likewise for move assignment).
Try something more like this:
struct Node {
ListItem item;
Node *next = nullptr;
Node(const ListItem &value) : item(value) {}
};
class OLList {
private:
Node *headM = nullptr;
public:
OLList() = default;
OLList(const OLList &src)
{
Node *current_source_node = src.headM;
Node **current_node = &headM;
while (current_source_node)
{
*current_node = new Node(current_source_node->item);
current_node = &((*current_node)->next);
current_source_node = current_source_node->next;
}
/* alternatively:
Node *current_source_node = src.headM;
while (current_source_node) {
insert(current_source_node->item);
}
*/
}
OLList(OLList&& src)
{
src.swap(*this);
}
~OLList()
{
Node *next_node;
while (headM)
{
next_node = headM->next;
delete headM;
headM = next_node;
}
}
void clear() {
OLList().swap(*this);
}
OLList& operator=(const OLList& rhs)
{
if (this != &rhs) {
OLList(rhs).swap(*this);
}
return *this;
}
OLList& OLList::operator=(OLList&& rhs)
{
OLList(std::move(rhs)).swap(*this);
return *this;
}
void swap(OLList &other) {
std::swap(headM, other.headM);
}
void insert(const ListItem &value) {
...
}
void print() const {
...
}
...
};
Your destroy()
method will fail if headM
is nullptr
. You should be using while(current_node != nullptr)
instead of while(current_node->next != nullptr)
. But more importantly, it doesn't reset headM
to nullptr
after destroying the list. So after operator=
calls destroy()
, headM
is no longer in a valid state for copy()
to use.
Your copy()
method is similarly not checking if either source or target headM
are nullptr
. But more importantly, it assumes the target list is empty beforehand, otherwise it leaks memory, if it does not crash outright (per above). And frankly, it simply is not coded correctly in general to copy one list to another.
So, your code is invoking undefined behavior, this anything could happen.
Like @PaulMcKenzie stated in comments, you really should be using a proper copy constructor instead (and a destructor - and since you are clearly using C++11 or later, a move constructor and move assignment operator, too - see the Rule of 5). Your assignment operator can then be implemented using your copy constructor (and likewise for move assignment).
Try something more like this:
struct Node {
ListItem item;
Node *next = nullptr;
Node(const ListItem &value) : item(value) {}
};
class OLList {
private:
Node *headM = nullptr;
public:
OLList() = default;
OLList(const OLList &src)
{
Node *current_source_node = src.headM;
Node **current_node = &headM;
while (current_source_node)
{
*current_node = new Node(current_source_node->item);
current_node = &((*current_node)->next);
current_source_node = current_source_node->next;
}
/* alternatively:
Node *current_source_node = src.headM;
while (current_source_node) {
insert(current_source_node->item);
}
*/
}
OLList(OLList&& src)
{
src.swap(*this);
}
~OLList()
{
Node *next_node;
while (headM)
{
next_node = headM->next;
delete headM;
headM = next_node;
}
}
void clear() {
OLList().swap(*this);
}
OLList& operator=(const OLList& rhs)
{
if (this != &rhs) {
OLList(rhs).swap(*this);
}
return *this;
}
OLList& OLList::operator=(OLList&& rhs)
{
OLList(std::move(rhs)).swap(*this);
return *this;
}
void swap(OLList &other) {
std::swap(headM, other.headM);
}
void insert(const ListItem &value) {
...
}
void print() const {
...
}
...
};
edited Nov 20 '18 at 7:12
answered Nov 20 '18 at 2:15
Remy LebeauRemy Lebeau
332k18251444
332k18251444
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when youroperator=
callsdestroy()
,headM
is left in an invalid state, and thencopy()
tries to useheadM
, causing undefined behavior.
– Remy Lebeau
Nov 20 '18 at 7:10
add a comment |
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when youroperator=
callsdestroy()
,headM
is left in an invalid state, and thencopy()
tries to useheadM
, causing undefined behavior.
– Remy Lebeau
Nov 20 '18 at 7:10
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Well, I assume that would work.. However I'm not doing this for practical purposes; I'm trying to learn it. I still want to know why my code doesn't work, yes I see those problems but they are simple fixes by just adding in an if statement to check for a nullptr Head, and also adding in a 'destroy();' call at the beginning of copy(). What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?
– Jedediah
Nov 20 '18 at 4:44
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
Also, I did write a copy constructor that calls OLList::copy(). This copy() function is a private helper function used in multiple other functions.
– Jedediah
Nov 20 '18 at 4:47
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah -- Well there is a flaw in your assignment operator. You destroyed your data before knowing whether the allocation will work or not. Using the copy / swap idiom in the assignment operator alleviates this from happening.
– PaulMcKenzie
Nov 20 '18 at 5:18
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when your
operator=
calls destroy()
, headM
is left in an invalid state, and then copy()
tries to use headM
, causing undefined behavior.– Remy Lebeau
Nov 20 '18 at 7:10
@Jedediah "What is incorrect about the rest of the process assuming those fixes are made, assuming I still want to stay with this method?" - well, for one thing, when your
operator=
calls destroy()
, headM
is left in an invalid state, and then copy()
tries to use headM
, causing undefined behavior.– Remy Lebeau
Nov 20 '18 at 7:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384717%2fc-linked-list-assignment-operator-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
void OLList::copy(const OLList& source)
-- Why didn't you write a true copy constructor instead ofcopy
?– PaulMcKenzie
Nov 20 '18 at 1:57