Student question: Receive still reachable byts, but i am closing the file how can I fix? | Code context (simplify for kids): int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("----

Question: Receive still reachable byts, but i am closing the file how can I fix?


Student question: i am receiving 472 still reachable byts in this code even though I'm closing file, why? | Code context (simplify for kids): int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { fclose(file); printf("Name: %s\n", user->name);

Question: i am receiving 472 still reachable byts in this code even though I'm closing file, why?


Student question: Receiving still reachable byts why? whats a fix | Code context (simplify for kids): int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { fclose(file); printf("Name: %s\n", user->name);

Question: Receiving still reachable byts why? whats a fix


Student question: how to cast deserialized object without unchecked type error?

Question: how to cast deserialized object without unchecked type error?


Student question: how can i write a newline without creating new write() | Code context (simplify for kids): write(client->sock_fd, users->name, strlen(users->name)

Question: how can i write a newline without creating new write()


Goal: Define a string literal called season that has the value "winter". | Code context (simplify for kids): char season[] = 'winter'; char holiday[20] = "Valentine's Day";


Goal: Define a string literal called season that has the value "winter". | Code context (simplify for kids): char season[] = "winter"; char holiday[20] = "Valentine's Day";


Student question: can i compare unsigned long and int

Question: can i compare unsigned long and int


Goal: return 2 if the string name is longer than the value stored in MAX_NAME | Code context (simplify for kids): //first check if the name is too long if((int)strlen(name) > MAX_NAME){ return 2; }


Question: how do i use gdb


Question: how do i use valgrand


Student question: are pointers in functions freed after the function executes

Question: are pointers in functions freed after the function executes


Student question: are pointers created in functions freed after the function executes

Question: are pointers created in functions freed after the function executes


Student question: are pointers created in function in the heap

Question: are pointers created in function in the heap


Student question: does fgets work on files that are .ascii in c

Question: does fgets work on files that are .ascii in c


Student question: how do i read the contents of an ascii file and print the contents line by line in c?

Question: how do i read the contents of an ascii file and print the contents line by line in c?


Student question: What is the return value when the input stream to scanf is closed?

Question: What is the return value when the input stream to scanf is closed?


Student question: is this a valid syntax else if (0 <= temp <= 9)

Question: is this a valid syntax else if (0 <= temp <= 9)


Student question: Is this a valid syntax in C else if (0 <= temp <= 9)

Question: Is this a valid syntax in C else if (0 <= temp <= 9)


Student question: what is being compared here? | Code context (simplify for kids): while (scanf("%d", &input) != "EOF") {

Question: what is being compared here?


Student question: what is the return type of scanf | Code context (simplify for kids): while (scanf("%d", &input) != "EOF") {

Question: what is the return type of scanf


Student question: in C, how to convert int to string

Question: in C, how to convert int to string


Student question: how to compare an int with a string

Question: how to compare an int with a string


Student question: how would i compare the return value of scanf with a string, without compilation error

Question: how would i compare the return value of scanf with a string, without compilation error


Student question: What would be an example of how to use scanf

Question: What would be an example of how to use scanf


Student question: does a c file need a main

Question: does a c file need a main


Student question: what is stdout used for

Question: what is stdout used for


Student question: how to allocate space with malloc

Question: how to allocate space with malloc


Student question: How to change a file name in C

Question: How to change a file name in C


Student question: generate a detailed documentation of `rename` with usage examples and explanations

Question: generate a detailed documentation of `rename` with usage examples and explanations


Student question: generate a detailed documentation of `rename(old_name, new_name)` with usage examples and explanations

Question: generate a detailed documentation of `rename(old_name, new_name)` with usage examples and explanations


Student question: How to check if a file can be opened?

Question: How to check if a file can be opened?


Student question: How to iterate through an ascii code.

Question: How to iterate through an ascii code.


Student question: How to iterate through an ascii file to print out everything

Question: How to iterate through an ascii file to print out everything


Student question: how to print a time_t?

Question: how to print a time_t?


Student question: how to print a blank line?

Question: how to print a blank line?


Student question: how to make a variable with the current time

Question: how to make a variable with the current time


Student question: how to check if a file exitsts

Question: how to check if a file exitsts


Student question: how to check if a file can be opened

Question: how to check if a file can be opened


Student question: Why is curr always null? | Code context (simplify for kids): void list_users(const User *curr) { printf("User List\n"); while(curr != NULL){ printf(" %s\n", curr->name); curr = curr->next; } }

Question: Why is curr always null?


Goal: fix segmentation fault | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); User *curr = *user_ptr_del; if(leaver == NULL || curr == NULL){ return 1; } if(curr == leaver){ *user_ptr_del = curr->next; } for(int i = 0; i < 10; i++){ if(curr->next->friends[i]==leaver){ curr-


Student question: how do you remove an element from an array inside a struct

Question: how do you remove an element from an array inside a struct


Student question: how to make an array of the same type as: struct user *friends[MAX_FRIENDS];

Question: how to make an array of the same type as: struct user *friends[MAX_FRIENDS];


Student question: how to change an array to point to another array

Question: how to change an array to point to another array


Student question: How to point an array of equal size with an array

Question: How to point an array of equal size with an array


Goal: remove segmentation fault | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); //The user we want to delete User *curr = *user_ptr_del; //for traversal if(leaver == NULL || curr == NULL){ return 1; } if(curr == leaver){ *user_ptr_del = curr->next; } else if(curr != leaver){


Goal: help remove segmentation fault | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); //The user we want to delete User *curr = *user_ptr_del; //for traversal if(leaver == NULL || curr == NULL){ return 1; } if(curr == leaver){ *user_ptr_del = curr->next; } else if(curr != leaver){


Goal: remove non-zero offset errors | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); //The user we want to delete User *curr = *user_ptr_del; //for traversal if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it return 1; } if(curr ==


Goal: help with freeing memory stored in leaver | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); //The user we want to delete User *curr = *user_ptr_del; //for traversal if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it return 1; } if(curr ==


Student question: how do you free an element in a linked list struct?

Question: how do you free an element in a linked list struct?


Student question: given these structs, how would you free a user? | Code context (simplify for kids): typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next;

Question: given these structs, how would you free a user?


Student question: given the structs, make a program that frees the memory associated with a user's friends | Code context (simplify for kids): typedef struct user { char name[32]; char profile_pic[32]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[10]; struct user *next; } User; typedef struct post { char author[32]; char *contents; time_t *date; struct post *next; } Post;

Question: given the structs, make a program that frees the memory associated with a user's friends


Goal: fix memory leak | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); //The user we want to delete User *curr = *user_ptr_del; //for traversal if(leaver == NULL || curr == NULL){ //check if leaver exists and if the userlist has anything in it return 1; } if(curr ==


Student question: What does char arr[] mean?

Question: What does char arr[] mean?


Student question: is char arr[] stack or read only memory

Question: is char arr[] stack or read only memory


Student question: what does a regular file mean

Question: what does a regular file mean


Question: how to chmod


Student question: How do we make sure only a parent can call fork?

Question: How do we make sure only a parent can call fork?


Student question: Why is each the pid of each child class the same? | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");

Question: Why is each the pid of each child class the same?


Student question: How do you make sure a process only creates one fork?

Question: How do you make sure a process only creates one fork?


Student question: how could I make sure each process only produces one distinct process? | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");

Question: how could I make sure each process only produces one distinct process?


Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define MAXLINE 32 #define MAX_PASSWORD 10 #define PASSWORD_FILE "pass.txt" int main(void) { int n, user_length; char userid[MAXLINE]; char password[MAXLINE]; if ((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) { pe


Task: Your task is to complete checkpasswd.c, which reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.


Student question: What does this code do? | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> int main() { char phone[11]; int num; int error = 0; scanf("%10s", phone); while (scanf("%d", &num) > 0) { if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { printf("%c\n", phone[num]); } else { printf("ERROR\n"); error = 1; } } return e

Question: What does this code do?


Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> int main() { char phone[11]; int num; int error = 0; scanf("%10s", phone); while (scanf("%d", &num) > 0) { if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { printf("%c\n", phone[num]); } else { printf("ERROR\n"); error = 1; } } return e


Student question: How do you convert string to int?

Question: How do you convert string to int?


Goal: avoid forward declaration error/ | Code context (simplify for kids): void tokenize(char *line, Action *act; Action *next_a){ int white_spaces = 0; int count = 0; for (int i = 1; i < MAXLINE; i++){ if (line[i-1] == ' ' &&line[i] != ' '){ white_spaces += 1; } if (line[i] == '\t' || line[i] == '\n' || line[i] == '\r'){ break; } } char **args = malloc(sizeof(char*)*(whit


Student question: what should i do if i encounter a forward declaration error?

Question: what should i do if i encounter a forward declaration error?


Student question: How to create a post with the current time? | Code context (simplify for kids): typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post;

Question: How to create a post with the current time?


Student question: Can you generate a c code segment that creates a post with the current timestamp? | Code context (simplify for kids): typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post;

Question: Can you generate a c code segment that creates a post with the current timestamp?


Student question: When I declare a struct, can I use it as a pointer the same way as an array?

Question: When I declare a struct, can I use it as a pointer the same way as an array?


Student question: how do I create a struct

Question: how do I create a struct


Student question: How do I assign values to parameters of structs

Question: How do I assign values to parameters of structs


Student question: When I use the dot operator on structs, does it give me a pointer?

Question: When I use the dot operator on structs, does it give me a pointer?


Student question: How do I assign a value to a struct?

Question: How do I assign a value to a struct?


Student question: How do I close a bitmap file

Question: How do I close a bitmap file


Student question: how do I deallocate memory

Question: how do I deallocate memory


Student question: what does segmentation fault mean

Question: what does segmentation fault mean


Question: how do I use gdb


Student question: how do I use gdb to find the value of a variable

Question: how do I use gdb to find the value of a variable


Student question: how to step out using gdb

Question: how to step out using gdb


Student question: Can you explain a little more from your previous answer?

Question: Can you explain a little more from your previous answer?


Student question: what does "expression must be a modifiable lvalue" mean?

Question: what does "expression must be a modifiable lvalue" mean?


Student question: What happens to the declaration on line 2 after the c pre-processor? | Code context (simplify for kids): #define MAXNAME = 32; char name[MAXNAME];

Question: What happens to the declaration on line 2 after the c pre-processor?


Student question: I'm getting the error "Conditional jump or move depends on uninitialised value(s)" from this function at line 26. | Code context (simplify for kids): int print_user(const User *user) { // Return if the user doesn't exist if (user == NULL) { return 1; } // Print the user's profile pic only if the file exists FILE *file = fopen(user->profile_pic, "r"); if (file != NULL) { char line[MAX_NAME + 100]; while(fgets(line, MAX_NAME + 100, file) != NULL) {

Question: I'm getting the error "Conditional jump or move depends on uninitialised value(s)" from this function at line 26.


Student question: Which macro is used with wait's status to check if the child process was terminated by a signal?

Question: Which macro is used with wait's status to check if the child process was terminated by a signal?


Student question: What is the name for a child process whose parent terminates before it does?

Question: What is the name for a child process whose parent terminates before it does?


Question: how do I use stat


Student question: I'm confused how to use stat to find last modified time. Here's what I have. | Code context (simplify for kids): struct stat *stats = NULL; stat("testy.txt", stats);

Question: I'm confused how to use stat to find last modified time. Here's what I have.


Student question: How does setitimer work

Question: How does setitimer work


Student question: How do I use a timer to have a program wait

Question: How do I use a timer to have a program wait


Code context (simplify for kids): void fib(int **arr, int count){ *arr = malloc(sizeof(int) * count); if (count == 1){ *arr[0] = 0; printf("*arr[0] %d", *arr[0]); } else { *arr[0] = 0; printf("*arr[0] %d", *arr[0]); *arr[1] = 1; printf("*arr[1] %d", *arr[1]); for (int i = 2; i < count; i++){ *arr[i] = *arr[i-2] + *arr[i-1]; printf("


Goal: read each line from standard input and parse it | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *, char *); int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1;


Goal: parse each line read from the standard input. The first character from each line is not being read correctly. | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. int check_permissions(char *, char *); int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1;


Student question: how can I read from standard input, line by line (separated by newline)?

Question: how can I read from standard input, line by line (separated by newline)?


Goal: check if the SIN given by command line argument is valid, using the Luhn algorithm | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> int populate_array(int, int *); int check_sin(int *); int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { return 2; } int sin = strtol(argv[1], NULL, 10); int sin_array[10]; // TODO: Parse arguments and then


Student question: i keep getting the error subscripted value is neither array nor pointer nor vector

Question: i keep getting the error subscripted value is neither array nor pointer nor vector


Student question: what does this mean c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' E - 16416123456733 E ? ^ E + 1 E 6 E 4161234567 E 3 E ? + + ^

Question: what does this mean c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' E - 16416123456733 E ? ^ E + 1 E 6 E 4161234567 E 3 E ? + + ^


Student question: how do I fix this error c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' E - 16416123456733 E ? ^ E + 1 E 6 E 4161234567 E 3 E ? + + ^ | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c", arg); } else { printf("ERROR"); return 1; } } return 0; }

Question: how do I fix this error c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' E - 16416123456733 E ? ^ E + 1 E 6 E 4161234567 E 3 E ? + + ^


Goal: why does this code raise this error c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '16416123456733' != '1\n6\n4161234567\n3' E - 16416123456733 E ? ^ E + 1 E 6 E 4161234567 E 3 E ? + + ^ | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c", arg); } else { printf("ERROR"); return 1; } } return 0; }


Goal: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3' E - 1 E ? - E + 1 E - 6 E ? - E + 6 E - 41612345673 E ? -- E + 4161234567 E 3 | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c", arg); } else { printf("ERROR"); return 1; } } return 0; }


Goal: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1 \n6 \n41612345673 \n3' != '1\n6\n4161234567\n3' E - 1 E ? - E + 1 E - 6 E ? - E + 6 E - 41612345673 E ? -- E + 4161234567 E 3 | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c \n", arg); } else { printf("ERROR"); return 1; } } return 0


Goal: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' E 1 E 6 E 4161234567 E - 3 E 3 | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c\n", arg); } else { printf("ERROR"); return 1; } } return


Goal: Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the first input value but then it repeatedly reads integers until standard input is closed. (Hint: Use a while loop that checks the return value of scanf to accomplish this. Check the  | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c\n", arg); } else { printf("ERROR"); return 1; } } return


Goal: I have this error: c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' I want my code to do this. Your task is to write a C program called phone_loop.c. This program will again read from standard input using scanf and take no command-line arguments. Similar to phone.c, this program reads a 10-character string as the f | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c\n", arg); } else { printf("ERROR"); return 1; } } return


Student question: in C how can I check if standard input is closed using scanf

Question: in C how can I check if standard input is closed using scanf


Goal: why does my code raise this error? c_helper.py:148: in _t self.assertEqual(stdout, expected_stdout) E AssertionError: '1\n6\n4161234567\n3\n3' != '1\n6\n4161234567\n3' E 1 E 6 E 4161234567 E - 3 E 3 | Code context (simplify for kids): #include <stdio.h> int main() { char phone[11]; scanf("%s", phone); int val = 1; int num; while (val == 1) { val = scanf("%d", &num); if (num == -1) { printf("%s\n", phone); } else if (num >= 0 && num <= 9) { char arg = phone[num]; printf("%c\n", arg); } else { printf("ERROR"); return 1; } } return


Student question: how can scanf read certain lines

Question: how can scanf read certain lines


Student question: how to use scanf to read permissions and file size from redirected input

Question: how to use scanf to read permissions and file size from redirected input


Student question: what is this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

Question: what is this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.


Student question: how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i <= n; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } }

Question: how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.


Student question: fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i < n + 1; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } } int main(int argc, char **argv) { /* do not cha

Question: fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.


Student question: how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i <= n; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } }

Question: how to fix this error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.


Student question: error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. why does changing loop condition <= to < fix the problem? | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i <= n; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } }

Question: error malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed. why does changing loop condition <= to < fix the problem?


Student question: why would an incorrect loop condition result in malloc error | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i <= n; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } }

Question: why would an incorrect loop condition result in malloc error


Student question: why would an incorrect loop condition result in malloc error, but only for certain n values (such as n = 10, n = 14) | Code context (simplify for kids): void fib(int **heap_loc, int n) { *heap_loc = malloc(sizeof(int) * n); for (int i = 0; i <= n; i++) { if (i == 0) { (*heap_loc)[i] = 0; } else if (i == 1) { (*heap_loc)[i] = 1; } else { (*heap_loc)[i] = (*heap_loc)[i - 1] + (*heap_loc)[i - 2]; } } }

Question: why would an incorrect loop condition result in malloc error, but only for certain n values (such as n = 10, n = 14)


Code context (simplify for kids): int *build_array(char **strs, int size) { int *ints = malloc(sizeof(int) * size); for (int i = 0; i < size; i++) { ints[i] = (int) strtol(strs[i], NULL, 10); } return ints; }


Code context (simplify for kids): int **split_array(const int *s, int length) { int **arr_indexes = malloc(2 * sizeof(int*)); int even_count = 0; int odd_count = 0; for (int i = 0; i < length; i++) { if (s[i] % 2 == 0) { even_count += 1; } else { odd_count += 1; } } int *even_pt = malloc(even_count * sizeof(int)); int *odd_pt = mall


Student question: which line causes error | Code context (simplify for kids): int **split_array(const int *s, int length) { int **arr_indexes = malloc(2 * sizeof(int*)); int even_count = 0; int odd_count = 0; for (int i = 0; i < length; i++) { if (s[i] % 2 == 0) { even_count += 1; } else { odd_count += 1; } } int *even_pt = malloc(even_count * sizeof(int)); int *odd_pt = mall

Question: which line causes error


Student question: why does arr_indexes[0] = even_pt; raise an error and how to fix it? | Code context (simplify for kids): int **split_array(const int *s, int length) { int **arr_indexes = malloc(2 * sizeof(int*)); int even_count = 0; int odd_count = 0; for (int i = 0; i < length; i++) { if (s[i] % 2 == 0) { even_count += 1; } else { odd_count += 1; } } int *even_pt = malloc(even_count * sizeof(int)); int *odd_pt = mall

Question: why does arr_indexes[0] = even_pt; raise an error and how to fix it?


Student question: with input ./count_large 1000 rwx------ with command line input total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c why does the function return 1 instead of 2 | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. // int check_permissions(char *, char *); int check_permissions(char *a, char *b) { for (int i = 0; i < 9; i++) { if (b[i] == '-') { ; } else if (b[i] != a[i]) { return 1; } }

Question: with input ./count_large 1000 rwx------ with command line input total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c why does the function return 1 instead of 2


Student question: what's wrong with this code | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. // int check_permissions(char *, char *); int check_permissions(char *a, char *b) { for (int i = 0; i < 9; i++) { if (b[i] == '-') { ; } else if (b[i] != a[i]) { return 1; } }

Question: what's wrong with this code


Student question: which part of the code is wrong | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. // int check_permissions(char *, char *); int check_permissions(char *a, char *b) { for (int i = 0; i < 9; i++) { if (b[i] == '-') { ; } else if (b[i] != a[i]) { return 1; } }

Question: which part of the code is wrong


Goal: print out permissions match and file size is larger or equal | Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. // int check_permissions(char *, char *); int check_permissions(char *a, char *b) { for (int i = 0; i < 9; i++) { if (b[i] == '-') { ; } else if (b[i] != a[i]) { return 1; } }


Code context (simplify for kids): #include <stdio.h> #include <stdlib.h> // TODO: Implement a helper named check_permissions that matches the prototype below. // int check_permissions(char *, char *); int check_permissions(char *a, char *b) { for (int i = 0; i < 9; i++) { if (b[i] == '-') { ; } else if (b[i] != a[i]) { return 1; } }


Student question: how can I ignore first line in scanf

Question: how can I ignore first line in scanf


Student question: ignore the first character using scanf

Question: ignore the first character using scanf


Student question: convert int to char[9]

Question: convert int to char[9]


Question: convert int to array


Question: index an int


Goal: fix this code | Code context (simplify for kids): int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; while (curr != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; }


Code context (simplify for kids): /* Convert an array of args to a single space-separated string in buffer. Returns buffer. Note that memory for args and buffer should be allocated by the caller. */ char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args


Student question: what's a buffer? | Code context (simplify for kids): /* Convert an array of args to a single space-separated string in buffer. Returns buffer. Note that memory for args and buffer should be allocated by the caller. */ char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args

Question: what's a buffer?


Code context (simplify for kids): include <stdio.h> int main() { char name[30]; printf("Hello CSC209 students!\n"); fgets(name, 30, stdin); printf("Hi %s\n", name); return 0; }


Student question: what is standard input?

Question: what is standard input?


Student question: what does scanf do in c?

Question: what does scanf do in c?


Code context (simplify for kids): scanf("%[^\n]", line);


Code context (simplify for kids): if (pid == 0) { //child process; read from pipe and write to stdin of validate close(pipefd[1]); if (dup2(pipefd[0], STDIN_FILENO) == -1) { perror("Dup2 failed"); exit(1); } close(pipefd[0]); execl("./validate", "validate", NULL); }


Student question: when execl calls validate, where is the standard input read from? | Code context (simplify for kids): if (pid == 0) { //child process; read from pipe and write to stdin of validate close(pipefd[1]); if (dup2(pipefd[0], STDIN_FILENO) == -1) { perror("Dup2 failed"); exit(1); } close(pipefd[0]); execl("./validate", "validate", NULL); }

Question: when execl calls validate, where is the standard input read from?


Student question: what does execl return?

Question: what does execl return?


Goal: Assign a dynamically allocated char * array of length 4 to last_names. Then, allocate a character array of length 20 for each element of the array pointed to by last_names. | Code context (simplify for kids): int main() { char **last_names; last_names = malloc(sizeof(char*)); last_names[0] = malloc(sizeof(char) * 20); last_names[1] = malloc(sizeof(char) * 20); last_names[2] = malloc(sizeof(char) * 20); last_names[3] = malloc(sizeof(char) * 20); return 0; }


Goal: rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to. | Code context (simplify for kids): }int main(int argc, char **argv) { float *rainfall; float rain_today; // rainfall has been dynamically allocated space for a floating point number. // Both rainfall and rain_today have been initialized in hidden code. // Assign the amount in rain_today to the space rainfall points to. return 0; }


Goal: rainfall has been dynamically allocated space for a floating point number. Both rainfall and rain_today have been initialized in hidden code. Assign the amount in rain_today to the space rainfall points to. | Code context (simplify for kids): int main(int argc, char **argv) { float *rainfall; float rain_today; // rainfall has been dynamically allocated space for a floating point number. // Both rainfall and rain_today have been initialized in hidden code. // Assign the amount in rain_today to the space rainfall points to. // Submitted co


Goal: Returns the sum of two integers in the string input_line. input_line is a string containing two integers separated by one or more spaces. Assume that the string will not be more than 20 characters long and that it contains two integers separated only by whitespace. There may also be spaces before the first integer and/or after the last, but there won't be any other characters in the string. For ex | Code context (simplify for kids): int get_sum(const char *input_line) { }


Student question: is the sizeof(char *) and sizeof(&char) the same?

Question: is the sizeof(char *) and sizeof(&char) the same?


Goal: Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | Code context (simplify for kids): int main(){ char greetings[15] = "salutations"; char *farewell[] = "goodbye"; }


Goal: Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | Code context (simplify for kids): int main(){ char greetings[15] = "salutations"; char *farewell = "goodbye"; }


Goal: Define a stack-allocated string called greetings that has space for 15 characters (including the null terminator) and set its initial value to "salutations". Then define a string literal with the name farewell that has the value "goodbye". | Code context (simplify for kids): #include <studio.h> int main() { char greetings[15] = "salutations"; char *farewell = "goodbye"; }


Student question: Why is 'arrays' uninitialized? | Code context (simplify for kids): int **split_array(const int *s, int length) { int **arrays; int half_length; if (length%2 == 0){ half_length = length/2 + 1; } else { half_length = length/2; } int *array1 = malloc(sizeof(int) * half_length); int *array2 = malloc(sizeof(int) * half_length); for (int i = 0; i < length; i = i + 2){ ar

Question: Why is 'arrays' uninitialized?


Goal: Return a pointer to an array of ints with size elements. - strs is an array of strings where each element is the string representation of an integer. - size is the size of the array | Code context (simplify for kids): int *build_array(char **strs, int size) { int *ptr; for (int i = 0; i < size; i++){ ptr[i] = (int) strtol(strs[i], NULL, 10); } return ptr; }


Goal: You are required to write (and use) a helper function named check_permissions that takes two 9-element character arrays as arguments and returns an integer. (The prototype for this function is in the starter code.) The first array will represent the permission field of a file and the second will represent the permissions that are required. The function will return 0 if the file has all the require | Code context (simplify for kids): int check_permissions(char *file_permission, char *required_permission){ for (int i; i < 10; i++){ if ((file_permission[i] != required_permission[i]) && (required_permission[i] != '-')){ return 1; } } return 0; }


Student question: How do you get multiple command line arguments and save them as variables?

Question: How do you get multiple command line arguments and save them as variables?


Student question: For example, given the output of ls -l: total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c how could I store each line in a separate string using scanf?

Question: For example, given the output of ls -l: total 329 -rwx------ 1 reid staff 1734 Jun 22 14:52 prog -rw------- 1 reid staff 21510 Apr 6 12:10 tmp.txt -rwxr-xr-x 1 reid staff 8968 Feb 1 2013 xyz -rw-r--r-- 1 reid staff 88 Feb 15 2013 xyz.c how could I store each line in a separate string using scanf?


Goal: print permission and count | Code context (simplify for kids): while (scanf("%10s %*d %*s %*s %d %*s %*d %*s %*s", permission, &file_size)!= EOF){ // printf("%d", file_size); int count = 0; if (file_size > size){ if (argc == 2){ count = count + 1; } // TODO: Call check_permissions as part of your solution to count the files to // compute and print the correct v


Goal: warning: ‘count’ may be used uninitialized in this function | Code context (simplify for kids): int main(int argc, char** argv) { 17 if (!(argc == 2 || argc == 3)) { 18 fprintf(stderr, "USAGE: count_large size [permissions]\n"); 19 return 1; 20 } 21 22 int size; 23 char required[11]; 24 int *count; 25 26 // TODO: Process command line arguments. 27 if (argc == 2){ 28 size = *argv[1]; 29 } else


Goal: It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits. | Code context (simplify for kids): int populate_array(int sin, int *sin_array) { // check if sin is 9 digits int count = 0; int num = sin; while (num != 0){ count++; num /= 10; } if (count != 9){ return 1; } // populate sin_array for (int i = 8; i > 0; i--){ sin_array[i] = sin % 10; sin = sin / 10; } return 0; }


Goal: It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer. Hint: use % 10 and / 10 to calculate the digits. The function must return 0 when it completes successfully, and 1 if the given integer is not 9 digits. | Code context (simplify for kids): int populate_array(int sin, int *sin_array) { // check if sin is 9 digits int count = 0; int num = sin; while (num != 0){ count++; num /= 10; } if (count != 9){ return 1; } // populate sin_array for (int i = 8; i >= 0; i--){ sin_array[i] = sin % 10; sin = sin / 10; } return 0; }


Goal: If the user calls the program with too few or too many arguments, the program should not print anything, but should return from main with return code 2. main should return 0 when the program runs successfully and the SIN is valid and 1 when the program runs successfully but the SIN is invalid. | Code context (simplify for kids): int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2){ return 2; } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int *populated_array; populate_array((int) argv[1], popu


Goal: Set required as the string in argv[2] | Code context (simplify for kids): int main(int argc, char** argv) { if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } int size; char required[11]; int count = 0; printf("argc: %d, argv: %s\n", argc, argv[2]); // TODO: Process command line arguments. if (argc == 2){ size = *argv[


Goal: Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); } if (argv[1] == argc[2]){ printf("Same\n"); } else{ printf("Different\n"); } return 0; }


Goal: /* Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your m | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); return 0; } if (*argv[1] == *argv[2]){ printf("Same\n"); return 0; } else{ printf("Different\n"); return 0; } }


Goal: Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); return 0; } if strcmp(*argv[1], *argv[2]){ printf("Same\n"); return 0; } else{ printf("Different\n"); return 0; } }


Goal: Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); return 0; } if !strcmp(*argv[1], *argv[2]){ printf("Same\n"); return 0; } else{ printf("Different\n"); return 0; } }


Goal: Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); return 0; } if !strcmp(argv[1], argv[2]){ printf("Same\n"); return 0; } else{ printf("Different\n"); return 0; } }


Goal: Write the main() function of a program that takes exactly two arguments, and prints one of the following: - "Same\n" if the arguments are the same. - "Different\n" if the arguments are different. - "Invalid\n" if the program is called with an incorrect number of arguments. NOTE: Cut and paste these strings into your printf statements to ensure that they will be exactly the strings above. Your main | Code context (simplify for kids): int main(int argc, char ** argv){ if (argc != 3){ printf("Invalid\n"); return 0; } if strcmp(argv[1], argv[2]){ printf("Same\n"); return 0; } else{ printf("Different\n"); return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ strncpy(s, s, n); int removed = strlen(s) - n; return removed; } else { return 0; }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ strncpy(s, s, n); int removed = strlen(s) - n; return removed; } else { return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ char copy[n+1]; strcpy(copy, s); strncpy(s, copy, n); int removed = strlen(s) - n; return removed; } else { return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ char copy[n+1]; strcpy(copy, s); strncpy(s, copy, n-1); s[n] = \0; int removed = strlen(s) - n; return removed; } else { return 0; } }


Goal: Complete this program by writing the function strip_q_marks that takes a single string and returns an integer. The function should modify the string to remove any trailing question marks and return the number of question marks that were removed. Note that you should put the command-line argument in double quotes when you type it in. This prevents the shell from interpreting characters such as "?"  | Code context (simplify for kids): int strip_q_marks(char *string_ptr){ int n = strlen(*string_ptr); int count = 0; for (int i = n; i > 0; i--){ if (string_ptr[i] == "?"){ string_prt[i] = '\0'; count = count + 1; } else { return count; } } }


Goal: Complete this program by writing the function strip_q_marks that takes a single string and returns an integer. The function should modify the string to remove any trailing question marks and return the number of question marks that were removed. Note that you should put the command-line argument in double quotes when you type it in. This prevents the shell from interpreting characters such as "?"  | Code context (simplify for kids): int strip_q_marks(char *string_ptr){ int n = strlen(string_ptr); int count = 0; for (int i = n; i > 0; i--){ if (string_ptr[i] == '?'){ string_ptr[i] = '\0'; count = count + 1; } else { break; } } return count; }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ int removed = strlen(s) - n; char copy[n+1]; strcpy(copy, s); strncpy(s, copy, n); return removed; } else { return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ int removed = strlen(s) - n; char copy[n+1]; strcpy(copy, s); strncpy(s, copy, n); s[n] = '\0'; return removed; } else { return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ int removed = strlen(s) - n; char copy[n+1]; strcpy(copy, s); strncpy(s, copy, n-1); s[n] = '\0'; return removed; } else { return 0; } }


Goal: Write a function named truncate() that takes a string s and a non-negative integer n. If s has more than n characters (not including the null terminator), the function should truncate s at n characters and return the number of characters that were removed. If s has n or fewer characters, s is unchanged and the function returns 0. For example, if s is the string "function" and n is 3, then truncate | Code context (simplify for kids): int truncate(char s[], int n){ if(strlen(s) > n){ int removed = strlen(s) - n; s[n] = '\0'; return removed; } else { return 0; } }


Goal: This program has two arguments: the first is a greeting message, and the second is a name. The message is an impersonal greeting, such as "Hi" or "Good morning". name is set to refer to a string holding a friend's name, such as "Emmanuel" or "Xiao". First copy the first argument to the array greeting. (Make sure it is properly null-terminated.) Write code to personalize the greeting string by appe | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: greeting message name\n"); return 1; } char greeting[20]; char *name = argv[2]; // Your code goes here strncpy(greeting, argv[1], 20); int len_greet = strlen(argv[1]); greeting[len_greet + 1] = '\0'; strcat(greeting, " "); st


Goal: Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. | Code context (simplify for kids): void list_users(const User *curr) { User *temp; temp = (User *) curr; while (temp != NULL){ printf("%s\n", temp->name); temp = temp->next; } }


Student question: Why is this function is only printing the last two nodes instead of all nodes? | Code context (simplify for kids): void list_users(const User *curr) { printf("User List\n"); User *temp; User *head = (User *) curr; temp = (User *) curr; while (temp != NULL){ printf("\t%s\n", temp->name); temp = temp->next; } curr = head; }

Question: Why is this function is only printing the last two nodes instead of all nodes?


Student question: How to print the contents of a file?

Question: How to print the contents of a file?


Goal: tranverse through user's friends and print the name of each one | Code context (simplify for kids): int print_user(const User *user) { while (user->friends != NULL){ printf("\t%s\n", (user->friends)->name); user->friends = user->friends->next; } }


Goal: Print a user profile. * For an example of the required output format, see the example output * linked from the handout. * Return: * - 0 on success. * - 1 if the user is NULL. | Code context (simplify for kids): int print_user(const User *user) { if (user == NULL){ return 1; } printf("Name: %s", user->name); printf("------------------------------------------"); printf("Friends:\n"); User *friends = (User *) user->friends; while (friends != NULL){ printf("\t%s\n", friends->name); friends = friends->next; } p


Student question: how to print characters of a file one by one?

Question: how to print characters of a file one by one?


Student question: why is it giving me an error warning: control reaches end of non-void function [-Wreturn-type] 38 | } | Code context (simplify for kids): void read_bitmap_metadata(FILE *image, int *pixel_array_offset, int *width, int *height) { fseek(image, 10, SEEK_SET); fread(pixel_array_offset, 4, 1, image); fseek(image, 18, SEEK_SET); fread(width, 4, 1, image); fseek(image, 22, SEEK_SET); fread(height, 4, 1, image); return; }

Question: why is it giving me an error warning: control reaches end of non-void function [-Wreturn-type] 38 | }


Student question: suppose we have the following line at the top of our program #define MAXNAME = 32; and then the declaration char name[MAXNAME]; in the program. What will this declaration line become after the program has passed through the C pre-processor?

Question: suppose we have the following line at the top of our program #define MAXNAME = 32; and then the declaration char name[MAXNAME]; in the program. What will this declaration line become after the program has passed through the C pre-processor?


Student question: At our company, an employee has a regular pay rate but then gets 5 dollars extra per hour whenever they are working as a supervisor. Consider the following definition of a MACRO to represent this: #define SUPERVISOR(regular) regular + 5 What will be the output of the following program fragment? #define SUPERVISOR(regular) regular + 5 int main() { int regular_pay = 20; int hours_worked = 10; printf

Question: At our company, an employee has a regular pay rate but then gets 5 dollars extra per hour whenever they are working as a supervisor. Consider the following definition of a MACRO to represent this: #define SUPERVISOR(regular) regular + 5 What will be the output of the following program fragment? #define SUPERVISOR(regular) regular + 5 int main() { int regular_pay = 20; int hours_worked = 10; printf


Goal: Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. | Code context (simplify for kids): void list_users(const User *curr) { printf("User List\n"); User *temp; temp = (User *) curr; while (temp != NULL){ printf("\t%s\n", temp->name); temp = temp->next; } }


Student question: Why is this initialization for friends1[] and friends2[] invalid? | Code context (simplify for kids): char name[MAX_NAME]; char profile_pic[MAX_NAME]; // This is a *filename*, not the file contents. struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; User *user1 = find_user(name1, head); User friends1[] = (*user1).friends; User *user2 = find_user(name2, head); User

Question: Why is this initialization for friends1[] and friends2[] invalid?


Student question: why am I getting this error: error: ‘friends1’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 126 | friends1[i] = (*user1).friends[i]; | Code context (simplify for kids): User *user1 = find_user(name1, head); User **friends1; int i = 0; while ((*user1).friends[i] != NULL){ friends1[i] = (*user1).friends[i]; } User *user2 = find_user(name2, head); User **friends2; int j = 0; while ((*user2).friends[j] != NULL){ friends2[j] = (*user2).friends[j]; }

Question: why am I getting this error: error: ‘friends1’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 126 | friends1[i] = (*user1).friends[i];


Student question: how to check if an element in the array is equal to NULL?

Question: how to check if an element in the array is equal to NULL?


Student question: why is friends1[x] != NULL giving me an error | Code context (simplify for kids): User *user1 = find_user(name1, head); User friends1[MAX_FRIENDS]; int i = 0; while ((*user1).friends[i] != NULL){ friends1[i] = *(*user1).friends[i]; } User *user2 = find_user(name2, head); User friends2[MAX_FRIENDS]; int j = 0; while ((*user2).friends[j] != NULL){ friends2[j] = *(*user2).friends[j]

Question: why is friends1[x] != NULL giving me an error


Goal: Error in assigning first_post | Code context (simplify for kids): int print_user(const User *user) { if (user == NULL){ return 1; } FILE *profile_pic; if ((profile_pic = fopen(user->profile_pic, "r")) != NULL){ int c; while ((c = getc(profile_pic)) != EOF){ printf("%c", c); } fclose(profile_pic); } printf("\n"); printf("Name: %s\n", user->name); printf("----------


Goal: * From the list pointed to by *user_ptr_del, delete the user 242 * with the given name. 243 * Remove the deleted user from any lists of friends. 244 * 245 * Return: 246 * - 0 on success. 247 * - 1 if a user with this name does not exist. | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { 250 User *temp = *user_ptr_del; 251 if (strcmp((*user_ptr_del)->name, name) == 0){ 252 for (int i = 0; i < MAX_FRIENDS; i++){ 253 for (int j = 0; j < MAX_FRIENDS; j++){ 254 if (strcmp(((*user_ptr_del)->next->friends[i]->f riends[j]->name), nam


Goal: From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { User *temp = *user_ptr_del; if (strcmp((*user_ptr_del)->name, name) == 0){ for (int i = 0; i < MAX_FRIENDS; i++){ while ((*user_ptr_del)->friends[i] != NULL){ for (int j = 0; j < MAX_FRIENDS; j++){ while ((*user_ptr_del)->friends[i]->friends[j


Goal: From the list pointed to by *user_ptr_del, delete the user * with the given name. * Remove the deleted user from any lists of friends. * * Return: * - 0 on success. * - 1 if a user with this name does not exist. There is an error between lines 3 and 13 | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { if (strcmp((*user_ptr_del)->name, name) == 0){ for (int i = 0; i < MAX_FRIENDS; i++){ while ((*user_ptr_del)->friends[i] != NULL){ for (int j = 0; j < MAX_FRIENDS; j++){ while ((*user_ptr_del)->friends[i]->friends[j] != NULL){ if (strcmp(((*us


Goal: delete the first user From the list pointed to by *user_ptr_del | Code context (simplify for kids): int delete_user(const char *name, User **user_ptr_del) { if (strcmp((*user_ptr_del)->name, name) == 0){ int i = 0; while (((*user_ptr_del)->friends[i] != NULL) && (i < MAX_FRIENDS)){ int j = 0; while (((*user_ptr_del)->friends[i]->friends[j] != NULL) && (j < MAX_FRIENDS)){ if (strcmp(((*user_ptr_del


Goal: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ dest[i] = src[i]; } dest[capacity] = '\0'; return dest; }


Goal: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ dest[i] = src[i]; } dest[capacity] = "\0"; return dest; }


Code context (simplify for kids): #include <stdio.h> #define SIZE 4 #define OVERFLOW 4 int main() { int index = 0; int i; int before[SIZE] = {10, 10, 10, 10}; int a[SIZE] = {0, 0, 0, 0}; int after[SIZE] = {10, 10, 10, 10}; printf("Address of the variables:\n"); for (index = 0; index < SIZE; index++) { printf("%lx -> &after[%d]\n", (


Student question: How to add a null terminator to the last value of a string without using strcpy or strncpy?

Question: How to add a null terminator to the last value of a string without using strcpy or strncpy?


Student question: How to add a null terminator to the last value of a string without using any string functions??

Question: How to add a null terminator to the last value of a string without using any string functions??


Student question: Why is this string not properly terminated with a null character? | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ dest[i] = src[i]; } dest[capacity] = '\0'; return dest; }

Question: Why is this string not properly terminated with a null character?


Goal: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ dest[i] = src[i]; } dest[capacity - 1] = '\0'; return dest; }


Goal: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ if (src[i] == '\0'){ break; } else { dest[i] = src[i]; } return dest; }


Goal: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string | Code context (simplify for kids): char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity - 1; i++){ if (src[i] == '\0'){ break; } else { dest[i] = src[i]; } dest[capacity - 1] = '\0'; return dest; }


Student question: What is the missing code? | Code context (simplify for kids): char *course = "csc207"; // missing code x = course; x[5] = ’9’;

Question: What is the missing code?


Code context (simplify for kids): char *intertwine(char *s1, char *s2) { retval[size_ret] = ’\0’; return retval; char *result = malloc(strlen(s1) + strlen(s2) + 1); int min = strlen(s1); if (strlen(s2) < min) { min = strlen(s2); } for (int i = 0; i < min; i++) { result[i*2] = s1[i]; result[i*2 + 1] = s2[i]; } result[min*2] = ’\0’; s


Student question: Why do we need the address of s1 in strcat(result, &s1[min]); ? | Code context (simplify for kids): char *intertwine(char *s1, char *s2) { retval[size_ret] = ’\0’; return retval; char *result = malloc(strlen(s1) + strlen(s2) + 1); int min = strlen(s1); if (strlen(s2) < min) { min = strlen(s2); } for (int i = 0; i < min; i++) { result[i*2] = s1[i]; result[i*2 + 1] = s2[i]; } result[min*2] = ’\0’; s

Question: Why do we need the address of s1 in strcat(result, &s1[min]); ?


Code context (simplify for kids): char **w = malloc(2 * sizeof(char *)); w[0] = "hello"; *(w + 1) = w[0] + 1; printf("%s, %s", w[0], w[1]);


Student question: What is the difference between -> and . in c?

Question: What is the difference between -> and . in c?


Student question: What is a dangling pointer?

Question: What is a dangling pointer?


Student question: When should I allocate dynamic memory?

Question: When should I allocate dynamic memory?


Student question: Why does this code give a run-time error for returning stack memory? | Code context (simplify for kids): int *mkpoint(int x, int y) { int pt[2] = {x, y}; return pt; } x = mkpoint(3, 4);

Question: Why does this code give a run-time error for returning stack memory?


Student question: when does a segmentation fault happen?

Question: when does a segmentation fault happen?


Student question: If I removed result[size] = '\0';, what error would occur and why? | Code context (simplify for kids): char *every_nth(char *s, int n) { 3 4 int size = strlen(s) / n; 5 6 char *result = malloc(sizeof(char) * size + 1); 7 8 int i = 0; 9 for(i = 0; i < size; i++) { 10 result[i] = s[i * n]; 11 } 12 13 result[size] = ’\0’; 14 15 return result; 16 17 }

Question: If I removed result[size] = '\0';, what error would occur and why?


Student question: when would extra output happen?

Question: when would extra output happen?


Student question: Are char * and strings the same in all cases?

Question: Are char * and strings the same in all cases?


Student question: What is the difference between gcc -c and GCC -o?

Question: What is the difference between gcc -c and GCC -o?


Student question: What is **w? | Code context (simplify for kids): char **w = malloc(2 * sizeof(char *)); w[0] = "hello"; *(w + 1) = w[0] + 1; printf("%s, %s", w[0], w[1]);

Question: What is **w?


Student question: Why is "hello" spread across w[0] to w[4], instead of being pointed to by w[0]? | Code context (simplify for kids): char **w = malloc(2 * sizeof(char *)); w[0] = "hello"; *(w + 1) = w[0] + 1; printf("%s, %s", w[0], w[1]);

Question: Why is "hello" spread across w[0] to w[4], instead of being pointed to by w[0]?


Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main() { int ret; printf("A\n"); ret = fork(); printf("B\n"); if (ret < 0) { perror("fork"); exit(1); } else if (ret == 0) { printf("C\n"); } else { printf("D\n"); } printf("E\n"); return 0; }


Student question: How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments? | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(

Question: How many processes are created, including the original parent, when forkloop is called with 2, 3, and 4 as arguments? n arguments?


Student question: If we run forkloop 1, two processes are created, the original parent process and its child. Assuming the process id of the parent is 414 and the process id of the child is 416, we can represent the relationship between these processes using the following ASCII diagram: 414 -> 416 Use a similar ASCII diagram to show the processes created and their relationships when you run forkloop 3. In other wor | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");

Question: If we run forkloop 1, two processes are created, the original parent process and its child. Assuming the process id of the parent is 414 and the process id of the child is 416, we can represent the relationship between these processes using the following ASCII diagram: 414 -> 416 Use a similar ASCII diagram to show the processes created and their relationships when you run forkloop 3. In other wor


Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Goal: Only the original parent calls fork and the new children do not create additional processes. | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Goal: each process creates exactly one a new process. Keep the printf call for all processes. | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Goal: Each fork process creates exactly one new fork process. | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Goal: The original parent calls fork and new children do not create additional processes | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Goal: Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(


Goal: Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } break; printf("ppid = %d, pid = %d, i = %d\n", g


Goal: Modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. The resulting diagram will look something like the following when childcreates 3 is called: 414 -> 416 -> 417 -> 420 | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork");


Student question: What is the syntax for wait in a parent call that waits for the child fork call to complete?

Question: What is the syntax for wait in a parent call that waits for the child fork call to complete?


Student question: Why are all pid the same? | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); printf("ppid = %d, pid = %d\n", getppid(), getpid()); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(

Question: Why are all pid the same?


Goal: have the parent wait for all children forks to finish using the wait call. | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); printf("ppid = %d, pid = %d\n", getppid(), getpid()); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(


Student question: Why does all children fork have the same pid? | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); printf("ppid = %d, pid = %d\n", getppid(), getpid()); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(

Question: Why does all children fork have the same pid?


Goal: the command line argument is the number of forks done by this program. Have it such that all the forks have different process ids but are forked from the same parent (have the same ppid). | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); printf("ppid = %d, pid = %d\n", getppid(), getpid()); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(


Goal: In the new file, modify the program so that each process creates exactly one a new process. Keep the printf call for all processes. Add the appropriate wait calls to ensure that each parent does not terminate before its children. | Code context (simplify for kids): #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); printf("ppid = %d, pid = %d\n", getppid(), getpid()); for (int


Student question: What is the default standard input?

Question: What is the default standard input?


Student question: how do I run a c file validate.c and have it return the return code to stdout?

Question: how do I run a c file validate.c and have it return the return code to stdout?


Student question: when running a compiled file using the ./ command, how can I have the exit code in stdout?

Question: when running a compiled file using the ./ command, how can I have the exit code in stdout?


Student question: How to fork into another file in c?

Question: How to fork into another file in c?


Student question: This program should take in two arguments from stdin, then write them to a pipe. Then, have fork the program and have the child process call the program ./validate with the two arguments from the pipe. Depending on the exit code of ./validate, the program should output SUCCESS, INVALID or NO_USER. | Code context (simplify for kids): int fd[2]; if(pipe(fd) = -1){ perror("pipe"); } int status; int n = fork(); if(n < 0){ perror("fork"); exit(1); } else if (n > 0){ close(fd[0]); if (write(fd[1], user_id, MAXLINE) == -1){ perror("write to pipe"); } if (write(fd[1], password, MAXLINE) == -1){ perror("write to pipe"); } close(fd[1]);

Question: This program should take in two arguments from stdin, then write them to a pipe. Then, have fork the program and have the child process call the program ./validate with the two arguments from the pipe. Depending on the exit code of ./validate, the program should output SUCCESS, INVALID or NO_USER.


Student question: How to stop fork from rerunning fgets

Question: How to stop fork from rerunning fgets


Student question: If there are two things in the pipe do you use dup2 once or twice?

Question: If there are two things in the pipe do you use dup2 once or twice?


Student question: Initialize memory for fd[1], however, the code is giving me an error as expected ‘void *’ but argument is of type ‘int’ | Code context (simplify for kids): int fd[2]; if(pipe(fd) == -1){ perror("pipe"); } memset(fd[1], MAX_PASSWORD * 2, sizeof(char));

Question: Initialize memory for fd[1], however, the code is giving me an error as expected ‘void *’ but argument is of type ‘int’


Task: How to deference fd and get the first element of it?


Student question: *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1)); what does this mean?

Question: *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1)); what does this mean?


Task: int fd[2]; if(pipe(fd) == -1){ perror("pipe"); } Given the above code, use memset to allocate space of 2 *sizeof(char) for the first element in fd.


Task: int fd[2]; if(pipe(fd) == -1){ perror("pipe"); } Given the above code, use memset to allocate space of 2*MAX_PASSWORD *sizeof(char) for the second element in fd.


Student question: What does this error mean? /usr/include/string.h:61:14: note: declared here

Question: What does this error mean? /usr/include/string.h:61:14: note: declared here


Task: How to parse through a line like "main : linked_list.o main.o" to get all the dependencies and target?


Task: How can I name a variable with the counter I? For example, if I want string1, string2 and string3 when i=1,2,3 respectively?


Student question: error: variable-sized object may not be initialized 30 | Dependency *dep[i] = malloc(sizeof(Dependency)); Why is the compiler giving me this error?

Question: error: variable-sized object may not be initialized 30 | Dependency *dep[i] = malloc(sizeof(Dependency)); Why is the compiler giving me this error?


Task: How to parse through a line like "main : linked_list.o main.o" to get all the dependencies and target (where the number of dependencies is unknown), while mallocing space for each dependency with a data type dependency, where dependency->name be the name of the dependency?


Student question: I want a Dependency and a Rule for every i value | Code context (simplify for kids): int i = 1; while (token != NULL){ Dependency *dep[i] = malloc(sizeof(Dependency)); if (dep[i] == NULL){ perror("malloc"); exit(1); } Rule *rule[i] = malloc(sizeof(Rule)); if (rule[i] == NULL){ perror("malloc"); exit(1); } dep[i]->rule_node = rule[i]; i++; }

Question: I want a Dependency and a Rule for every i value


Student question: If I have multiple tokens, how can I fix this code such that I can easily access the previous dep and rule spaces? | Code context (simplify for kids): token = strtok(line, " "); while (token != NULL){ Dependency *dep = malloc(sizeof(Dependency)); if (dep == NULL){ perror("malloc"); exit(1); } Rule *rule = malloc(sizeof(Rule)); if (rule == NULL){ perror("malloc"); exit(1); } rule->target = token; dep->rule = rule; }

Question: If I have multiple tokens, how can I fix this code such that I can easily access the previous dep and rule spaces?


Task: How to add code to go to the next token?


Student question: How to check if the first character in a string is a tab?

Question: How to check if the first character in a string is a tab?


Student question: What are arguements in make files?

Question: What are arguements in make files?


Student question: WHat would the output of this code be if args = "gcc -Wall -g -std=gnu99 -o main linked_list.o main.o" | Code context (simplify for kids): char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args[i], size - strlen(buffer)); strncat(buffer, " ", size - strlen(buffer)); i++; } return buffer; }

Question: WHat would the output of this code be if args = "gcc -Wall -g -std=gnu99 -o main linked_list.o main.o"


Student question: WHat would the output of this code be if args = "main : linked_list.o main.o" | Code context (simplify for kids): char *args_to_string(char **args, char *buffer, int size) { buffer[0] = '\0'; int i = 0; while (args[i] != NULL) { strncat(buffer, args[i], size - strlen(buffer)); strncat(buffer, " ", size - strlen(buffer)); i++; } return buffer; }

Question: WHat would the output of this code be if args = "main : linked_list.o main.o"


Student question: How to fix this code error: lvalue required as unary ‘&’ operand 60 | char **argument = &strtok(line, " ");

Question: How to fix this code error: lvalue required as unary ‘&’ operand 60 | char **argument = &strtok(line, " ");


Student question: How to fix the error error: lvalue required as unary ‘&’ operand 60 | char **argument = &(strtok(line, " ")); | Code context (simplify for kids): fgets(line, MAXLINE, fp); if (strstr(line, "-o") != NULL) { strtok(line, " "); Action *action = malloc(sizeof(Action)); char **argument = &(strtok(line, " ")); action->args = argument; }

Question: How to fix the error error: lvalue required as unary ‘&’ operand 60 | char **argument = &(strtok(line, " "));


Student question: How to fix this error parse.c:24:22: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [-Wint-conversion] 24 | if (strchr(line, "\t") != line){ | ^~~~ | | | char *

Question: How to fix this error parse.c:24:22: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [-Wint-conversion] 24 | if (strchr(line, "\t") != line){ | ^~~~ | | | char *


Student question: This code is giving me a malloc: Cannot allocate memory error | Code context (simplify for kids): Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *head = malloc(sizeof(Rule)); if (head == NULL){ perror("malloc"); exit(1); } char line[MAXLINE]; while(fgets(line, MAXLINE, fp) != NULL){ // checking if the first character is a tab to determ

Question: This code is giving me a malloc: Cannot allocate memory error


Student question: What is the code to check where the memory error happened with valgrind

Question: What is the code to check where the memory error happened with valgrind


Student question: What does memset do and how do I use it?

Question: What does memset do and how do I use it?


Student question: How to fix this warning? warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion] 8 | FILE *fp = fopen(argv[1], "r"); | ~~~~^~~

Question: How to fix this warning? warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion] 8 | FILE *fp = fopen(argv[1], "r"); | ~~~~^~~


Student question: How to set action->args[1] to be arg_name with open and close quotation marks ""?

Question: How to set action->args[1] to be arg_name with open and close quotation marks ""?


Student question: What does the program print to stderr when it runs without interruption? | Code context (simplify for kids): int x = 5; void handler(int sig) { x += 3; fprintf(stderr, "inside %d ", x); } int main() { fprintf(stderr, "start "); struct sigaction act; act.sa_handler = handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); x += 2; fprintf(stderr, "outside %d", x); return 0; }

Question: What does the program print to stderr when it runs without interruption?


Student question: How do I fix this error? Invalid read of size 4 ==13752== at 0x4900657: fgets (iofgets.c:47) ==13752== by 0x1094B2: parse_file (parse.c:17) ==13752== by 0x10984B: main (parse.c:197) ==13752== Address 0x0 is not stack'd, malloc'd or (recently) free'd This is line 17: while(fgets(line, MAXLINE, fp) != NULL){

Question: How do I fix this error? Invalid read of size 4 ==13752== at 0x4900657: fgets (iofgets.c:47) ==13752== by 0x1094B2: parse_file (parse.c:17) ==13752== by 0x10984B: main (parse.c:197) ==13752== Address 0x0 is not stack'd, malloc'd or (recently) free'd This is line 17: while(fgets(line, MAXLINE, fp) != NULL){


Task: Make sure that the last character of a string is not \n, if it is \n, delete \n.


Student question: what might be the reasons of my output missing one line after clearn : | Code context (simplify for kids): main : linked_list.o main.o gcc - Wall - g - std = gnu99 - o main linked_list.o main .o main.o : linked_list.h gcc - Wall - g - std = gnu99 - c main .c linked_list.o : linked_list.c linked_list.h gcc - Wall - g - std = gnu99 - c linked_list.c clean : rm - f linked_list.o rm - f main.o

Question: what might be the reasons of my output missing one line after clearn :


Task: Use stat to get the last modified time (mtime) of a file. Read the man page (using man 2 stat) to learn how to call stat. The field of the stat struct that you want is called st_mtim on Linux. The struct timespec st_mtim; has two fields, one for seconds (tv_sec) and one for nanoseconds (tv_nsec). You will need both of these values to find out which time is more recent.


Student question: go through target_rule's nodes of dependencies and save the last modified time present in any dependency | Code context (simplify for kids): struct stat st; st.st_mtim.tv_sec = 0; struct stat last_mod_time; last_mod_time.st_mtim.tv_sec = 0; while (target_rule->dependencies != NULL){ if(stat(dep->rule->target, &st) == 0){ if(last_mod_time.st_mtim.tv_sec == 0){ last_mod_time = st; } else { if(difftime(st, last_mod_time) > 0){ last_mod_time

Question: go through target_rule's nodes of dependencies and save the last modified time present in any dependency


Student question: How to fix st_mtim: No such file or directory error

Question: How to fix st_mtim: No such file or directory error


Student question: What header files to include for st_mtim?

Question: What header files to include for st_mtim?


Student question: How to fix warning: ‘st’ may be used uninitialize, same with 'last_mod_time' and 'target_mod_time' | Code context (simplify for kids): struct stat *st; 53 st->st_mtim.tv_sec = 0; 54 struct stat *last_mod_time; 55 last_mod_time->st_mtim.tv_sec = 0; 56 while (target_rule->dependencies != NULL) { 57 if (stat(dep->rule->target, st) == 0) { 58 if (last_mod_time->st_mtim.tv_sec == 0) { 59 last_mod_time = st; 60 } else { 61 if (last_mod_t

Question: How to fix warning: ‘st’ may be used uninitialize, same with 'last_mod_time' and 'target_mod_time'


Student question: I am receiving a st_mtim: No such file or directory error | Code context (simplify for kids): #include <sys/stat.h> struct stat target_mod_time; // struct timespec target_time = target_mod_time->st_mtim; if(stat(target, &target_mod_time) == 0){ printf("successfully got target time"); } else if(stat(target, &target_mod_time) == -1){ perror("st_mtim"); exit(1); } struct stat st; while (target_

Question: I am receiving a st_mtim: No such file or directory error


Student question: Trying to check if target exit with stat | Code context (simplify for kids): struct stat target_exist; stat(target, &target_exist); if(stat(target, &target_exist) != 0){ perror("target non-existent"); exit(1); }

Question: Trying to check if target exit with stat


Student question: Change this code such that it prints out random numbers from 0 to 99 instead of in that order, use the function random | Code context (simplify for kids): for (int i = 0; i < 100; i++){ fprintf(fp, "%d\n", i); }

Question: Change this code such that it prints out random numbers from 0 to 99 instead of in that order, use the function random


Student question: It is giving me an error that buffer may not be initialized. | Code context (simplify for kids): int *buffer; 41 for (;;) { 42 fseek(fp, (rand()%99)*sizeof(int), SEEK_SET); 43 fread(buffer, sizeof(int), 1, fp); 44 45 }

Question: It is giving me an error that buffer may not be initialized.


Student question: How to fix the code giving me an error that buffer may not be initialized. | Code context (simplify for kids): 24 int main(int argc, char **argv) { 25 if (argc != 3) { 26 fprintf(stderr, "Usage: time_reads s filename\n"); 27 exit(1); 28 } 29 seconds = strtol(argv[1], NULL, 10); 30 31 FILE *fp; 32 if ((fp = fopen(argv[2], "r")) == NULL) { 33 perror("fopen"); 34 exit(1); 35 } 36 37 /* In an infinite loop, read

Question: How to fix the code giving me an error that buffer may not be initialized.


Task: Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF.


Student question: Fix the code such that the integers printed out are from 0 to 99. Currectly, the integers printed out are 9 digits | Code context (simplify for kids): int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "Usage: time_reads s filename\n"); exit(1); } seconds = strtol(argv[1], NULL, 10); FILE *fp; if ((fp = fopen(argv[2], "r")) == NULL) { perror("fopen"); exit(1); } /* In an infinite loop, read an int from a random location in the file

Question: Fix the code such that the integers printed out are from 0 to 99. Currectly, the integers printed out are 9 digits


Code context (simplify for kids): int main(void) { int sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd < 0) { perror("client: socket"); exit(1); } struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(PORT); if (inet_pton(AF_INET, "127.0.0.1", &server.sin_addr) < 1) { perror("client: inet_pton"); clo


Student question: There are now two different types of input that we could receive: a username, or a message. How could we tell if we have if we have received a username yet? | Code context (simplify for kids): int read_from(int client_index, struct sockname *users) { int fd = users[client_index].sock_fd; char buf[BUF_SIZE + 1]; int num_read = read(fd, &buf, BUF_SIZE); buf[num_read] = '\0'; if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { users[client_index].sock_fd = -1; return fd; } ret

Question: There are now two different types of input that we could receive: a username, or a message. How could we tell if we have if we have received a username yet?


Student question: your task is to modify read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo. | Code context (simplify for kids): int read_from(int client_index, struct sockname *users) { int fd = users[client_index].sock_fd; char buf[BUF_SIZE + 1]; /* In Lab 10, you focused on handling partial reads. For this lab, you do * not need handle partial reads. Because of that, this server program * does not check for "\r\n" when it

Question: your task is to modify read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo.


Code context (simplify for kids): int read_from(int client_index, struct sockname *users) { int fd = users[client_index].sock_fd; char buf[BUF_SIZE + 1]; int num_read = read(fd, &buf, BUF_SIZE); buf[num_read] = '\0'; if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { users[client_index].sock_fd = -1; return fd; } ret


Task: Write read_from to see if we have received a username yet (how can we tell?), and allocate memory for the username field in the sockname struct and copy the name to the field for that client. After the username has been set, all new incoming messages from that connection will be messages to echo.


Task: I am writing code in the server. How do I broadcast to all clients that are connected to this server?


Student question: How to add to this code such that it will print buf to all of the clients connected to this server? | Code context (simplify for kids): int read_from(int client_index, struct sockname *users) { int fd = users[client_index].sock_fd; char buf[BUF_SIZE + 1]; /* In Lab 10, you focused on handling partial reads. For this lab, you do * not need handle partial reads. Because of that, this server program * does not check for "\r\n" when it

Question: How to add to this code such that it will print buf to all of the clients connected to this server?


Student question: How to loop through the users and check if each one is connected? | Code context (simplify for kids): int read_from(int client_index, struct sockname *users) { int fd = users[client_index].sock_fd; char buf[BUF_SIZE + 1]; int num_read = read(fd, &buf, BUF_SIZE); buf[num_read] = '\0'; if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) { users[client_index].sock_fd = -1; return fd; } ret

Question: How to loop through the users and check if each one is connected?


Code context (simplify for kids): while (1) { fd_set listen_fds = all_fds; if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) { perror("server: select"); exit(1); } if (FD_ISSET(sock_fd, &listen_fds)) { int client_fd = accept_connection(sock_fd, users); if (client_fd > max_fd) { max_fd = client_fd; } FD_SET(client_fd, &all


Student question: Why does printf("[Server] %s", buf); prints successfully but printf("[Server]"); not print anything?

Question: Why does printf("[Server] %s", buf); prints successfully but printf("[Server]"); not print anything?