The (current_ptr != NULL); part is redundant; you've already tested for the null pointer. worse, because it ends in a semi-colon, it causes that conditional statement to be treated as if it were the only thing guarded by the else statement. if you remove the (current_ptr != NULL); entirely, then it will prevent some of the segfaults you are experiencing.