ENEE150 Discussion 07 Section 0101 Adam Wang
Overview More on project 2 Structs Malloc Quiz
Reading in the title and Author One big loop that runs until you’re done with the file In the loop, write your code that parses the title, author, time, categories, instructions, and ingredients When the loop starts again that same code should work for the next recipe
Method 1 (From last week) We have read in everything into raw_recipes[MAX_SIZE] We need to find the character right after “Recipe: ” Use a counter that will move across the entire array int i = 0 Reference the character with raw_recipes[i]
Method 1 We’ll keep moving it until we see the ‘:’ character Then we’ll move until we hit the first non-space character raw_recipes[i] != ‘ ’ Alternatively, just bump i up by 6 Then every char until the ‘\n’ we copy into the titles array titles[index] = raw_recipes[i] Don’t forget to append the ‘\0’ char at the end to make it a string titles[index] = ‘\0’
Method 2 (maybe more intuitive) We’ll use our string methods to find the title Create a new pointer, set it to raw_recipes Use strstr to find “Recipe: “ Bump the pointer up by 13 Now you can copy it in char by char like method 1 (this time you’re using pointer arithmetic though)
Method 2 You could alternatively use strncpy to copy in the title Use strstr again to find the next “\n” char Use strncpy to copy it into the title array Source would be p_start Destination is the title array Number of chars is p_end – p_start Append the ‘\0’ to title[p_end – p_start]
Structs struct books { char title[50]; char author[50]; char subject[100]; int book_id; }; struct books s1 = {“a”, “b”, “c”, 20}; Everything is stored right next to each other (more or less) s1 = s2 is VALID for structs! s1 == s2 is NOT VALID for structs
Typedef typedef struct data { float score1; int score2; int score3; int score4; } data_el; data_el example = {2.5,7,8,9}; // just makes typing it out easier example.score4++;
Struct pointers typedef struct my_structure { char name[20]; int number; int rank; } my_struct; my_struct var = {“bob”, 5, 10}; my_struct *ptr = &var; printf(“Name: %s”, ptr->name); // same as (*ptr).name
Malloc Dynamically allocating memory for new variables Memory gets taken from the heap Regular variables are made on the runtime stack + Main advantage is now we don’t have to waste memory - Taking from the heap is a low slower than stack Computer has to figure out where to find the memory (this is called fragmentation) Heap is considered global memory whereas variables on the stack are restricted to the function they’re in
Malloc int *ptr = (int *)malloc(sizeof(int)); heap Why do we use sizeof instead of 4 bytes? Not all computers will use 4 bytes for a size of an integer Malloc normally returns a void pointer; this cannot be dereferenced, but can be casted to anything heap ptr 0xEF 0xA4 0x37 0x9D 0xAE536…
Free Free(ptr); Computer knows this is memory is no longer needed Pointer that points to freed memory is called a dangling pointer C does not have automatic garbage collection like other languages
Arrays Arrays are basically pointers to the first element int *array = (int *) malloc (10 * sizeof(int)); or int *array = (int *) calloc (10 * sizeof(int));
Very important concepts to know Stack vs heap When would you use either Tradeoffs How does computer visualize memory Void pointers, NULL pointers, dangling pointers, when they are dangerous Malloc; how it works Memory leaks: occurs when user is not freeing memory Tradeoffs in usage
Quiz wget ece.umd.edu/~aw97/quizzes/quiz2.c Write a function count_x that takes in a string and returns the number of x’s count_x("abcxyz") ----> 1 count_x("20458x4820x;?$#") ----> 2 count_x("abc") ----> 0 You MUST use pointer arithmetic, no array indexing Submit under assignment 101: submit 2017 fall enee 150 0101 101 quiz2.c