This all could be because I'm just not understanding some basic things about what undefined behaviour is or how it manifests itself. The code in the snippet below is what I've got so far, it creates a linked list. I used the online IDE IDEone run it.
The main thing I'm not understanding is whether printf("\nhead value at the end = %d", (*head).value);
just above return
in the main function is producing UB or not.
That line prints out a 0, I expect UB since the clean_list function frees memory previously allocated to each list node (unless I'm doing that function wrong).
Shouldn't I get an error or something like that about undefined behaviour during runtime, or at the very least should the program not crash? Or could it be IDEone's safety measures that prevent UB? Or am I just not understanding something with regards to UB?
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node* next;
};
typedef struct node node_t;
node_t* create_linked_list(unsigned int number_of_nodes) {
node_t* head = NULL;
for (unsigned int i = 1; i < number_of_nodes + 1; ++i) {
node_t* node = malloc(sizeof(node_t));
node->value = i;
node->next = head;
head = node;
}
return head;
}
void print_list(node_t* head) {
node_t* temp = head;
while (temp != NULL) {
printf("%d - ", temp->value);
temp = temp->next;
}
printf("\n");
}
void clean_list(node_t* head) {
node_t* current = head;
while (current != NULL) {
node_t* temp = current;
current = current->next;
free(temp);
}
printf("\nclean_list complete \n");
}
int main(void) {
node_t* head = create_linked_list(15);
printf("head address at beginning = %p\n", head);
printf("head value at the beginning = %d\n\n", (*head).value);
// Prints a list from 1 to 15.
print_list(head);
clean_list(head);
printf("\nhead address at the end = %p", head);
printf("\nhead value at the end = %d", (*head).value);
return 0;
}