What I was suggesting for checking if L is valid is this:
Code:
void InsertNewLast(dataItem value, NodeType **L)
{
if (L == NULL)
// somebody passed in a NULL pointer for L, we can't continue, so return error
// if L is NULL, and we try the next line, *L will seg fault, see method 1 below
// here we can still seg fault if L was passed in with a bad address, as in method 2 below
if (*L == NULL) {
*L = N;
}
}
If the code that calls InsertNewLast is not correct, then you can get a seg fault:
Code:
dataItem value;
NodeType **L1 = NULL;
NodeType **L2;
NodeType *L3 = NULL;
InsertNewLast(value, L1); // L1 doesn't point to a valid piece of memory, but this can be protected against by the first if check above
InsertNewLast(value, L2); // L2 doesn't point to a valid piece of memory, and we can't protect against it, it's a seg fault waiting to happen
InsertNewLast(value, &L3); // This is the correct way to pass in a list to add to it