I have already solved the Segmentation Fault, which caused this problem, but I'd still like to know why this was happening:
I have a method which causes a segmentation fault later on in the code, but the printf statment, which is BEFORE the problematic code never gets excecuted.
element* insert_sorted(element *first, element *new_elem)
{
printf("%lld", new_elem->isbn);
// Some funny code which causes a Segmentation Fault
}
// Output: Segmentation Fault only, not even the expected first ISBN
But if I return before this code, it prints just fine
element* insert_sorted(element *first, element *new_elem)
{
printf("%lld", new_elem->isbn);
return NULL;
// Some funny code which causes a Segmentation Fault
}
// Output: Prints all the ISBN numbers just fine
And lastly, if I put a line break into the print, it prints the first two ISBNs and than it gives an Segmentation Fault.
element* insert_sorted(element *first, element *new_elem)
{
printf("%lld\n", new_elem->isbn);
// Some funny code which causes a Segmentation Fault
}
// Output: First two ISBNs and than a Segmentation Fault
Question 1:
How is it possible that a Segmentation Fault prevents printing text to the console? Is the order of execution broken??
Question 2:
How is it possible, that adding a character (\n
) changes this behaviour? And why would it still only print the first two times? (This function gets called about 20 times.)
Question 3:
How can I debug my code, when errors are 'eating' my debugging information? :(
(The code is part of a homework, so I can't post the entire code.)