Trying to write my own linked list impementation in c++, code segfaults after hitting 3 elements in the list
I've been trying to write my own implementation of linked list, but the code segfaults when I try to access an the third element or anything after it. Adding elements doesn't segfault, but accessing does. I can't find the pointer error in my get() function.
Each node in the list stores data (of Template t) and a pointer leading to the next node. I have two functions for everything- one for the first element, and one for any subsequent elements. The get() function for the subsequent elements always segfaults. I have some debug messages in the function that spit out results I can't explain. For example, if I run a get() request for the second element, an then the third, the code doesn't segfault, but it does return clearly incorrect results. Debug messages I placed indicate the segfault occurs when the second element calls the function to check the third element, if it occurs at all. Try the code with and without the line cout << newList.get(2) << endl; and you'll get very different results.
One possible cause is the pointer storage- I have the get() function output the pointer of each element (except the first) as it cycles through, and compare them to the pointers outputted by the add() function, and and pointers for element 0 and 1 match, but 2 and beyond do not match, and I can't seem to figure out why that would be.
#include <iostream>
using namespace std;
template <class T> class myLinkedList{
T data;
myLinkedList<T> *next = NULL;
public:
myLinkedList(T input){
data = input;
}
void add(T input){
if(next == NULL){
myLinkedList<T> newItem(input);
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}else{
myLinkedList<T> nextEntry = *next;
nextEntry.add(input);
}
}
T getData(){
return data;
}
//the start of the get function, only used by the first entry in the list
T get(int entry){
int currentPosition = 0;
if(entry == currentPosition){
return getData();
}else{
//defrefrence the pointer anc check the next entry
myLinkedList<T> nextEntry = *next;
return nextEntry.get(entry, ++currentPosition);
}
}
private:
//this vesion is the hidden, private vesion only used by nodes other than the first one
//used to keep track of position in the list
T get(int entry, int currentPosition){
//cout << currentPosition << endl;
if(entry == currentPosition){
return data;
}else{
//derefrence the pointer and check the next entry
cout << next << endl;
myLinkedList<T> nextEntry = *next;
currentPosition++;
T output = nextEntry.get(entry, currentPosition);
return output;
}
}
};
int main(){
myLinkedList<int> newList(3);
newList.add(4);
newList.add(5);
newList.add(7);
newList.add(9);
cout << newList.get(2) << endl;
cout << newList.get(3) << endl;
return 0;
}
Results are clearly erroneous- program should spit oout two macthing sets of pointers, as well as the numbers 5 and 7 ( the list elements)
c++ pointers
|
show 4 more comments
I've been trying to write my own implementation of linked list, but the code segfaults when I try to access an the third element or anything after it. Adding elements doesn't segfault, but accessing does. I can't find the pointer error in my get() function.
Each node in the list stores data (of Template t) and a pointer leading to the next node. I have two functions for everything- one for the first element, and one for any subsequent elements. The get() function for the subsequent elements always segfaults. I have some debug messages in the function that spit out results I can't explain. For example, if I run a get() request for the second element, an then the third, the code doesn't segfault, but it does return clearly incorrect results. Debug messages I placed indicate the segfault occurs when the second element calls the function to check the third element, if it occurs at all. Try the code with and without the line cout << newList.get(2) << endl; and you'll get very different results.
One possible cause is the pointer storage- I have the get() function output the pointer of each element (except the first) as it cycles through, and compare them to the pointers outputted by the add() function, and and pointers for element 0 and 1 match, but 2 and beyond do not match, and I can't seem to figure out why that would be.
#include <iostream>
using namespace std;
template <class T> class myLinkedList{
T data;
myLinkedList<T> *next = NULL;
public:
myLinkedList(T input){
data = input;
}
void add(T input){
if(next == NULL){
myLinkedList<T> newItem(input);
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}else{
myLinkedList<T> nextEntry = *next;
nextEntry.add(input);
}
}
T getData(){
return data;
}
//the start of the get function, only used by the first entry in the list
T get(int entry){
int currentPosition = 0;
if(entry == currentPosition){
return getData();
}else{
//defrefrence the pointer anc check the next entry
myLinkedList<T> nextEntry = *next;
return nextEntry.get(entry, ++currentPosition);
}
}
private:
//this vesion is the hidden, private vesion only used by nodes other than the first one
//used to keep track of position in the list
T get(int entry, int currentPosition){
//cout << currentPosition << endl;
if(entry == currentPosition){
return data;
}else{
//derefrence the pointer and check the next entry
cout << next << endl;
myLinkedList<T> nextEntry = *next;
currentPosition++;
T output = nextEntry.get(entry, currentPosition);
return output;
}
}
};
int main(){
myLinkedList<int> newList(3);
newList.add(4);
newList.add(5);
newList.add(7);
newList.add(9);
cout << newList.get(2) << endl;
cout << newList.get(3) << endl;
return 0;
}
Results are clearly erroneous- program should spit oout two macthing sets of pointers, as well as the numbers 5 and 7 ( the list elements)
c++ pointers
1
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
3
A bug (probably the bug) inadd
,myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.
– user4581301
Dec 29 '18 at 3:07
1
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13
|
show 4 more comments
I've been trying to write my own implementation of linked list, but the code segfaults when I try to access an the third element or anything after it. Adding elements doesn't segfault, but accessing does. I can't find the pointer error in my get() function.
Each node in the list stores data (of Template t) and a pointer leading to the next node. I have two functions for everything- one for the first element, and one for any subsequent elements. The get() function for the subsequent elements always segfaults. I have some debug messages in the function that spit out results I can't explain. For example, if I run a get() request for the second element, an then the third, the code doesn't segfault, but it does return clearly incorrect results. Debug messages I placed indicate the segfault occurs when the second element calls the function to check the third element, if it occurs at all. Try the code with and without the line cout << newList.get(2) << endl; and you'll get very different results.
One possible cause is the pointer storage- I have the get() function output the pointer of each element (except the first) as it cycles through, and compare them to the pointers outputted by the add() function, and and pointers for element 0 and 1 match, but 2 and beyond do not match, and I can't seem to figure out why that would be.
#include <iostream>
using namespace std;
template <class T> class myLinkedList{
T data;
myLinkedList<T> *next = NULL;
public:
myLinkedList(T input){
data = input;
}
void add(T input){
if(next == NULL){
myLinkedList<T> newItem(input);
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}else{
myLinkedList<T> nextEntry = *next;
nextEntry.add(input);
}
}
T getData(){
return data;
}
//the start of the get function, only used by the first entry in the list
T get(int entry){
int currentPosition = 0;
if(entry == currentPosition){
return getData();
}else{
//defrefrence the pointer anc check the next entry
myLinkedList<T> nextEntry = *next;
return nextEntry.get(entry, ++currentPosition);
}
}
private:
//this vesion is the hidden, private vesion only used by nodes other than the first one
//used to keep track of position in the list
T get(int entry, int currentPosition){
//cout << currentPosition << endl;
if(entry == currentPosition){
return data;
}else{
//derefrence the pointer and check the next entry
cout << next << endl;
myLinkedList<T> nextEntry = *next;
currentPosition++;
T output = nextEntry.get(entry, currentPosition);
return output;
}
}
};
int main(){
myLinkedList<int> newList(3);
newList.add(4);
newList.add(5);
newList.add(7);
newList.add(9);
cout << newList.get(2) << endl;
cout << newList.get(3) << endl;
return 0;
}
Results are clearly erroneous- program should spit oout two macthing sets of pointers, as well as the numbers 5 and 7 ( the list elements)
c++ pointers
I've been trying to write my own implementation of linked list, but the code segfaults when I try to access an the third element or anything after it. Adding elements doesn't segfault, but accessing does. I can't find the pointer error in my get() function.
Each node in the list stores data (of Template t) and a pointer leading to the next node. I have two functions for everything- one for the first element, and one for any subsequent elements. The get() function for the subsequent elements always segfaults. I have some debug messages in the function that spit out results I can't explain. For example, if I run a get() request for the second element, an then the third, the code doesn't segfault, but it does return clearly incorrect results. Debug messages I placed indicate the segfault occurs when the second element calls the function to check the third element, if it occurs at all. Try the code with and without the line cout << newList.get(2) << endl; and you'll get very different results.
One possible cause is the pointer storage- I have the get() function output the pointer of each element (except the first) as it cycles through, and compare them to the pointers outputted by the add() function, and and pointers for element 0 and 1 match, but 2 and beyond do not match, and I can't seem to figure out why that would be.
#include <iostream>
using namespace std;
template <class T> class myLinkedList{
T data;
myLinkedList<T> *next = NULL;
public:
myLinkedList(T input){
data = input;
}
void add(T input){
if(next == NULL){
myLinkedList<T> newItem(input);
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}else{
myLinkedList<T> nextEntry = *next;
nextEntry.add(input);
}
}
T getData(){
return data;
}
//the start of the get function, only used by the first entry in the list
T get(int entry){
int currentPosition = 0;
if(entry == currentPosition){
return getData();
}else{
//defrefrence the pointer anc check the next entry
myLinkedList<T> nextEntry = *next;
return nextEntry.get(entry, ++currentPosition);
}
}
private:
//this vesion is the hidden, private vesion only used by nodes other than the first one
//used to keep track of position in the list
T get(int entry, int currentPosition){
//cout << currentPosition << endl;
if(entry == currentPosition){
return data;
}else{
//derefrence the pointer and check the next entry
cout << next << endl;
myLinkedList<T> nextEntry = *next;
currentPosition++;
T output = nextEntry.get(entry, currentPosition);
return output;
}
}
};
int main(){
myLinkedList<int> newList(3);
newList.add(4);
newList.add(5);
newList.add(7);
newList.add(9);
cout << newList.get(2) << endl;
cout << newList.get(3) << endl;
return 0;
}
Results are clearly erroneous- program should spit oout two macthing sets of pointers, as well as the numbers 5 and 7 ( the list elements)
c++ pointers
c++ pointers
asked Dec 29 '18 at 2:49
cpwestcpwest
11
11
1
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
3
A bug (probably the bug) inadd
,myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.
– user4581301
Dec 29 '18 at 3:07
1
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13
|
show 4 more comments
1
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
3
A bug (probably the bug) inadd
,myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.
– user4581301
Dec 29 '18 at 3:07
1
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13
1
1
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
3
3
A bug (probably the bug) in
add
, myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.– user4581301
Dec 29 '18 at 3:07
A bug (probably the bug) in
add
, myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.– user4581301
Dec 29 '18 at 3:07
1
1
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13
|
show 4 more comments
2 Answers
2
active
oldest
votes
One of your main problems is here:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
you allocate an item on stack inside the if
scope. Then you make next to point to this item. But... lifetime of the item is bounded by this scope. As son as you exit the scope, this item does not exist any longer. You need to allocate it dynamically by 'new' or other methods.
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.
– Serge
Dec 30 '18 at 14:54
add a comment |
I had a breakthrough! Following Serge's solution was helpful, but one more change was needed- rather than create a function reference in the else block of my add function,
eg
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
i needed to use the pointer directly, as in
next->add(input)
I didn't know my pointer/object syntax
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%2f53966298%2ftrying-to-write-my-own-linked-list-impementation-in-c-code-segfaults-after-hi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One of your main problems is here:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
you allocate an item on stack inside the if
scope. Then you make next to point to this item. But... lifetime of the item is bounded by this scope. As son as you exit the scope, this item does not exist any longer. You need to allocate it dynamically by 'new' or other methods.
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.
– Serge
Dec 30 '18 at 14:54
add a comment |
One of your main problems is here:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
you allocate an item on stack inside the if
scope. Then you make next to point to this item. But... lifetime of the item is bounded by this scope. As son as you exit the scope, this item does not exist any longer. You need to allocate it dynamically by 'new' or other methods.
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.
– Serge
Dec 30 '18 at 14:54
add a comment |
One of your main problems is here:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
you allocate an item on stack inside the if
scope. Then you make next to point to this item. But... lifetime of the item is bounded by this scope. As son as you exit the scope, this item does not exist any longer. You need to allocate it dynamically by 'new' or other methods.
One of your main problems is here:
if(next == NULL){
myLinkedList<T> newItem(input); // <<<<<<<<<<<<<
next = &newItem;
cout << "adding to list, data is " << input << ", pointer is " << next << endl;
}
you allocate an item on stack inside the if
scope. Then you make next to point to this item. But... lifetime of the item is bounded by this scope. As son as you exit the scope, this item does not exist any longer. You need to allocate it dynamically by 'new' or other methods.
answered Dec 29 '18 at 3:10
SergeSerge
3,57921014
3,57921014
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.
– Serge
Dec 30 '18 at 14:54
add a comment |
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.
– Serge
Dec 30 '18 at 14:54
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
Trying to allocate memory with new (myLinkedList<T> newItem = new myLinkedList<T>(input);) leads to some puzzling compile time errors LinkedListTest.cpp:15:33: error: invalid conversion from ‘myLinkedList<int>*’ to ‘int’ [-fpermissive] Basically, calling the add function in main causes some pointer issue myLinkedList<T> newItem = new myLinkedList<T>(input);
– cpwest
Dec 30 '18 at 4:53
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.– Serge
Dec 30 '18 at 14:54
myLinkedList<T> *newItem = new myLinkedList<T>(input);
-- you need to declare a pointer in such a case.– Serge
Dec 30 '18 at 14:54
add a comment |
I had a breakthrough! Following Serge's solution was helpful, but one more change was needed- rather than create a function reference in the else block of my add function,
eg
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
i needed to use the pointer directly, as in
next->add(input)
I didn't know my pointer/object syntax
add a comment |
I had a breakthrough! Following Serge's solution was helpful, but one more change was needed- rather than create a function reference in the else block of my add function,
eg
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
i needed to use the pointer directly, as in
next->add(input)
I didn't know my pointer/object syntax
add a comment |
I had a breakthrough! Following Serge's solution was helpful, but one more change was needed- rather than create a function reference in the else block of my add function,
eg
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
i needed to use the pointer directly, as in
next->add(input)
I didn't know my pointer/object syntax
I had a breakthrough! Following Serge's solution was helpful, but one more change was needed- rather than create a function reference in the else block of my add function,
eg
myLinkedList<T> nextEntry = *next;
nextEntry.add(input)
i needed to use the pointer directly, as in
next->add(input)
I didn't know my pointer/object syntax
answered Dec 31 '18 at 2:57
cpwestcpwest
11
11
add a comment |
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%2f53966298%2ftrying-to-write-my-own-linked-list-impementation-in-c-code-segfaults-after-hi%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
1
Before I look at this, I'm going to point out the two best ways to figure out linked list problems. 1) Draw pictures of what the list has to look like. This often suggests how to write the code and when you have a bug, follow your instructions to modify the drawing. If you find yourself drawing something silly, you just found a bug.
– user4581301
Dec 29 '18 at 3:00
2) Use whatever debugger comes with your development environment. A debugger allows you to execute the program on your terms. You can advance the program with whatever granularity you want and investigate the variables as you go. If you spot the program doing something unexpected, you just found a bug (or an error in your expectations. You'll need to fix that, too). Debuggers are an essential tool for the working programmer. They are likely the best productivity tool this side of the compiler, so the sooner you get a handle on them, the sooner you reap the rewards.
– user4581301
Dec 29 '18 at 3:02
3
A bug (probably the bug) in
add
,myLinkedList<T> newItem(input);
declares a local variable, This is an automatic allocation that will be destroyed when it goes out of scope (the program reaches the enclosing close brace). Storing a pointer to this is not useful to you because the data pointed at is gone almost immediately leaving the program with a dangling pointer.– user4581301
Dec 29 '18 at 3:07
1
Funny, I can't find where you are allocating storage for each new node? Am I missing it?
– David C. Rankin
Dec 29 '18 at 3:09
Is the linked list supposed to own the objects it links (and manage their lifetimes) or not?
– David Schwartz
Dec 29 '18 at 3:13