So the following code was presented in the lecture, and I'm struggling to understand why it was necessary to set n->next = null. David (Lecturer) did say it would be important to clean up potential garbage, but shouldn't the garbage end up being substituted anyway?
int main(int argc, char *argv[])
{
// Memory for numbers
node *list = NULL;
// For each command-line argument
for (int i = 1; i < argc; i++)
{
// Convert argument to int
int number = atoi(argv[i]);
// Allocate node for number
node *n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
n->number = number;
n->next = NULL;
// Prepend node to list
n->next = list;
list = n;
}
Since it's already followed with a new assignment of n->next = list. For the first run in the loop, list is pointing to null, and afterwards, it would point to the updated value. Garbage would be substituted anyway, so it doesn't seem like setting n->next = null is doing anything? The final node would have a NULL pointer and following nodes would be pointed in sequence. So is it possibly for comprehension? Or does it have significance, and what would it be??
n->next = NULL;
is not needed as the next line n->next = list;
overwrites n->next
.
Example is too trivial (or inaccurate) to demo the Lecturer's "important to clean up potential garbage" valid concern.