Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Segmentation Fault in conditional statement of while loop parsing linked list for ordered insertion
    primarykey
    data
    text
    <p>This is a class assignment to do ordered insertion into a sorted linked list. I've avoided accessing null pointers, and I've added some lines of debugging output to narrow the seg fault down to the conditional statement of the while loop itself. </p> <p>I can add a node to the empty list and add nodes with smaller keys to the head of the list, but I run into a seg fault when adding a key to the end of a non-empty list. As displayed by the debug output, the problematic while loop cycles successfully until it sees cursor->next == NULL. That's when it throws the seg fault. I'm not trying to access the empty pointer as far as I can tell. I'm really pretty stumped and would appreciate any help. Thanks!</p> <p>I've denoted the troublesome line in the code below.</p> <pre><code>// Headers #include &lt;iostream&gt; #include &lt;iomanip&gt; #include &lt;string&gt; using namespace std; struct Node { int id; string initials; double score; Node* next; }; const int ESC = -9999; const int ID_LIMIT = 1000000000; int main(){ // initialize variables int temp_id = 0; // temporary input Node* Start=NULL; // first node in linked list // get input until escape value is entered or input stream fails while ( temp_id != ESC &amp;&amp; !cin.fail() ){ cout &lt;&lt; "\nPlease enter the student id, or " &lt;&lt; ESC &lt;&lt; " to finish inputing: "; cin &gt;&gt; temp_id; if ( temp_id &gt;=0 &amp;&amp; temp_id &lt; ID_LIMIT ) { // if valid input, continue input &amp; create node string temp_inits; double temp_score; cout &lt;&lt; "Please enter the student initials: " ; cin &gt;&gt; temp_inits; cout &lt;&lt; "Please enter the student's test score: " ; cin &gt;&gt; temp_score; // create new node with input values Node* temp = new Node; temp-&gt;id = temp_id; temp-&gt;initials = temp_inits; temp-&gt;score = temp_score; temp-&gt;next = NULL; // TASK 4: SORTED INPUT if ( Start == NULL ) { // if first node to be input Start = temp; // point the first pointer to it }else{ // otherwise if( temp-&gt;id &lt; Start-&gt;id ){ // if new node should go before Start, temp-&gt;next = Start; // move start after temp Start = temp; // and reassign Start }else{ Node* cursor = Start; // parse list for proper place or end cout &lt;&lt; "\nDEBUG: while ( cursor-id:" &lt;&lt; cursor-&gt;id ; cout &lt;&lt; " &lt; temp-id:" &lt;&lt; temp-&gt;id; cout &lt;&lt; " &amp;&amp; cursor-next:" &lt;&lt; cursor-&gt;next &lt;&lt; " != NULL )"; // I THINK THE NEXT LINE IS THE CULPRIT while ( cursor-&gt;id &lt; temp-&gt;id &amp;&amp; cursor-&gt;next != NULL ) { cout &lt;&lt; "\nDEBUG: { old cursor:" &lt;&lt; cursor-&gt;id ; cursor = cursor-&gt;next; cout &lt;&lt; ", new cursor:" &lt;&lt; cursor-&gt;id &lt;&lt; " }"; cout &lt;&lt; "\nDEBUG: while ( cursor-id:" &lt;&lt; cursor-&gt;id ; cout &lt;&lt; " &lt; temp-id:" &lt;&lt; temp-&gt;id; cout &lt;&lt; " &amp;&amp; cursor-next:" &lt;&lt; cursor-&gt;next &lt;&lt; " != NULL )"; } cout &lt;&lt; "\nDEBUG: insert temp-id:" &lt;&lt; temp-&gt;id &lt;&lt; " after cursor-id:" &lt;&lt; cursor-&gt;id ; temp-&gt;next = cursor-&gt;next; cursor-&gt;next = temp; // inject new node into list. cursor = temp-&gt;next; cout &lt;&lt; " before id " &lt;&lt; cursor-&gt;id; } } cout &lt;&lt; "Node with id=" &lt;&lt; temp-&gt;id &lt;&lt; ", initials=" &lt;&lt; temp-&gt;initials &lt;&lt; ", and test score=" &lt;&lt; temp-&gt;score &lt;&lt; " added to the list.\n"; }else{ // if invalid input &amp; not escape value, print error if ( temp_id != ESC ) cout &lt;&lt; "!!! Invalid input !!!\n"; } } return 0; } </code></pre> <p>Output:</p> <pre><code>Please enter the student id, or -9999 to finish inputing: 654 Please enter the student initials: abc Please enter the student's test score: 99.9 Node with id=654, initials=abc, and test score=99.9 added to the list. Please enter the student id, or -9999 to finish inputing: 312 Please enter the student initials: qwe Please enter the student's test score: 54.8 Node with id=312, initials=qwe, and test score=54.8 added to the list. Please enter the student id, or -9999 to finish inputing: 987 Please enter the student initials: rty Please enter the student's test score: 87.5 DEBUG: while ( cursor-id:312 &lt; temp-id:987 &amp;&amp; cursor-next:0x1c26040 != NULL ) DEBUG: { old cursor:312, new cursor:654 } DEBUG: while ( cursor-id:654 &lt; temp-id:987 &amp;&amp; cursor-next:0 != NULL ) Segmentation fault </code></pre> <p>I have also tried the loop with <code>( ... cursor-&gt;next != 0 )</code> and <code>( ... cursor-&gt;next )</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload