Skip to content

Instantly share code, notes, and snippets.

@johnsogg
Created January 25, 2013 21:49
Show Gist options
  • Save johnsogg/4638191 to your computer and use it in GitHub Desktop.
Save johnsogg/4638191 to your computer and use it in GitHub Desktop.
seg fault is somewhere
void remove(node** parent, int offset) {
node* cursor = *parent;
if (cursor->next==NULL || offset > size(*parent)) {
return;
} else if (offset==0) {
cursor=cursor->next;
} else {
for (int i=1;i<offset;i++) {
cursor=cursor->next;
}
cursor->next=cursor->next->next;
}
}
@johnsogg
Copy link
Author

What happens if you pass in an empty list?
What happens if *cursor is null?

Segfaults happen when you use the arrow operator on a NULL pointer.
I would debug this by putting a cout in front of every line that contains an arrow operator, and print the value of the pointer. When it is null, the pointer should be 0.

Consider what happens if cursor is null. it gets to line 5, and enters that else clause. Then it dereferences cursor, and causes a segfault.

Not saying that's where your segfault is actually happening, but that is one spot it could happen.

@coaw8679
Copy link

Okay, so I've changed my code to this:

void remove(node** parent, int offset)
{
cout<<"all good"<<endl;
node* cursor = *parent;
if (cursor==NULL||offset>=size(*parent))
{
cout<<"all good"<<endl;
return;
}

else if (offset==0)
{
cout<<"all good"<<endl;
cursor=cursor->next;
}

else
{
for (int i=1;i<offset;i++)
{
cursor=cursor->next;
}
cout<<"all good"<<endl;
cursor->next=cursor->next->next;
}

}

My initial thought was that it should be if (*cursor==NULL) for that part of the if statement, but the compiler didn't like that for some reason. I added a test for the empty list case, removing from the end of the list, a too large offset, and removing from the middle of the list to my copy of the driver and it all seems to work as it should, but retrograde is still giving me a seg fault

@johnsogg
Copy link
Author

well,
*cursor is a node.
but,
cursor is a node*
cursor->next is also a node*

There are a bunch of syntax errors in the above, so I assume you didn't paste in literally what you have.

At any rate, if cursor (the node pointer) is null, then calling cursor->next will segfault.

Paste in the RG output.

@coaw8679
Copy link

Created a RetroGrade instance.
Assignment: hw_1_linked_lists
Student: connor.awe@colorado.edu
Student Files: /tmp/tmpsHfsFt/linked_list.cpp
File: /tmp/tmpsHfsFt/linked_list.cpp
Determined language = cpp
About to copy files to working directory: /tmp/tmpVxLqvb
Copying Student files...
... /tmp/tmpsHfsFt/linked_list.cpp
Copied Student files.
Copying Instructor files...
... linked_list.h
... description.json
... linked_list_test.cpp
... RetroPrinter.h
... Makefile
... RetroPrinter.cpp
Copied Instructor files.
Copy files successful
Invoking grade script
Changing directory to /tmp/tmpVxLqvb
... successfully changed directory.
Using description files: /home/gabe/grading-scripts/hw_1_linked_lists/description-common.json /home/gabe/grading-scripts/hw_1_linked_lists/cpp/description.json
... got assignment instance.
Invoking grade()...
Description File: /home/gabe/grading-scripts/hw_1_linked_lists/description-common.json
Description File: /home/gabe/grading-scripts/hw_1_linked_lists/cpp/description.json
Assignment: Linked Lists
Language: cpp
Points Possible: 15
Instructor file(s) in place.
Student file(s) in place.

Running Build command: make...
... return value: True

g++ -I/usr/local/gtest-1.6.0/include -g -Wall -Wextra -c ./linked_list.cpp
g++ -I/usr/local/gtest-1.6.0/include -g -Wall -Wextra -c ./linked_list_test.cpp
g++ -I/usr/local/gtest-1.6.0/include -g -Wall -Wextra -c ./RetroPrinter.cpp
g++ -I/usr/local/gtest-1.6.0/include -g -Wall -Wextra -pthread linked_list.o linked_list_test.o RetroPrinter.o /usr/local/gtest-1.6.0/make/gtest_main.a -o linked_list_test

Running Unit Test command: ./linked_list_test...
... return value: False

Segmentation fault

Parsing outfile...
Checking for ommitted tests


  • WARNING: Tests performed were not the same as those prescribed.
  • missing: Contains
  • missing: Size

Completed unit test command.
result: {'Insert': (3, 3), 'InitNode': (1, 1), 'InsertData': (1, 1), 'Remove': (0, 3), 'Report': (2, 2), 'AppendData': (1, 1), 'Append': (2, 2)}
errors: FYI: should be 4, 30, 20, 10: 4 30 20 10
FYI: should be 4, 30, -8, 20, 10: 4 30 -8 20 10
FYI: should be 4, 30, -8, 20, 10, 99: 4 30 -8 20 10 99
FYI: should be 5, 7, 98, -34: 5 7 98 -34
FYI: should be 5, 7, 20, 98, -34: 5 7 20 98 -34
FYI: should be 5, 7, 20, 98, -34, 800: 5 7 20 98 -34 800
7 != 86
Failure at ./linked_list_test.cpp:198:
Value of: expect_all(vals2, 2, &top)
Actual: false
Expected: true

Done reporting result and errors.
FYI: should be 4, 30, 20, 10: 4 30 20 10
FYI: should be 4, 30, -8, 20, 10: 4 30 -8 20 10
FYI: should be 4, 30, -8, 20, 10, 99: 4 30 -8 20 10 99
FYI: should be 5, 7, 98, -34: 5 7 98 -34
FYI: should be 5, 7, 20, 98, -34: 5 7 20 98 -34
FYI: should be 5, 7, 20, 98, -34, 800: 5 7 20 98 -34 800
7 != 86
Failure at ./linked_list_test.cpp:198:
Value of: expect_all(vals2, 2, &top)
Actual: false
Expected: true

Output is in the file: /tmp/tmpVxLqvb/unit_test_output.txt

@coaw8679
Copy link

It gives the same message even when I comment out everything in the remove function (except the brackets so that the driver will run).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment