Segmentation Default Error when Initializing Class object

Reply

Join Date: Jan 2009
Posts: 5
Reputation: kennycason is an unknown quantity at this point 
Solved Threads: 0
kennycason kennycason is offline Offline
Newbie Poster

Segmentation Default Error when Initializing Class object

 
0
  #1
Jan 2nd, 2009
I've been working at this, ALL day... and stumped
I have a class called LinkedList, which I am writing, Everything compiles, but at run time I get a segmentation default error.
It occurs when I initialize a LinkedList object.
I.e. LinkedList ll; // causes a seg def error at runtime

Also, As i can't even get the object to initialize, please dont be too critical over the accuracy of the algorithms used. Thanks alot!

here is LinkedList.h
  1. #ifndef LinkedList_h
  2. #define LinkedList_h
  3.  
  4. #include "common.h"
  5. #include "Node.h"
  6. class Node; // forward reference
  7.  
  8. class LinkedList {
  9. public:
  10. LinkedList();
  11. void insert(int,int);
  12. void append(int);
  13. void prepend(int);
  14. Node* getNode(int);
  15. int getValue(int);
  16. int remove(int);
  17. int getLast();
  18. int getFirst();
  19. void enqueue(int);
  20. int dequeue();
  21. int pop();
  22. void push(int);
  23. int getSize();
  24. void clear();
  25. void destroy();
  26.  
  27. private:
  28. Node root;
  29. // Node* root_ptr;
  30. int size;
  31. };
  32. #endif

LinkedList.cpp
  1. #include "LinkedList.h"
  2. using namespace std;
  3.  
  4.  
  5. LinkedList::LinkedList() {
  6. root = Node(); cout << "TEST" << endl;
  7. size = 0;
  8. }
  9.  
  10.  
  11. void LinkedList::insert(int i, int val) {
  12. if(i >= 0 && i < size) {
  13. Node* walkthru = root.getNext();
  14. for(int c = 0; c < i; c++) {
  15. walkthru = walkthru->getNext();
  16. }
  17. Node tmp = Node(val); // make a new node
  18. tmp.setPrev(walkthru);
  19. if(i < size - 1) { // if its not last
  20. tmp.setNext(walkthru->getNext());
  21. walkthru->getNext()->setPrev(&tmp);
  22. }
  23. walkthru->setNext(&tmp);
  24. size++;
  25. } else {
  26. cout << " could not insert to list" << endl;
  27. }
  28. }
  29.  
  30. void LinkedList::append(int val) {
  31. cout << "TEST" << endl;
  32. Node* walkthru = root.getNext();
  33. for(int c = 0; c < size; c++) {
  34. walkthru = walkthru->getNext();
  35. }
  36. Node tmp = Node(val); // make a new node
  37. tmp.setPrev(walkthru);
  38. walkthru->setNext(&tmp);
  39. size++;
  40. }
  41.  
  42. void LinkedList::prepend(int val) {
  43. insert(0,val);
  44. }
  45.  
  46. Node* LinkedList::getNode(int i) {
  47. if(i >= 0 && i < size) {
  48. Node* walkthru = root.getNext();
  49. for(int c = 0; c < i; c++) {
  50. walkthru = walkthru->getNext();
  51. }
  52. return walkthru;
  53. }
  54. return NULL;
  55. }
  56.  
  57. int LinkedList::getValue(int i) {
  58. if(i >= 0 && i < size) {
  59. Node* walkthru = root.getNext();
  60. for(int c = 0; c < i; c++) {
  61. walkthru = walkthru->getNext();
  62. }
  63. return walkthru->get();
  64. }
  65. return NULL;
  66. }
  67.  
  68. int LinkedList::remove(int i) {
  69. if(i >= 0 && i < size) {
  70. Node* walkthru = root.getNext();
  71. for(int c = 0; c < i; c++) {
  72. walkthru = walkthru->getNext();
  73. }
  74. Node* tmp = walkthru;
  75. walkthru->getPrev()->setNext(walkthru->getNext());
  76. int val = tmp->get();
  77. delete tmp;
  78. size--;
  79. return val;
  80. }
  81. return NULL;
  82. }
  83.  
  84. int LinkedList::getFirst() { // head
  85. return getValue(0);
  86. }
  87.  
  88. int LinkedList::getLast() { // tail
  89. return getValue(size - 1);
  90. }
  91.  
  92. void LinkedList::enqueue(int val) { // enqueue to tail
  93. insert(size,val);
  94. remove(size);
  95. }
  96.  
  97. int LinkedList::dequeue() { // dequeue from head
  98. return remove(0);
  99. }
  100.  
  101. int LinkedList::pop() { // pop from tail
  102. return remove(size - 1);
  103. }
  104.  
  105. void LinkedList::push(int val) {
  106. insert(size,val); // push to tail
  107. }
  108.  
  109. int LinkedList::getSize() {
  110. return size;
  111. }
  112. void LinkedList::clear() {
  113. Node* walkthru = root.getNext();
  114. for(int i = 0; i < size; i++) {
  115. walkthru = walkthru->getNext();
  116. if(walkthru->getPrev() != &root) {
  117. delete walkthru->getPrev();
  118. }
  119. }
  120. size = 0;
  121. root = NULL;
  122. }
  123.  
  124. void LinkedList::destroy() {
  125. // should do some sort of clean up. :)
  126. root = NULL;
  127. }

