#include
#include
#include
/* A completely correct, simple implementation of linked list */
struct LinkedListNode {
int i;
struct LinkedListNode* next;
};
struct LinkedListNode* head;
void goodMake() {
head = malloc (sizeof (struct LinkedListNode));
printf(“head = 0x%p\n”, head);
head->i = 100;
head->next = NULL;
}
void goodPrintHelper(struct LinkedListNode* node) {
if (node != NULL) {
printf(“here i am 0x%p\n”, node);
printf(“0x%x\n”, node->i);
goodPrintHelper(node->next);
}
}
void goodPrint() {
goodPrintHelper(head);
}
/* Some bad code that derefernces a dangling pointer */
char* bad;
void badStepOne() {
bad = malloc(16);
free(bad);
}
void badStepTwo() {
//strlcpy(bad, “abcdefghijklmno”, 16);
char* src = “abcdefghijklmno”;
for (int i = 0; i < 16; i++)
bad[i] = src[i];
}
/* What will happen and why? */
int main (int argc, char** argv) {
badStepOne();
goodMake();
goodPrint();
badStepTwo();
goodPrint();
}