![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Getting a seg. fault with a simple linked-list
I figure knowing how to program a linked-list using a class would be a good exercise, so I attempted to code one with the following code:
//numbers.cpp -- implementing a linked-list with classes
#include <iostream>
using namespace std;
class Numbers
{
private:
struct Node
{
int value;
Node *nexts;
};
Node *firsts, *currents, *news;
public:
Numbers();
bool appendNumber(int);
//bool insertNumber(int, int);
bool showNumber(int);
};
Numbers::Numbers()
{
firsts = currents = news = 0;
}
bool Numbers::appendNumber(int x)
{
if(firsts == 0) {
firsts = new Node;
firsts->value = x;
firsts->nexts = 0;
return true;
}
else {
currents = firsts;
do {
currents = currents->nexts;
}while(currents->nexts != 0);
news = new Node;
currents->nexts = news;
news = currents;
currents->value = x;
currents->nexts = 0;
}
return true;
}
bool Numbers::showNumber(int x)
{
if(firsts == 0)
return false;
currents = firsts;
for(int i = 0; i < x; i++)
currents = currents->nexts;
cout << currents->value;
return true;
}
int main()
{
Numbers numbs1;
numbs1.appendNumber(4);
numbs1.appendNumber(5);
numbs1.showNumber(1);
numbs1.showNumber(2);
return 0;
}the only output I get is Quote:
Any help would be appreciated; Thanks ![]() EDIT: I just ran it through gdb (which I have very little expirience using), and apparently, line 42: }while(currents->nexts != 0); is a problem. Looks fine to me ...
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Oct 1st, 2005 at 11:48 PM. |
|
|
|
|
|
|
#2 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
Quote:
You are telling the computer to move to the next node (regardless of it validity) and then (without checking the validity) dereference that node. When that node is NULL, you will have trouble. My suggestion to you is to try to implement error checking in your code. I would prefer to write this as if (currents) {
while (currents->next)
currents = currents->next;
} else {
//handle the NULL case
}Any time a pointer is dereferenced, it should have been checked for NULL be fore the dereference. for(int i = 0; i < x; i++) currents = currents->nexts;
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
|
#3 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
Thanks for the reply ![]() I don't know if this is good practise, but often I will code a very simple, check-free version of a program to test the basic theory behind it. Once that is confirmed working, I add in bounds-checking ( correct term? ), and all the "padding". I will add the changes you mentioned ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
I added in a few changes:
//numbers.cpp -- implementing a linked-list with classes
#include <iostream>
using namespace std;
class Numbers
{
private:
struct Node
{
int value;
Node *nexts;
};
Node *firsts, *currents, *news;
public:
Numbers();
bool appendNumber(int);
//bool insertNumber(int, int);
bool showNumber(int);
void show();
};
Numbers::Numbers()
{
firsts = currents = news = 0;
}
bool Numbers::appendNumber(int x)
{
if(firsts == 0) {
firsts = new Node;
firsts->value = x;
firsts->nexts = 0;
return true;
}
else {
currents = firsts;
while(currents->nexts)
currents = currents->nexts;
news = new Node;
currents->nexts = news;
news = currents;
currents->value = x;
currents->nexts = 0;
}
return true;
}
/*bool Numbers::showNumber(int x)
{
if(firsts == 0)
return false;
currents = firsts;
for(int i = 0; i < x - 2; i++)
currents = currents->nexts;
cout << currents->value;
return true;
}*/
void Numbers::show()
{
currents = firsts;
while(currents->nexts != 0) {
cout << currents->value << endl;
currents = currents->nexts;
}
}
int main()
{
Numbers numbs1;
numbs1.appendNumber(4);
numbs1.appendNumber(5);
//numbs1.showNumber(1);
cout << endl;
//numbs1.showNumber(2);
numbs1.show();
return 0;
}this time, I don't get a segmentation fault, but I just get 2 lines of blank output. I don't understand why this is occuring.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Also use NULL instead of 0 in case you want a NULL pointer.
here 's a recent discussion. and i believe Scorpions4ever is right there.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
Edit: you need to use another pointer that stays a step back to the currents pointer, that way when your while loop finishes, that pointer points to the last element in your list. currents=start;
while(currents->nexts)
{
save=currents;
currents = currents->nexts;
}
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
Quote:
@JessehK Your print statement is never being reached. You are doing some funky stuff in your appendnumber function... You are always checking currents->nexts after assigning it to firsts, whos nexts is assigned null right before. So currents is always pointing to firsts. You update the value by assigning news to currents->nexts and assign the news to the head, and then the currents->nexts to 0 (What happened to news??) Now in your show function, you assign currents to firsts whos next is already determined to be NULL and check for currents->nexts to be non-null. This will always fail. Try putting a cout statement right before you test for while (currents->nexts != 0)...I bet you get output. If you put cout statements after the while loop, you will also get output. Look into these problems and see where you get.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#9 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
I fixed it after changing a few of the do-while loops.
Works perfectly, now I just need to add in the bulk that you were refering to L7sqr .here is the code
//numbers.cpp -- implementing a linked-list with classes
#include <iostream>
using namespace std;
class Numbers
{
private:
struct Node
{
int value;
Node *nexts;
};
Node *firsts, *temps, *news, *currents;
public:
Numbers();
bool appendNumber(int);
void insertNumber(int, int);
void showNumber(int);
void show();
};
Numbers::Numbers()
{
firsts = /*lasts =*/ news = currents = NULL;
}
bool Numbers::appendNumber(int x)
{
news = new Node;
news->value = x;
if(firsts == NULL) {
firsts = news;
firsts->nexts = NULL;
return true;
}
else {
currents = firsts;
while(currents->nexts != NULL) {
currents = currents->nexts;
}
currents->nexts = news;
currents = news;
currents->nexts = NULL;
}
return true;
}
void Numbers::showNumber(int x)
{
//if(firsts == 0)
//return false;
currents = firsts;
for(int i = 0; i < x - 1 ; i++)
currents = currents->nexts;
cout << currents->value;
}
void Numbers::insertNumber(int x, int y)
{
currents = firsts;
news = new Node;
news->value = x;
for(int i = 0; i < y - 2; i++) {
currents = currents->nexts;
}
temps = currents->nexts;
currents->nexts = news;
news->nexts = temps;
}
void Numbers::show()
{
currents = firsts;
do {
cout << currents->value << endl;
}while((currents = currents->nexts) != NULL);
}
int main()
{
Numbers numbs1;
numbs1.appendNumber(1);
numbs1.appendNumber(3);
numbs1.appendNumber(5);
numbs1.appendNumber(7);
cout << endl;
cout << endl;
numbs1.show();
numbs1.insertNumber(6, 4);
cout << endl;
numbs1.show();
cout << endl;
cout << "4th Node = ";
numbs1.showNumber(4);
cout << endl;
return 0;
}and the output: Quote:
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Oct 2nd, 2005 at 1:14 AM. |
|
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
Quote:
To tell you the truth, with all the currents', nexts', firsts' and lasts' I was kinds spinning myself. It's hard to pluralise items in a sentence that contains singular items with plural spellings. Rereading my post, I'm not sure how any of that helped ![]()
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|