4 ms·
If the precondition states that the pointer is valid (which I expect it does), then this is not incorrect. If it does not state anything about the pointer, the
by Jweb_Guru 5y ago
If the precondition states that the pointer is valid (which I expect it does), then this is not incorrect. If it does not state anything about the pointer, then just checking for NULL isn't sufficient. It'd have to explicitly state "the pointer is either valid for the duration of the function, or NULL" for this to be a worthwhile check to perform.
- deleted 5y ago[deleted]
- gopiandcode 5y agoYes, that is correct. The precondition for the function states that the pointer should not be null. The specification for the listcopy is listed slightly lower down on the article as follows: // r :-> x ** linked_list(x,S) void listcopy(void **r) // r :-> y ** linked_list(y,S) ** linked_list(x,S) As the precondition explicitly states that r points to a value x, it can not be NULL, so no NULL checks are needed. I'm not sure what the sibling post is trying to imply with recursive calls though.
- vidarh 5y ago> I'm not sure what the sibling post is trying to imply with recursive calls though. I think that is based on a misunderstanding of what it being passed in at the point listcopy() is called in the body of listcopy(). The pointer passed in there can't be NULL due to how it is obtained, so no further checks is necessary.
- omnicognate 5y agoAFAICT the recursion doesn't mean the function can segfault due to a null pointer. It does however mean that it will cause a stack overflow if the list is sufficiently large, which makes it a bad implementation. It can also segfault if malloc returns zero as discussed above. Apart from these serious runtime behaviour flaws the code is also violently unreadable, which you strangely use as a way of attacking C programmers generally, as if this code is representative of human-coded C. This implementation would fail code review for numerous reasons anywhere I've ever worked. The article itself looks detailed and very interesting and I'll have a proper read of it if I get the time. You didn't need to make it so aggressive and clickbaity.