Node.h
  1. #ifndef Node_h
  2. #define Node_h
  3.  
  4. #include "common.h"
  5.  
  6. class Node {
  7. public:
  8. Node();
  9. Node(int);
  10. Node(Node*);
  11. void set(int);
  12. int get();
  13. void setNext(Node*);
  14. void setPrev(Node*);
  15. Node* getNext();
  16. Node* getPrev();
  17.  
  18. private:
  19. Node* prev;
  20. Node* next;
  21. int data;
  22.  
  23. };
  24. #endif

Node.cpp
  1. #include "Node.h"
  2. using namespace std;
  3.  
  4. Node::Node() {
  5. *prev = NULL;
  6. *next = NULL;
  7. data = 0;
  8. }
  9.  
  10. Node::Node(int val) {
  11. *prev = NULL;
  12. *next = NULL;
  13. data = val;
  14. }
  15.  
  16. Node::Node(Node* n) {
  17. *prev = n->getPrev();
  18. *next = n->getNext();
  19. data = n->get();
  20. }
  21.  
  22. void Node::set(int val) {
  23. data = val;
  24. }
  25.  
  26. int Node::get() {
  27. return data;
  28. }
  29.  
  30. void Node::setNext(Node* n) {
  31. next = n;
  32. }
  33.  
  34. void Node::setPrev(Node* p) {
  35. prev = p;
  36. }
  37.  
  38. Node* Node::getNext() {
  39. return next;
  40. }
  41.  
  42. Node* Node::getPrev() {
  43. return prev;
  44. }

Main.cpp
  1. #include "Main.h"
  2.  
  3. using namespace std;
  4.  
  5. int main ( int argc, char* argv[] ) {
  6. cout << "Init" << endl;
  7.  
  8. LinkedList ll;
  9.  
  10. cout << "Init end" << endl;
  11.  
  12. ll.append(1);
  13. ll.append(2);
  14. ll.append(3);
  15. cout << "pop " << ll.getLast() << endl;
  16. cout << "Size " << ll.getSize() << endl;
  17. return 0;
  18. }

Main.h
  1. #ifndef Main_h
  2. #define Main_h
  3.  
  4. #include "common.h"
  5. #include "LinkedList.h"
  6.  
  7. #endif

common.h
  1. #ifndef common_h
  2. #define common_h
  3.  
  4.  
  5. #ifdef __cplusplus
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <fstream>
  9. #else
  10. #include <stdlib.h>
  11. #include <iostream>
  12. #include <fstream>
  13. #endif
  14.  
  15. using namespace std;
  16. #endif
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Segmentation Default Error when Initializing Class object

 
0
  #2
Jan 2nd, 2009
*prev = NULL;
*next = NULL;

Should be
prev = NULL;
next = NULL;
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: kennycason is an unknown quantity at this point 
Solved Threads: 0
kennycason kennycason is offline Offline
Newbie Poster

Re: Segmentation Default Error when Initializing Class object

 
0
  #3
Jan 2nd, 2009
wow.. so stupid
Thanks alot! Can't believe I didn't see that.
now i can continue debugging. I got another error, but I know whats wrong.
peace
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC