/* memory.c Jeff Ondich, 13 Jan 2022 More thinking about memory, pointers, linked lists, etc. Get out a piece of paper and start drawing what you think things look like in memory. Stuff to understand: - type T vs. type T * - & as a unary operator ("address-of") - * as a unary operator (dereferencing, or "thing-pointed-to-by") - struct - . operator with structs - -> operator (p->field is the same as (*p).field) - sizeof - linked lists */ #include #include #include #define BUFFER_SIZE 10 ////////////////////////////////// // Play with & and * void experiment1() { printf("======= Experiment 1 =======\n"); char ch = 'b'; char *char_pointer = &ch; printf("Before: ch=%c, ch size=%ld, char_pointer=%p\n", ch, sizeof(ch), char_pointer); *char_pointer = 'x'; printf("After: ch=%c\n\n", ch); } ////////////////////////////////// typedef struct { char name[BUFFER_SIZE]; int leg_count; } animal_t; // Play with struct, ., and -> void experiment2() { printf("======= Experiment 2 =======\n"); animal_t animal; strcpy(animal.name, "tapir"); animal.leg_count = 4; printf("animal_t size: %ld\n", sizeof(animal)); printf("Before: name=%s, legs=%d\n\n", animal.name, animal.leg_count); animal_t *animal_pointer; animal_pointer = &animal; printf("animal pointer size: %ld\n", sizeof(animal_pointer)); (*animal_pointer).leg_count = 9; // animal_pointer->leg_count = 7; printf("After: name=%s, legs=%d\n\n", animal.name, animal.leg_count); } ////////////////////////////////// typedef struct node { char value[BUFFER_SIZE]; struct node *next; } node_t; typedef struct { node_t *head; } linked_list_t; // Print a linked list void print_list(linked_list_t *linked_list) { printf("["); if (linked_list != NULL) { node_t *current = linked_list->head; while (current != NULL) { printf(" %s", current->value); current = current->next; } } printf("]\n"); } // Build a linked list manually void experiment3() { printf("======= Experiment 3 =======\n"); linked_list_t linked_list; node_t first_node; node_t second_node; node_t third_node; linked_list.head = &first_node; strcpy(first_node.value, "goat"); first_node.next = &second_node; strcpy(second_node.value, "moose"); second_node.next = &third_node; strcpy(third_node.value, "coatimundi"); third_node.next = NULL; print_list(&linked_list); printf("\n"); } ////////////////////////////////// void free_list(linked_list_t *linked_list) { // ??? } // Same as experiment3, but using malloc void experiment4() { printf("======= Experiment 4 =======\n"); linked_list_t *linked_list; node_t *node; node_t *last_node; // Allocate and initialize the list linked_list = malloc(sizeof(linked_list_t)); if (linked_list == NULL) { return; } linked_list->head = NULL; // Allocate and initialize the first node node = malloc(sizeof(node_t)); if (node == NULL) { free_list(linked_list); return; } strcpy(node->value, "caribou"); linked_list->head = node; last_node = node; // Allocate and initialize the second node node = malloc(sizeof(node_t)); if (node == NULL) { free_list(linked_list); return; } strcpy(node->value, "kudu"); last_node->next = node; last_node = node; // Allocate and initialize the third and final node node = malloc(sizeof(node_t)); if (node == NULL) { free_list(linked_list); return; } strcpy(node->value, "vole"); last_node->next = node; node->next = NULL; print_list(linked_list); printf("\n"); free_list(linked_list); } ////////////////////////////////// int main() { experiment1(); //experiment2(); //experiment3(); //experiment4(); return 0; }