0

I keep getting a segmentation fault on my deep copy of a linked list. I use this deep copy in my Copy Contructor and my assignment operator (operator=) and have come to the conclusion that it is this that is seg faulting.

bigint::Node* bigint::deepcopy(bigint::Node* target){
      bigint::Node* current = target;
      bigint::Node*cpy = new Node;
      cpy->digit = current->digit;
      Node* const hd = cpy;
      current = current->next;
      while(current != nullptr){
           bigint::Node* tmp = new Node;
           tmp->digit = current->digit;
           cpy->next = tmp;
           cpy = cpy->next;
           current = current->next;
       }
    return hd;
}

My Node struct looks like:

private:
struct Node{
      int digit;
      Node* next;
};
Node* head;
static Node* deepcopy(Node* target);

My class is closed and all, just showing what is in private that is related to this function. Thanks in advance for any advice.

  • http://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant – macroland Dec 07 '16 at 07:47
  • First of all I would suggest start using unique_ptr and references instead of pointers whenever you can. That usually reduces errors like you are having now to minimum. Also what exactly does deep copy mean and what you intend to do with that copy? – Maroš Beťko Dec 07 '16 at 08:24
  • @MarošBeťko I think "deep copy" means that all nodes inside the list gets copied/cloned. So at the end you have another list with the same values in the same order – Thomas Dec 07 '16 at 08:38
  • It would also be really useful to show us the line where your program SIGSEGV's, that would help us solve your problem much faster than just guessing where can be the fault. If you can't seem to locate it, just pop a break point f.e. on the Node* current = target line, run debug and go step by step until it fails. – Maroš Beťko Dec 07 '16 at 09:06
  • If you make your `bigint` immutable, there is no need for a deep copy. A shallow copy (of the node pointer) is all that is required. And why on earth are you using a linked list anyway? – Rudy Velthuis Dec 07 '16 at 09:28

1 Answers1

0

When you use your deepcopy function, you must make sure that the paramater target is not nullptr. So, you should check if (target == nullptr) at the beginning of your deepcopy function.

Also, after the while loop finished, you should set the tail of your new list to nullptr.

From the information you post, it seems you have use ->digit or ->next on a null pointer.

If you still get this error, you'd better provide a example code.

September
  • 416
  • 4
  • 11
  • Okay I added the check at the beginning and basically if it was nullptr made a new node* and set it equal to nullptr and returned it. I found out through some testing that I am getting through the beginning of the code but seg faulting somewhere in the while loop. I added cpy->next = nullptr; before return hd; I am not sure if that is what you meant. After those two adjustments I am still experiencing the seg fault and in the same location. – Evan Furniss Dec 07 '16 at 08:22
  • I wrote some code to test with your deepcopy code. But It seems that it works fine here. [Here is my test code](http://paste.ubuntu.com/23592508/). – September Dec 07 '16 at 08:46