2
void List_sort (struct nodeStruct **headRef){
    struct nodeStruct *tempNode = *headRef;
    struct nodeStruct *nextNode, *smallestNode;
        while (tempNode != NULL){
            nextNode = tempNode->next;
            smallestNode = tempNode;
            while (nextNode != NULL){
                if (nextNode->item < smallestNode->item) {
                    smallestNode = nextNode->next;
                }
                nextNode = nextNode->next;
            }
            int tempInt = tempNode->item;
            tempNode->item = smallestNode->item;
            smallestNode->item = tempInt;
            tempNode = tempNode->next;
        }   
}

Don't know why it's generating a seg fault, there should be no edits to NULL. Segmentation fault. 0x00000000004008d4 in List_sort (headRef=0x7fffffffe458) at list.c:130 130 tempNode->item = smallestNode->item; Anyone know why it's doing this?

Update: Performed the suggestions and no more seg fault, however does not sort correctly.

Value: 1
Value: 2
Value: 7
Value: 3
Value: 4
Value: 6
Value: 5
Value After Sort: 1
Value After Sort: 2
Value After Sort: 4
Value After Sort: 3
Value After Sort: 5
Value After Sort: 6
Value After Sort: 7
7
  • 1
    the if(tempNode == NULL) {} else {... check before while(tempNode != NULL) is redundant. Commented Jan 26, 2015 at 4:10
  • 2
    in while (nextNode != NULL){ if (nextNode->item < smallestNode->item) smallestNode = nextNode->next;, nextNode->next may be NULL, causing smallestNode to be NULL afterwards. I'd say the offending line should be smallestNode = nextNode instead. Commented Jan 26, 2015 at 4:18
  • @EOF jesus christ, could not see that small mistake. Thank you Commented Jan 26, 2015 at 4:21
  • It's always the small ones which trip one up... Commented Jan 26, 2015 at 4:23
  • Is there a reason not to close this as 'off-topic, trivial typo'? Commented Jan 26, 2015 at 4:49

1 Answer 1

1

This isn't right:

            if (nextNode->item < smallestNode->item) {
                smallestNode = nextNode->next;
            }

it should be:

            if (nextNode->item < smallestNode->item) {
                smallestNode = nextNode;
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.