Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENEE150 Discussion 07 Section 0101 Adam Wang.

Similar presentations


Presentation on theme: "ENEE150 Discussion 07 Section 0101 Adam Wang."— Presentation transcript:

1 ENEE150 Discussion 07 Section 0101 Adam Wang

2 Overview More on project 2 Structs Malloc Quiz

3 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

4 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]

5 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’

6 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)

7 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]

8 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

9 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++;

10 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

11 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

12 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…

13 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

14 Arrays Arrays are basically pointers to the first element
int *array = (int *) malloc (10 * sizeof(int)); or int *array = (int *) calloc (10 * sizeof(int));

15 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

16 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 quiz2.c


Download ppt "ENEE150 Discussion 07 Section 0101 Adam Wang."

Similar presentations


Ads by Google