C++ Linked List and Double Pointer Questions

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
o I am practicing doing data structures. Figured I would start with a linked list and building them in a few different ways.

Right now when I create a double pointer and try to set the values my program crashes at run time. Can somebody help me figure this out. I don't have a lot of experience with double pointers but I was told (*dataValue) is how you use it.

#include<iostream>
using namespace std;

class node {
int value;
node* ptr;

public:
void setValue(int theValue) { value = theValue; }
void setPointer(node* thePtr) { ptr = thePtr; }
int getValue() { return value; }
node* getPtr() { return ptr; }
};

typedef node* nodePtr;

class linkedList {

public:
nodePtr link;
void insertLinkHead(nodePtr* next, int theValue);
void outPut(nodePtr head);
};


int main() {


linkedList LL;


nodePtr* head = new node*;


//THIS PART CRASHES BELOW
(*head)->setPointer(NULL);
(*head)->setValue(0);
//THIS PART CRASHES ABOVE

//setting up linked list
for (int i = 1; i <= 5; i++) {
LL.insertLinkHead(head, i);
}




LL.outPut(*head);

cout << "Testing" << endl;


return 0;
}//end of main function

void linkedList::insertLinkHead(nodePtr* head, int theValue) {

nodePtr newLink = new node;

newLink->setValue(theValue);
newLink->setPointer(*head);
*head = newLink;
}

void linkedList::eek:utPut(nodePtr head) {

nodePtr temp;

temp = head;

while (temp != NULL) {
cout << "value is " << temp->getValue() << endl;
temp = temp->getPtr();
}
}
 
Use code tags :).

Most of these errors should be caught with strict compile and/or compiler warnings.

Code:
nodePtr* head = new node*;

The right side of the equals sign isn't creating a new node and getting you the address, it's creating a new node pointer and getting you the address to it. Then it assigns that to the left side. In short: remove the second *

Code:
//THIS PART CRASHES BELOW
(*head)->setPointer(NULL);
(*head)->setValue(0);
//THIS PART CRASHES ABOVE

After fixing the call to new node, you can remove the extra dereferences here, turning (*head)-> into head->
 
Code:
nodePtr* head = new node*;

... In short: remove the second *

A better suggestion would be to use this...
Code:
nodePtr* head = new nodePtr;

...as it's easier to read.

After fixing the call to new node, you can remove the extra dereferences here, turning (*head)-> into head->

Dereferencing head is still necessary due to the double indirection. Recall, local variable head is really of the type node**. A good compiler should catch this, regardless.

It's failing for a different reason, which I've given enough hint to the OP as to why it is failing. OP stated they were inexperienced with double pointers, and this is a good lesson for them.

Edit: I was quick to judge the compiler on this. There are some things a compiler can't catch, such as runtime errors. Upon second glance, there is nothing syntactically wrong with the OPs code. That's probably why it compiles fine. The issue isn't caught until runtime. Here's another hint: There's missing step(s) between these two statements:
Code:
nodePtr* head = new node*;

(*head)->setPointer(NULL);
 
Last edited:
A better suggestion would be to use this...
Code:
nodePtr* head = new nodePtr;

...as it's easier to read.



Dereferencing head is still necessary due to the double indirection. Recall, local variable head is really of the type node**. A good compiler should catch this, regardless.

It's failing for a different reason, which I've given enough hint to the OP as to why it is failing. OP stated they were inexperienced with double pointers, and this is a good lesson for them.

Edit: I was quick to judge the compiler on this. There are some things a compiler can't catch, such as runtime errors. Upon second glance, there is nothing syntactically wrong with the OPs code. That's probably why it compiles fine. The issue isn't caught until runtime. Here's another hint: There's missing step(s) between these two statements:
Code:
nodePtr* head = new node*;

(*head)->setPointer(NULL);
Yeah, I missed the typedef nodePtr node*... It makes me wonder why the double pointer is needed, instead of just doing node* nodePtr = new node;
 
Yeah, I missed the typedef nodePtr node*... It makes me wonder why the double pointer is needed, instead of just doing node* nodePtr = new node;

The OP wanted local variable head as an in/out parameter to his class method insertLinkHead. This presumably allows 1) less arguments to the method signature and 2) slightly more encapsulation (debatable). Of minor note, the for loop block (in main) becomes a one liner, although the compiler will most likely inline expand the method making any notion of that moot.

I would tend to agree with you that using a single pointer would suffice and it makes the code overall easier to read. Memory allocation for a new node could happen in the main loop and then the resulting pointer/address could be passed to the Linked List's Insert method. There are a multitude of ways to accomplish the same thing. It's a matter of style.

In addition, the OP's solution doesn't address resource cleanup. As a rule of thumb, for every C++ 'new' operation you perform, you eventually have to follow up with a 'delete'. Failing to do this leads to memory leaks in your program. It's the classic 'newbie' mistake.
 
Back
Top