I saw a simple C program:
//create a pointer to 3 bytes on heap
char *start = malloc(3);
*start = 'u';
*(start + 1) = 'v';
*(start + 2) = 'w';
printf("%s has %zu characters.\n", start, strlen(start));
// Free the memory so that it can be reused
free(start);
//Why we need to set start = NULL if we have already freed the memory above
start = NULL;
I understand everything except the last line start = NULL;
, why we need to set it to NULL
? Is it just to make the pointer point to a NULL
instead of non-sense memory space?
Is start = NULL;
a must action or nice-to-have action?