user_id
stringlengths
24
24
time
stringlengths
24
24
feature_type
stringclasses
5 values
feature_version
stringclasses
2 values
input_question
stringlengths
1
1.24k
input_code
stringlengths
2
18.5k
input_intention
stringlengths
1
2.48k
input_task_description
stringlengths
18
3.32k
63c648555f004bf4898cfb8f
2023-01-27T19:35:16.764Z
General Question
v1
when do we use ** instead of * using pointers
null
null
null
63c648555f004bf4898cfb8f
2023-01-27T19:37:58.706Z
General Question
v1
how to set the value of an array malloc
null
null
null
63c648555f004bf4898cfb8f
2023-01-27T19:38:31.247Z
General Question
v1
how to set the value of the third item in an array malloc
null
null
null
63c648555f004bf4898cfb8f
2023-01-27T19:39:24.970Z
General Question
v1
how to return a void function in c
null
null
null
63c648555f004bf4898cfb8f
2023-01-27T19:57:30.340Z
General Question
v1
how to convert str to integer in c
null
null
null
63c648555f004bf4898cfb8f
2023-01-27T20:07:10.482Z
General Question
v1
how to make nested arrays stored in heap in c
null
null
null
63c648585f004bf4898cfba8
2023-01-18T21:35:40.109Z
Question from Code
v1
why is my output delayed by one iteration?
#include <stdio.h> #include <stdlib.h> int main() { char phone[11]; scanf("%s\n", phone); int num; scanf("%d\n", &num); while (scanf(" %d\n", &num) == 1) { if (num == -1) { printf("%s\n", phone); } else if (0 <= num && num <= 9) { printf("%c\n", phone[num]); } else { printf("ERROR\n"); } } return 0; }
null
null
63c648585f004bf4898cfba8
2023-01-18T21:37:44.784Z
Question from Code
v1
why is my output delayed by one iteration
#include <stdio.h> #include <stdlib.h> int main() { char phone[11]; scanf("%s\n", phone); int num; scanf("%d", &num); while (scanf("%d", &num) == 1) { if (num == -1) { printf("%s\n", phone); } else if (0 <= num && num <= 9) { printf("%c\n", phone[num]); } else { printf("ERROR\n"); } } return 0; }
null
null
63c648585f004bf4898cfba8
2023-01-23T21:02:02.445Z
Question from Code
v1
why is result[1] containing the wrong output
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { int *arr1, *arr2; int **arr_pt = malloc(sizeof(int*) * 2); int size = (int)sizeof(int) * (int)(length / 2); if (length % 2 == 0) { arr1 = malloc(size); arr2 = malloc(size); printf("%d\n", size); } else { arr1 = malloc(size + 1); arr2 = malloc(size); } for (int i = 0; i < size; i++) { //printf("%d%d\n", s[i], s[i+1]); arr1[i] = s[2 * i]; arr2[i] = s[2 * i + 1]; } for (int i = 0; i < size; i++) { printf("%d%d\n", arr1[i], arr2[i]); } arr_pt[0] = arr1; arr_pt[1] = arr2; return arr_pt; } /* 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 */ int *build_array(char **strs, int size) { int *arr = malloc(sizeof(int) * size - 1); for (int i = 1; i < size; i++) { int j = strtol(strs[i], NULL, 10); arr[i-1] = j; } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc - 1); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
null
null
63c648585f004bf4898cfba8
2023-01-23T21:04:01.733Z
Question from Code
v1
Why am I getting an abort error message
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { int *arr1, *arr2; int **arr_pt = malloc(sizeof(int*) * 2); int size = (int)sizeof(int) * (int)(length / 2); if (length % 2 == 0) { arr1 = malloc(size); arr2 = malloc(size); printf("%d\n", size); } else { arr1 = malloc(size + 1); arr2 = malloc(size); } for (int i = 0; i < size; i++) { //printf("%d%d\n", s[i], s[i+1]); arr1[i] = s[2 * i]; if (2 * i + 1) { arr2[i] = s[2 * i + 1]; } } for (int i = 0; i < size; i++) { printf("%d%d\n", arr1[i], arr2[i]); } arr_pt[0] = arr1; arr_pt[1] = arr2; return arr_pt; } /* 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 */ int *build_array(char **strs, int size) { int *arr = malloc(sizeof(int) * size - 1); for (int i = 1; i < size; i++) { int j = strtol(strs[i], NULL, 10); arr[i-1] = j; } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc - 1); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
null
null
63c648585f004bf4898cfba8
2023-01-23T21:05:18.984Z
Question from Code
v1
How can I change the helper functions to stop getting the abort error message?
#include <stdio.h> #include <stdlib.h> /* Return a pointer to an array of two dynamically allocated arrays of ints. The first array contains the elements of the input array s that are at even indices. The second array contains the elements of the input array s that are at odd indices. Do not allocate any more memory than necessary. You are not permitted to include math.h. You can do the math with modulo arithmetic and integer division. */ int **split_array(const int *s, int length) { int *arr1, *arr2; int **arr_pt = malloc(sizeof(int*) * 2); int size = (int)sizeof(int) * (int)(length / 2); if (length % 2 == 0) { arr1 = malloc(size); arr2 = malloc(size); printf("%d\n", size); } else { arr1 = malloc(size + 1); arr2 = malloc(size); } for (int i = 0; i < size; i++) { //printf("%d%d\n", s[i], s[i+1]); arr1[i] = s[2 * i]; if (2 * i + 1) { arr2[i] = s[2 * i + 1]; } } for (int i = 0; i < size; i++) { printf("%d%d\n", arr1[i], arr2[i]); } arr_pt[0] = arr1; arr_pt[1] = arr2; return arr_pt; } /* 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 */ int *build_array(char **strs, int size) { int *arr = malloc(sizeof(int) * size - 1); for (int i = 1; i < size; i++) { int j = strtol(strs[i], NULL, 10); arr[i-1] = j; } return arr; } int main(int argc, char **argv) { /* Replace the comments in the next two lines with the appropriate arguments. Do not add any additional lines of code to the main function or make other changes. */ int *full_array = build_array(argv, argc); int **result = split_array(full_array, argc - 1); printf("Original array:\n"); for (int i = 0; i < argc - 1; i++) { printf("%d ", full_array[i]); } printf("\n"); printf("result[0]:\n"); for (int i = 0; i < argc / 2; i++) { printf("%d ", result[0][i]); } printf("\n"); printf("result[1]:\n"); for (int i = 0; i < (argc - 1) / 2; i++) { printf("%d ", result[1][i]); } printf("\n"); free(full_array); free(result[0]); free(result[1]); free(result); return 0; }
null
null
63c648585f004bf4898cfba8
2023-02-01T20:12:42.424Z
General Question
v1
How to write code to remove trailing periods
null
null
null
63c648585f004bf4898cfba8
2023-02-01T20:34:40.250Z
General Question
v1
does strlen in c count the number of question marks
null
null
null
63c648585f004bf4898cfba8
2023-02-01T20:35:14.756Z
General Question
v1
why is strlen() not counting the number of question marks
null
null
null
63c648585f004bf4898cfba8
2023-02-08T20:52:33.138Z
Question from Code
v1
Why am I getting a segmentation fault from this section of the code?
#include <stdio.h> #include <stdlib.h> #include "bitmap.h" int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: bitmap_printer input_bmp\n"); exit(1); } FILE *image = fopen(argv[1], "rb"); if (image == NULL) { fprintf(stderr, "Cannot open file\n"); exit(1); } // Read in bitmap file metadata int pixel_array_offset, width, height; read_bitmap_metadata(image, &pixel_array_offset, &width, &height); // Print out metadata. printf("Pixel array offset: %d\n", pixel_array_offset); printf("Width: %d\n", width); printf("Height: %d\n", height); // Read in the pixel data struct pixel **pixels = read_pixel_array(image, pixel_array_offset, width, height); // Print out some pixels from each of the image's corners. for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { print_pixel(pixels[i][j]); print_pixel(pixels[i][width - 1 - j]); print_pixel(pixels[height - 1 - i][j]); print_pixel(pixels[height - 1 - i][width - 1 - j]); } } // Clean up: you need to do this! return 0; } struct pixel **read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) { struct pixel *arr[height]; for (int i = 0; i < height; i++) { arr[i] = malloc(sizeof(struct pixel) * width); fseek(image, pixel_array_offset + sizeof(struct pixel) * i, SEEK_SET); fread(arr[i], sizeof(struct pixel), width, image); } return arr; }
null
null
63c648585f004bf4898cfba8
2023-02-14T05:27:52.510Z
Question from Code
v1
Why does deleting a user then adding a user not update properly on friend's list
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Check if two users are friends * * Return: * - 0 if the users are friends * - 1 if the users are not friends */ int check_friends(const User *user1, User *user2) { for (int i = 0; i < MAX_FRIENDS; i++) { if(user1 -> friends[i] != NULL) { if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { return 0; } } } return 1; } /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Check length of name if (strlen(name) > MAX_NAME - 1) { return 2; } // Create Space for the user & Initialize Variables User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); strcpy(new_user -> profile_pic, ""); new_user -> first_post = NULL; for (int i = 0; i < MAX_FRIENDS; i++) { new_user -> friends[i] = NULL; } new_user -> next = NULL; // First User to be created if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // User(s) already exist User *curr = *user_ptr_add; // printf("Linked List Excluding Last: "); while (curr -> next != NULL) { //printf("%s ", curr -> name); if (strcmp(curr -> name, name) == 0) { return 1; } curr = curr -> next; } // Check for the final node if (strcmp(curr -> name, name) == 0) { return 1; } curr -> next = new_user; //printf("\n%s %s\n", curr -> name, new_user -> name); return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // Check to make sure at least one User exists if ((User *) head == NULL) { return NULL; } // Find a User with the same name, and return if it exists User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr -> name, name) == 0) { return curr; } curr = curr -> next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // Check to make sure at least one User exists if ((User *) curr == NULL) { } else { User *current = (User *) curr; // Loop through every User while (current != NULL) { printf("User: %s\n", current -> name); printf("Profile Pic: %s\n", current -> profile_pic); printf("Friends: "); for (int i = 0; i < MAX_FRIENDS; i++) { if (current -> friends[i] != NULL) { printf("%s ", current -> friends[i] -> name); } } printf("\n\n"); current = current -> next; } } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // Check filename length if (strlen(filename) > MAX_NAME - 1) { return 2; } // Check file existance and workings FILE *pic = fopen(filename, "r"); if (pic == NULL) { return 1; } fclose(pic); // Change user profile picture strncpy(user -> profile_pic, filename, MAX_NAME - 1); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *person1 = find_user(name1, head); User *person2 = find_user(name2, head); // Check to make sure both users exist if (person1 == NULL || person2 == NULL) { return 4; } // Check for the same user passed in twice if (strcmp(name1, name2) == 0) { return 3; } int open1 = MAX_FRIENDS; int open2 = MAX_FRIENDS; for (int i = MAX_FRIENDS - 1; i >= 0; i--) { // Check to see if users are already friends if(person1 -> friends[i] != NULL) { if (strcmp(person1 -> friends[i] -> name, name2) == 0) { return 1; } } // Find first open index of each user's friend list if (person1 -> friends[i] == NULL) { open1 = i; } if (person2 -> friends[i] == NULL) { open2 = i; } // Check to ensure there is room in both user's friend lists if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { return 2; } } // Add to list person1 -> friends[open1] = person2; person2 -> friends[open2] = person1; return 0; } /* * 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. */ int print_user(const User *user) { if (user == NULL) { return 1; } // Print Profile Picture FILE *pic = fopen(user -> profile_pic, "r"); if (pic == NULL) { } else { char line_info[100]; while(fgets(line_info, 100, pic) != NULL) { printf("%s\n", line_info); } fclose(pic); } // Print Name printf("Name: %s\n", user -> name); printf("------------------------------------------\n"); // Print Friends List printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { if (user -> friends[i] != NULL) { printf("%s\n", user -> friends[i] -> name); } } printf("------------------------------------------\n"); // Print Posts printf("Posts:\n"); struct post *curr; if (user -> first_post != NULL) { curr = user -> first_post; } while (curr != NULL) { printf("From: %s\n", curr -> author); printf("Date: %s\n", ctime(curr -> date)); printf("%s\n", curr -> contents); curr = curr -> next; if (curr != NULL) { printf("\n===\n\n"); } } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { // Check both exists if (author == NULL || target == NULL) { return 2; } // Check users are friends int friend = check_friends(author, target); if (friend == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strcpy(new_post -> author, author -> name); time_t *curtime = malloc(sizeof(time_t)); time(curtime); new_post -> date = curtime; new_post -> contents = contents; // Link Post to Post Linked List if (target -> first_post == NULL) { target -> first_post = new_post; } else { new_post -> next = target ->first_post; target -> first_post = new_post; } return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { // Check to ensure user with name exists if (*user_ptr_del == NULL) { return 1; } // Check to see if first user is to be deleted User *user = find_user(name, *user_ptr_del); if (user == NULL) { return 1; } User *prev = NULL; User *curr = *user_ptr_del; if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted } else { // Check the rest of the users while (prev == NULL) { if (strcmp((curr -> next) -> name, name) == 0) { prev = curr; } curr = curr -> next; } } // Delete user from friend's list for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = NULL; if (user -> friends[i] != NULL) { friend = user -> friends[i]; for (int j = 0; j < MAX_FRIENDS; j++) { if (friend -> friends[i] == user) { friend -> friends[i] = NULL; } } } } // Free all dynamically allocated memory Post *curr_post = user -> first_post; while (curr_post != NULL) { Post *temp = curr_post; curr_post = curr_post -> next; free(temp -> contents); free(temp -> date); free(temp); } if (prev == NULL) { *user_ptr_del = curr -> next; } else { prev -> next = curr -> next; } free(curr); return 0; }
null
null
63c648585f004bf4898cfba8
2023-02-14T05:34:34.491Z
Help Fix Code
v1
null
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Check if two users are friends * * Return: * - 0 if the users are friends * - 1 if the users are not friends */ int check_friends(const User *user1, User *user2) { for (int i = 0; i < MAX_FRIENDS; i++) { if(user1 -> friends[i] != NULL) { if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { return 0; } } } return 1; } /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Check length of name if (strlen(name) > MAX_NAME - 1) { return 2; } // Create Space for the user & Initialize Variables User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); strcpy(new_user -> profile_pic, ""); new_user -> first_post = NULL; for (int i = 0; i < MAX_FRIENDS; i++) { new_user -> friends[i] = NULL; } new_user -> next = NULL; // First User to be created if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // User(s) already exist User *curr = *user_ptr_add; // printf("Linked List Excluding Last: "); while (curr -> next != NULL) { //printf("%s ", curr -> name); if (strcmp(curr -> name, name) == 0) { return 1; } curr = curr -> next; } // Check for the final node if (strcmp(curr -> name, name) == 0) { return 1; } curr -> next = new_user; //printf("\n%s %s\n", curr -> name, new_user -> name); return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // Check to make sure at least one User exists if ((User *) head == NULL) { return NULL; } // Find a User with the same name, and return if it exists User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr -> name, name) == 0) { return curr; } curr = curr -> next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // Check to make sure at least one User exists if ((User *) curr == NULL) { } else { User *current = (User *) curr; // Loop through every User while (current != NULL) { printf("User: %s\n", current -> name); printf("Profile Pic: %s\n", current -> profile_pic); printf("Friends: "); for (int i = 0; i < MAX_FRIENDS; i++) { if (current -> friends[i] != NULL) { printf("%s ", current -> friends[i] -> name); } } printf("\n\n"); current = current -> next; } } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // Check filename length if (strlen(filename) > MAX_NAME - 1) { return 2; } // Check file existance and workings FILE *pic = fopen(filename, "r"); if (pic == NULL) { return 1; } fclose(pic); // Change user profile picture strncpy(user -> profile_pic, filename, MAX_NAME - 1); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *person1 = find_user(name1, head); User *person2 = find_user(name2, head); // Check to make sure both users exist if (person1 == NULL || person2 == NULL) { return 4; } // Check for the same user passed in twice if (strcmp(name1, name2) == 0) { return 3; } int open1 = MAX_FRIENDS; int open2 = MAX_FRIENDS; for (int i = MAX_FRIENDS - 1; i >= 0; i--) { // Check to see if users are already friends if(person1 -> friends[i] != NULL) { if (strcmp(person1 -> friends[i] -> name, name2) == 0) { return 1; } } // Find first open index of each user's friend list if (person1 -> friends[i] == NULL) { open1 = i; } if (person2 -> friends[i] == NULL) { open2 = i; } // Check to ensure there is room in both user's friend lists if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { return 2; } } // Add to list person1 -> friends[open1] = person2; person2 -> friends[open2] = person1; return 0; } /* * 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. */ int print_user(const User *user) { if (user == NULL) { return 1; } // Print Profile Picture FILE *pic = fopen(user -> profile_pic, "r"); if (pic == NULL) { } else { char line_info[100]; while(fgets(line_info, 100, pic) != NULL) { printf("%s\n", line_info); } fclose(pic); } // Print Name printf("Name: %s\n", user -> name); printf("------------------------------------------\n"); // Print Friends List printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { if (user -> friends[i] != NULL) { printf("%s\n", user -> friends[i] -> name); } } printf("------------------------------------------\n"); // Print Posts printf("Posts:\n"); struct post *curr; if (user -> first_post != NULL) { curr = user -> first_post; } while (curr != NULL) { printf("From: %s\n", curr -> author); printf("Date: %s\n", ctime(curr -> date)); printf("%s\n", curr -> contents); curr = curr -> next; if (curr != NULL) { printf("\n===\n\n"); } } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { // Check both exists if (author == NULL || target == NULL) { return 2; } // Check users are friends int friend = check_friends(author, target); if (friend == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strcpy(new_post -> author, author -> name); time_t *curtime = malloc(sizeof(time_t)); time(curtime); new_post -> date = curtime; new_post -> contents = contents; // Link Post to Post Linked List if (target -> first_post == NULL) { target -> first_post = new_post; } else { new_post -> next = target ->first_post; target -> first_post = new_post; } return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { // Check to ensure user with name exists if (*user_ptr_del == NULL) { return 1; } // Check to see if first user is to be deleted User *user = find_user(name, *user_ptr_del); if (user == NULL) { return 1; } User *prev = NULL; User *curr = *user_ptr_del; if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted } else { // Check the rest of the users while (prev == NULL) { if (strcmp((curr -> next) -> name, name) == 0) { prev = curr; } curr = curr -> next; } } // Delete user from friend's list for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = NULL; if (user -> friends[i] != NULL) { friend = user -> friends[i]; for (int j = 0; j < MAX_FRIENDS; j++) { if (friend -> friends[i] == user) { friend -> friends[i] = NULL; } } } } // Free all dynamically allocated memory Post *curr_post = user -> first_post; while (curr_post != NULL) { Post *temp = curr_post; curr_post = curr_post -> next; free(temp -> contents); free(temp -> date); free(temp); } if (prev == NULL) { *user_ptr_del = curr -> next; } else { prev -> next = curr -> next; } free(curr); return 0; }
Delete User fails to delete user from friend's friend list. When a user with the same name is created afterwards, the user reappears on their friend's list
null
63c648585f004bf4898cfba8
2023-02-14T05:36:30.732Z
Question from Code
v1
Why does the for loop not work in deleting user from friend's list.
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> /* * Check if two users are friends * * Return: * - 0 if the users are friends * - 1 if the users are not friends */ int check_friends(const User *user1, User *user2) { for (int i = 0; i < MAX_FRIENDS; i++) { if(user1 -> friends[i] != NULL) { if (strcmp(user1 -> friends[i] -> name, user2 -> name) == 0) { return 0; } } } return 1; } /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { // Check length of name if (strlen(name) > MAX_NAME - 1) { return 2; } // Create Space for the user & Initialize Variables User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); strcpy(new_user -> profile_pic, ""); new_user -> first_post = NULL; for (int i = 0; i < MAX_FRIENDS; i++) { new_user -> friends[i] = NULL; } new_user -> next = NULL; // First User to be created if (*user_ptr_add == NULL) { *user_ptr_add = new_user; return 0; } // User(s) already exist User *curr = *user_ptr_add; // printf("Linked List Excluding Last: "); while (curr -> next != NULL) { //printf("%s ", curr -> name); if (strcmp(curr -> name, name) == 0) { return 1; } curr = curr -> next; } // Check for the final node if (strcmp(curr -> name, name) == 0) { return 1; } curr -> next = new_user; //printf("\n%s %s\n", curr -> name, new_user -> name); return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // Check to make sure at least one User exists if ((User *) head == NULL) { return NULL; } // Find a User with the same name, and return if it exists User *curr = (User *) head; while (curr != NULL) { if (strcmp(curr -> name, name) == 0) { return curr; } curr = curr -> next; } return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // Check to make sure at least one User exists if ((User *) curr == NULL) { } else { User *current = (User *) curr; // Loop through every User while (current != NULL) { printf("User: %s\n", current -> name); printf("Profile Pic: %s\n", current -> profile_pic); printf("Friends: "); for (int i = 0; i < MAX_FRIENDS; i++) { if (current -> friends[i] != NULL) { printf("%s ", current -> friends[i] -> name); } } printf("\n\n"); current = current -> next; } } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // Check filename length if (strlen(filename) > MAX_NAME - 1) { return 2; } // Check file existance and workings FILE *pic = fopen(filename, "r"); if (pic == NULL) { return 1; } fclose(pic); // Change user profile picture strncpy(user -> profile_pic, filename, MAX_NAME - 1); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { User *person1 = find_user(name1, head); User *person2 = find_user(name2, head); // Check to make sure both users exist if (person1 == NULL || person2 == NULL) { return 4; } // Check for the same user passed in twice if (strcmp(name1, name2) == 0) { return 3; } int open1 = MAX_FRIENDS; int open2 = MAX_FRIENDS; for (int i = MAX_FRIENDS - 1; i >= 0; i--) { // Check to see if users are already friends if(person1 -> friends[i] != NULL) { if (strcmp(person1 -> friends[i] -> name, name2) == 0) { return 1; } } // Find first open index of each user's friend list if (person1 -> friends[i] == NULL) { open1 = i; } if (person2 -> friends[i] == NULL) { open2 = i; } // Check to ensure there is room in both user's friend lists if (open1 == MAX_FRIENDS || open2 == MAX_FRIENDS) { return 2; } } // Add to list person1 -> friends[open1] = person2; person2 -> friends[open2] = person1; return 0; } /* * 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. */ int print_user(const User *user) { if (user == NULL) { return 1; } // Print Profile Picture FILE *pic = fopen(user -> profile_pic, "r"); if (pic == NULL) { } else { char line_info[100]; while(fgets(line_info, 100, pic) != NULL) { printf("%s\n", line_info); } fclose(pic); } // Print Name printf("Name: %s\n", user -> name); printf("------------------------------------------\n"); // Print Friends List printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { if (user -> friends[i] != NULL) { printf("%s\n", user -> friends[i] -> name); } } printf("------------------------------------------\n"); // Print Posts printf("Posts:\n"); struct post *curr; if (user -> first_post != NULL) { curr = user -> first_post; } while (curr != NULL) { printf("From: %s\n", curr -> author); printf("Date: %s\n", ctime(curr -> date)); printf("%s\n", curr -> contents); curr = curr -> next; if (curr != NULL) { printf("\n===\n\n"); } } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { // Check both exists if (author == NULL || target == NULL) { return 2; } // Check users are friends int friend = check_friends(author, target); if (friend == 1) { return 1; } // Create Post Post *new_post = malloc(sizeof(Post)); strcpy(new_post -> author, author -> name); time_t *curtime = malloc(sizeof(time_t)); time(curtime); new_post -> date = curtime; new_post -> contents = contents; // Link Post to Post Linked List if (target -> first_post == NULL) { target -> first_post = new_post; } else { new_post -> next = target ->first_post; target -> first_post = new_post; } return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { // Check to ensure user with name exists if (*user_ptr_del == NULL) { return 1; } // Check to see if first user is to be deleted User *user = find_user(name, *user_ptr_del); if (user == NULL) { return 1; } User *prev = NULL; User *curr = *user_ptr_del; if (strcmp((*user_ptr_del) -> name, name) == 0) { // Check to see if first user is to be deleted } else { // Check the rest of the users while (prev == NULL) { if (strcmp((curr -> next) -> name, name) == 0) { prev = curr; } curr = curr -> next; } } // Delete user from friend's list for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = NULL; if (user -> friends[i] != NULL) { friend = user -> friends[i]; for (int j = 0; j < MAX_FRIENDS; j++) { if (friend -> friends[i] == user) { friend -> friends[i] = NULL; } } } } // Free all dynamically allocated memory Post *curr_post = user -> first_post; while (curr_post != NULL) { Post *temp = curr_post; curr_post = curr_post -> next; free(temp -> contents); free(temp -> date); free(temp); } if (prev == NULL) { *user_ptr_del = curr -> next; } else { prev -> next = curr -> next; } free(curr); return 0; }
null
null
63c648585f004bf4898cfba8
2023-03-10T04:27:08.094Z
Question from Code
v2
why is token[1] an integer and not the first character of the string
char *token = strtok(line, " "); // Check if line is a target line while (token != NULL) { printf("%d%s|\n", token[1], token); token = strtok(NULL, " "); }
null
null
63c648585f004bf4898cfba8
2023-03-10T04:28:03.732Z
Question from Code
v2
why is token[0] and integer and not the first character of the string
char *token = strtok(line, " "); // Check if line is a target line while (token != NULL) { printf("%d%s|\n", token[0], token); token = strtok(NULL, " "); }
null
null
63c648585f004bf4898cfba8
2023-03-10T04:29:00.933Z
General Question
v2
how do i get the first character of a token
null
null
null
63c648585f004bf4898cfba8
2023-03-10T04:31:19.567Z
General Question
v2
how to dereference a token to get the first character
null
null
null
63c648585f004bf4898cfba8
2023-03-10T16:43:35.580Z
Question from Code
v2
why is line 47 causing a segmentation fault
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" #include <ctype.h> /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { // File is already opened for reading char line[MAXLINE]; while (fgets(line, MAXLINE, fp)) { //Check if line is only white space and skip int all_white = 1; for (int i = 0; i < strlen(line); i++) { if (!isspace(line[i])) { all_white = 0; } } if (all_white) { continue; } // Check if line is a comment and skip if (is_comment_or_empty(line)) { continue; } char str[] = " "; char *token = strtok(line, str); printf("|%s|", token); char first = *token; // Check if line is a target line if (isalpha(first)) { printf("Target: "); Rule *curr_rule; // First argument must be a target curr_rule -> target = token; // Second argument is a colon // Subsequent arguments are dependencies if any exists while (token != NULL) { printf("%s ", token); token = strtok(NULL, str); } } // Check if line is an action line if (first == '\t') { printf("Action: "); while (token != NULL) { printf("%s ", token); token = strtok(NULL, str); } } printf("---------------NEXT LINE-------------\n"); // printf("%s", line); } return 0; } /* Return 1 if the line is a comment line, as defined on the assignment handout. Return 0 otherwise. */ int is_comment_or_empty(const char *line) { for (int i = 0; i < strlen(line); i++){ if (line[i] == '#') { return 1; } if (line[i] != '\t' && line[i] != ' ') { return 0; } } return 1; }
null
null
63c648585f004bf4898cfba8
2023-03-10T17:07:14.881Z
Help Write Code
v2
null
null
null
how to get last character of token given by strtok in c
63c648585f004bf4898cfba8
2023-03-11T01:54:29.822Z
Question from Code
v2
why is line 2 causing a segmentation fault
char *token = strtok(line, str); char first = *token;
null
null
63c648585f004bf4898cfba8
2023-03-11T04:29:16.311Z
Question from Code
v2
why is space not incrementing whenever there is a space in line?
int space = 1; for (int i = 0; line[i] != '\0'; i++) { if (line[i] == ' ') { space++; } }
null
null
63c648585f004bf4898cfba8
2023-03-11T04:34:28.000Z
General Question
v2
how to check number of spaces in sentence with spaces
null
null
null
63c648585f004bf4898cfba8
2023-03-11T05:03:49.766Z
Question from Code
v2
Why is the malloc data not saved
char **arr = malloc(sizeof(char *) * space); malloc_check(arr); // Fill array with values int index = 0; while (token != NULL) { arr[index] = malloc(sizeof(char) * (strlen(token) + 1)); malloc_check(arr[index]); strcpy(arr[index], token); index++; token = strtok(NULL, str); } // Create and initialize new Action node Action *new_act = malloc(sizeof(Action)); malloc_check(new_act); new_act->args = arr; new_act->next_act = NULL; // Link Action and Rules Action *last_act = select_last_act(line_first_rule->actions); if (last_act == NULL) { line_first_rule->actions = new_act; } else { last_act->next_act = new_act; }
null
null
63c648585f004bf4898cfba8
2023-03-11T19:01:54.707Z
General Question
v2
How do I use stat in c
null
null
null
63c6485f5f004bf4898cfbe4
2023-02-02T15:14:26.663Z
General Question
v1
Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator)
null
null
null
63c6485f5f004bf4898cfbe4
2023-02-02T15:16:58.632Z
Explain Code
v1
null
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { return -1; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:26:51.636Z
Question from Code
v1
In this code, I have to store the author, contents, and date of a user's post. I use the time() function to get the time, but every time I test it, it gives me a date from the future. What is wrong with how I called time? I need it to give me the current time.
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t now; time(&now); newPost->date = &now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:34:24.761Z
Question from Code
v1
This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = "&now" with newPost->date = "now," I get a warning that I should use "&" before the now. What should I do to fix it?
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t now; time(&now); newPost->date = &now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:35:44.708Z
Question from Code
v1
This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = "&now" with newPost->date = "now," I get a warning that I should use "&" before the now. What should I do to fix it?
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t now; newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:39:36.939Z
Question from Code
v1
This method is supposed to store the author, content, and date of a post. When I run it, I get a future or past date instead of the current date. If I replace newPost->date = "now" with newPost->date = "&now," I get a warning that I should use "&" before the now. What should I do to fix it?
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t *now = (time_t*)malloc(sizeof(time_t)); newPost->date = &now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:41:12.068Z
Question from Code
v1
This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = "&now" with newPost->date = "now," I get a warning that I should use "&" before the now. If I replace newPost->date = "now" with newPost->date = "&now," I get a warning that I remove the "&" before the now. What should I do to fix it?
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t *now = (time_t*)malloc(sizeof(time_t)); newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-08T16:55:54.873Z
Question from Code
v1
This method is supposed to store the author, content, and date of a post. When I run it, I get a future date instead of the current date. If I replace newPost->date = "&now" with newPost->date = "now," I get a warning that I should use "&" before the now. If I replace newPost->date = "now" with newPost->date = "&now," I get a warning that I remove the "&" before the now. I am not using a string literal to store the date either. What should I do to fix it?
int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; time_t *now = (time_t*)malloc(sizeof(time_t)); newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:29:09.733Z
Question from Code
v1
do I have any possible memory leaks in my code?
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:32:43.216Z
Question from Code
v1
how can I fix the memory leak where curr is not NULL and curr->next is NULL . In this case, new_user is never freed?
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:35:00.577Z
Question from Code
v1
do i have any possible memory leaks in my code?
User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:35:52.901Z
Question from Code
v1
do i have any possible memory leaks in my code?
void list_users(const User *curr) { // formatted like the sample output printf("User List\n"); while (curr != NULL) { // indent printf(" "); // name printf("%s\n", curr->name); curr = curr->next; } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:36:35.658Z
Question from Code
v1
do i have any possible memory leaks in my code?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:37:42.324Z
Question from Code
v1
do i have any possible memory leaks in my code?
int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:39:26.432Z
Question from Code
v1
do i have any possible memory leaks in my code?
int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:41:09.825Z
Question from Code
v1
how do i fix the memory leak in find_user where I am returning a pointer to a local variable?
int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:42:34.428Z
Question from Code
v1
how to fix the memory leak by allocating memory for a new user and copying the data from the local user to the new user.
int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:45:39.361Z
Question from Code
v1
do I have any possible memory leaks?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:47:37.864Z
Question from Code
v1
DOES THIS FIX ANY MEMORY LEAKS?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); if (file != NULL) { fclose(file); } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:48:26.294Z
Question from Code
v1
how can i fix the memory leak?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:49:05.680Z
Question from Code
v1
how can I free the file pointer?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:50:18.112Z
Question from Code
v1
do I have any memory leaks?
int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:51:04.918Z
Question from Code
v1
do I have any memory leaks?
/* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:52:28.173Z
Question from Code
v1
where and how should i free memory to fix the memory leaks?
/* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:53:42.289Z
Question from Code
v1
do i have any memory leaks?
/* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:54:32.796Z
Question from Code
v1
how do i free memory to fix memory leaks?
/* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-09T18:55:47.812Z
Help Write Code
v1
null
null
null
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ Avoid memory leaks.
63c6485f5f004bf4898cfbe4
2023-02-10T16:14:05.953Z
Question from Code
v1
where do i have memory leaks and how can I fix them exactly?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void remove_friend(User *user, const char *friend_name); /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // formatted like the sample output printf("User List\n"); while (curr != NULL) { // indent printf(" "); // name printf("%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); time(now); newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = NULL; while (curr != NULL) { // found the user to delete if (strcmp(curr->name, name) == 0) { // update pointers to skip over this user if (prev != NULL) { prev->next = curr->next; } else { *user_ptr_del = curr->next; } // remove this user from their friends' lists of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (curr->friends[i] != NULL) { remove_friend(curr->friends[i], curr->name); } } // free memory associated with this user free(curr); return 0; } prev = curr; curr = curr->next; } // user not found return 1; } void remove_friend(User *user, const char *friend_name) { int i = 0; while (i < MAX_FRIENDS && user->friends[i] != NULL) { if (strcmp(user->friends[i]->name, friend_name) == 0) { // Shift the rest of the elements in the friends array to fill the gap for (int j = i; j < MAX_FRIENDS - 1; j++) { user->friends[j] = user->friends[j + 1]; } // Mark the last element in the array as NULL user->friends[MAX_FRIENDS - 1] = NULL; break; } i++; } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T16:48:19.157Z
Question from Code
v1
where exactly do i add free(newPost->date) and free(newPost)? what lines?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void remove_friend(User *user, const char *friend_name); /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // formatted like the sample output printf("User List\n"); while (curr != NULL) { // indent printf(" "); // name printf("%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); time(now); newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = NULL; while (curr != NULL) { // found the user to delete if (strcmp(curr->name, name) == 0) { // update pointers to skip over this user if (prev != NULL) { prev->next = curr->next; } else { *user_ptr_del = curr->next; } // remove this user from their friends' lists of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (curr->friends[i] != NULL) { remove_friend(curr->friends[i], curr->name); } } // free memory associated with this user free(curr); return 0; } prev = curr; curr = curr->next; } // user not found return 1; } void remove_friend(User *user, const char *friend_name) { int i = 0; while (i < MAX_FRIENDS && user->friends[i] != NULL) { if (strcmp(user->friends[i]->name, friend_name) == 0) { // Shift the rest of the elements in the friends array to fill the gap for (int j = i; j < MAX_FRIENDS - 1; j++) { user->friends[j] = user->friends[j + 1]; } // Mark the last element in the array as NULL user->friends[MAX_FRIENDS - 1] = NULL; break; } i++; } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T16:54:10.395Z
Question from Code
v1
where are the memory leaks coming from?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void remove_friend(User *user, const char *friend_name); /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // formatted like the sample output printf("User List\n"); while (curr != NULL) { // indent printf(" "); // name printf("%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); time(now); newPost->date = now; newPost->next = target->first_post; target->first_post = newPost; return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = NULL; while (curr != NULL) { // found the user to delete if (strcmp(curr->name, name) == 0) { // update pointers to skip over this user if (prev != NULL) { prev->next = curr->next; } else { *user_ptr_del = curr->next; } // remove this user from their friends' lists of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (curr->friends[i] != NULL) { remove_friend(curr->friends[i], curr->name); } } // free memory associated with this user free(curr); return 0; } prev = curr; curr = curr->next; } // user not found return 1; } void remove_friend(User *user, const char *friend_name) { int i = 0; while (i < MAX_FRIENDS && user->friends[i] != NULL) { if (strcmp(user->friends[i]->name, friend_name) == 0) { // Shift the rest of the elements in the friends array to fill the gap for (int j = i; j < MAX_FRIENDS - 1; j++) { user->friends[j] = user->friends[j + 1]; } // Mark the last element in the array as NULL user->friends[MAX_FRIENDS - 1] = NULL; break; } i++; } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T16:56:53.476Z
Question from Code
v1
where are the memory leaks?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void remove_friend(User *user, const char *friend_name); /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; } /* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; } /* * Print the usernames of all users in the list starting at curr. * Names should be printed to standard output, one per line. */ void list_users(const User *curr) { // formatted like the sample output printf("User List\n"); while (curr != NULL) { // indent printf(" "); // name printf("%s\n", curr->name); curr = curr->next; } } /* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); free(file); return 0; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; } /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ // open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } printf("\n"); }// print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; } /* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int areFriends = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { areFriends = 1; break; } } if (!areFriends) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); strcpy(newPost->author, author->name); newPost->contents = contents; // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); time(now); newPost->date = now; free(now); newPost->next = target->first_post; target->first_post = newPost; free(newPost); return 0; } /* * 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. */ int delete_user(const char *name, User **user_ptr_del) { User *curr = *user_ptr_del; User *prev = NULL; while (curr != NULL) { // found the user to delete if (strcmp(curr->name, name) == 0) { // update pointers to skip over this user if (prev != NULL) { prev->next = curr->next; } else { *user_ptr_del = curr->next; } // remove this user from their friends' lists of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (curr->friends[i] != NULL) { remove_friend(curr->friends[i], curr->name); } } // free memory associated with this user free(curr); return 0; } prev = curr; curr = curr->next; } // user not found return 1; } void remove_friend(User *user, const char *friend_name) { int i = 0; while (i < MAX_FRIENDS && user->friends[i] != NULL) { if (strcmp(user->friends[i]->name, friend_name) == 0) { // Shift the rest of the elements in the friends array to fill the gap for (int j = i; j < MAX_FRIENDS - 1; j++) { user->friends[j] = user->friends[j + 1]; } // Mark the last element in the array as NULL user->friends[MAX_FRIENDS - 1] = NULL; break; } i++; } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T17:41:26.663Z
Question from Code
v1
what is causing the memory leak?
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T17:47:17.308Z
Question from Code
v1
where should I free new_user? what line?
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T17:50:35.877Z
Question from Code
v1
where is the memory leak?
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } free(new_user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T17:51:15.862Z
Question from Code
v1
how should i free the memory allocated for new_user
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } free(new_user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-10T17:52:20.916Z
Question from Code
v1
how should i fix the following issues: (1) the memory allocated for new_user is freed before it is added to the list, so the memory is lost, and (2) the memory allocated for new_user is not freed when the function returns.
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } free(new_user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T00:54:32.481Z
Question from Code
v1
where is the memory leak?
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } free(new_user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T00:55:15.586Z
Question from Code
v1
how does the free(new_user) call free the memory allocated by malloc before the new user is added to the list.
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } free(new_user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:04:32.851Z
Question from Code
v1
what are the possible reasons that I might have a memory leak?
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:05:42.136Z
Question from Code
v1
how can i fix if malloc fails?
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; //first, check if user with same name exists while(curr != NULL){ if (strcmp(curr->name, name) == 0){ return 1; } curr = curr->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; // reset the current pointer curr = *user_ptr_add; if (curr == NULL) { *user_ptr_add = new_user; } else { while (curr->next != NULL){ curr = curr->next; } curr->next = new_user; } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:21:31.722Z
Question from Code
v1
do i have memory leak?
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:22:06.787Z
Question from Code
v1
do i have memory leak?
/* * Change the filename for the profile pic of the given user. * * Return: * - 0 on success. * - 1 if the file does not exist or cannot be opened. * - 2 if the filename is too long. */ int update_pic(User *user, const char *filename) { // check if the file name is too long if (strlen(filename) >= sizeof(user->profile_pic) - 1){ return 2; } // declare FILE variable like in PCRS prep (week 3) FILE *file; file = fopen(filename, "r"); // check if failed to be opened if (file == NULL){ return 1; } // can use unsafe strcpy since the filename can fit // close the file that opened successfully strcpy(user->profile_pic, filename); fclose(file); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:23:01.243Z
Question from Code
v1
do i have memory leak?
/* * Return a pointer to the user with this name in * the list starting with head. Return NULL if no such user exists. * * NOTE: You'll likely need to cast a (const User *) to a (User *) * to satisfy the prototype without warnings. */ User *find_user(const char *name, const User *head) { // initialize the head of the linked list const User *curr = head; while (curr != NULL) { // if match is found, return the user if (strcmp(curr->name, name) == 0){ return (User *)curr; } curr = curr->next; } // end of the list was reached without finding user return NULL; } /* * Make two users friends with each other. This is symmetric - a pointer to * each user must be stored in the 'friends' array of the other. * * New friends must be added in the first empty spot in the 'friends' array. * * Return: * - 0 on success. * - 1 if the two users are already friends. * - 2 if the users are not already friends, but at least one already has * MAX_FRIENDS friends. * - 3 if the same user is passed in twice. * - 4 if at least one user does not exist. * * Do not modify either user if the result is a failure. * NOTE: If multiple errors apply, return the *largest* error code that applies. */ int make_friends(const char *name1, const char *name2, User *head) { // find the users User *user1 = find_user(name1, head); User *user2 = find_user(name2, head); //check if at least one user doesn't exist if (!user1 || !user2){ return 4; } // check if the users are equal else if (user1 == user2){ return 3; } // initialize the number of friends each user has int user1_friends = 0; int user2_friends = 0; // loop counts the number of friends each user has to see if they // exceed the maximum number of friends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] != NULL) { user1_friends++; } if (user2->friends[i] != NULL) { user2_friends++; } } if (user1_friends == MAX_FRIENDS || user2_friends == MAX_FRIENDS) { return 2; } // checks if the users are already friends for (int j = 0; j < MAX_FRIENDS; j++) { if (user1->friends[j] == user2) { return 1; } } // otherwise, adds the users to each of their firends for (int i = 0; i < MAX_FRIENDS; i++) { if (user1->friends[i] == NULL) { user1->friends[i] = user2; break; } } for (int i = 0; i < MAX_FRIENDS; i++) { if (user2->friends[i] == NULL) { user2->friends[i] = user1; break; } } return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:23:59.631Z
Question from Code
v1
do i have memory leak?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:25:17.079Z
Question from Code
v1
do i have memory leak?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(current_post->contents); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:25:45.994Z
Question from Code
v1
did i free current_post->contents?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(current_post->contents); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:26:04.010Z
Question from Code
v1
how do i free current_post->contents?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(current_post->contents); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:26:31.785Z
Question from Code
v1
what method do i call to free current_post->contents and where?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(current_post->contents); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:27:34.348Z
Question from Code
v1
where are my memory leaks?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); free(current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:28:35.641Z
Question from Code
v1
do i have memory leaks?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); free(current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); free(current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:29:16.062Z
Question from Code
v1
how to correct the memory leaks?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); free(current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); free(current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:30:53.364Z
Question from Code
v1
do i still have memory leak?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(user->profile_pic); free(user->friends); free(user->first_post); free(current_post->contents); free(user); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:31:47.266Z
Question from Code
v1
do i have memory leak?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * 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. */ int print_user(const User *user) { // check if user is NULL if (user == NULL) { return 1; } // check if there is a profile pic associated with the user if (strlen(user->profile_pic) != 0){ //open the profile pic FILE *fptr; fptr = fopen(user->profile_pic, "r"); if (fptr == NULL){ printf("Cannot open file \n"); } // Read contents from file and print them else { char pic = fgetc(fptr); while (pic != EOF){ printf ("%c", pic); pic = fgetc(fptr); } } fclose(fptr); printf("\n"); } // print name printf("Name: %s\n", user->name); printf("------------------------------------------\n"); // print each friend in order in which they became friends printf("Friends:\n"); for (int i = 0; i < MAX_FRIENDS; i++) { User *friend = user->friends[i]; if (friend != NULL) { printf("%s\n", friend->name); } } printf("------------------------------------------\n"); printf("Posts:\n"); Post *current_post = user->first_post; while (current_post != NULL) { printf("From: %s\n", current_post->author); // convert time_t to a string printf("Date: %s", ctime(current_post->date)); // checks if post is the last to see if "===" is needed if (current_post->next == NULL){ printf("\n%s\n", current_post->contents); free(current_post->contents); } else { printf("\n%s\n\n===\n\n", current_post->contents); free(current_post->contents); } current_post = current_post->next; } printf("------------------------------------------\n"); free(user->profile_pic); free(user->friends); free(user->first_post); return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:42:51.363Z
Help Fix Code
v1
null
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr1 = *user_ptr_add; //first, check if user with same name exists while(curr1 != NULL){ if (strcmp(curr1->name, name) == 0){ return 1; } curr1 = curr1->next; } // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; User *curr2 = *user_ptr_add; if (curr2 == NULL) { *user_ptr_add = new_user; } else { while (curr2->next != NULL){ curr2 = curr2->next; } curr2->next = new_user; } return 0; }
this function is not supposed to have any memory leaks but it does
null
63c6485f5f004bf4898cfbe4
2023-02-11T01:47:15.563Z
Help Fix Code
v1
null
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 // Max number of friends a user can have 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; } Post; /* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr1 = *user_ptr_add; //first, check if user with same name exists while(curr1 != NULL){ if (strcmp(curr1->name, name) == 0){ return 1; } curr1 = curr1->next; } free(curr1); // next, check if the name cannot fit in the 'name' array // the '-1' reserves space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = (User *)malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; User *curr2 = *user_ptr_add; if (curr2 == NULL) { *user_ptr_add = new_user; } else { while (curr2->next != NULL){ curr2 = curr2->next; } curr2->next = new_user; } return 0; }
this function is not supposed to have any memory leaks but it does
null
63c6485f5f004bf4898cfbe4
2023-02-12T03:45:03.614Z
Question from Code
v1
I need to terminate the program using a non-zero status if malloc returns NULL. Using status 3 gives me a segmentation fault. what do I do?
/* * Create a new user with the given name. Insert it at the tail of the list * of users whose head is pointed to by *user_ptr_add. * * Return: * - 0 if successful * - 1 if a user by this name already exists in this list * - 2 if the given name cannot fit in the 'name' array * (don't forget about the null terminator) */ int create_user(const char *name, User **user_ptr_add) { User *curr1 = *user_ptr_add; //check if user with same name exists while(curr1 != NULL){ if (strcmp(curr1->name, name) == 0){ return 1; } curr1 = curr1->next; } // check if the name cannot fit in the 'name' array // use -1 to save space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = malloc(sizeof(User)); if (new_user == NULL){ perror("Cannot allocate space for user"); exit(3); } strcpy(new_user->name, name); new_user->next = NULL; User *curr2 = *user_ptr_add; if (curr2 == NULL) { *user_ptr_add = new_user; } else { while (curr2->next != NULL){ curr2 = curr2->next; } } curr2->next = new_user; for (int i = 0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } for (int i = 0; i < MAX_FRIENDS; i++) { new_user->profile_pic[i] = '\0'; } new_user->first_post = NULL; return 0; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-12T04:33:28.312Z
Question from Code
v1
where is my memory leak?
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int friend = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { friend = 1; break; } } if (!friend) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); if (newPost != NULL && now != NULL) { strcpy(newPost->author, author->name); newPost->contents = contents; time(now); newPost->date = now;; newPost->next = target->first_post; target->first_post = newPost; return 0; } else { perror("Cannot allocate space"); exit(3); } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-12T04:43:41.767Z
Question from Code
v1
contents doesn't need to be freed. what is causing the memory leak?
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int friend = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { friend = 1; break; } } if (!friend) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); if (newPost != NULL && now != NULL) { strcpy(newPost->author, author->name); newPost->contents = contents; time(now); newPost->date = now;; newPost->next = target->first_post; target->first_post = newPost; return 0; } else { perror("Cannot allocate space"); exit(3); } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-12T04:44:10.098Z
Question from Code
v1
contents doesn't need to be freed. what is causing the memory leak?
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } // numerical Boolean to make predicate logic easy int friend = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { friend = 1; break; } } if (!friend) { return 1; } // allocate space for a new post Post *newPost = (Post*)malloc(sizeof(Post)); // allocate space for the time time_t *now = (time_t*)malloc(sizeof(time_t)); if (newPost != NULL && now != NULL) { strcpy(newPost->author, author->name); newPost->contents = contents; time(now); newPost->date = now;; newPost->next = target->first_post; target->first_post = newPost; free(now); return 0; } else { perror("Cannot allocate space"); exit(3); } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-12T18:30:13.983Z
Question from Code
v1
do i have a memory leak?
int create_user(const char *name, User **user_ptr_add) { User *curr1 = *user_ptr_add; //check if user with same name exists while(curr1 != NULL){ if (strcmp(curr1->name, name) == 0){ return 1; } curr1 = curr1->next; } // check if the name cannot fit in the 'name' array // use -1 to save space for the null terminator if (strlen(name) >= MAX_NAME - 1){ return 2; } // create a new user and add it to the end of the list User *new_user = malloc(sizeof(User)); strcpy(new_user->name, name); new_user->next = NULL; User *curr2 = *user_ptr_add; if (new_user != NULL) { if (curr2 == NULL) { *user_ptr_add = new_user; } else { while (curr2->next != NULL){ curr2 = curr2->next; } curr2->next = new_user; } for (int i = 0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } for (int i = 0; i < MAX_FRIENDS; i++) { new_user->profile_pic[i] = '\0'; } new_user->first_post = NULL; return 0; } else { perror("Cannot allocate space"); exit(3); } }
null
null
63c6485f5f004bf4898cfbe4
2023-02-12T18:31:31.025Z
Help Write Code
v1
null
null
null
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */
63c6485f5f004bf4898cfbe4
2023-02-12T18:39:11.302Z
Help Fix Code
v1
null
/* * Make a new post from 'author' to the 'target' user, * containing the given contents, IF the users are friends. * * Insert the new post at the *front* of the user's list of posts. * * 'contents' is a pointer to heap-allocated memory - you do not need * to allocate more memory to store the contents of the post. * * Return: * - 0 on success * - 1 if users exist but are not friends * - 2 if either User pointer is NULL */ int make_post(const User *author, User *target, char *contents) { if (!author || !target) { return 2; } int friend = 0; for (int i = 0; i < MAX_FRIENDS; i++) { if (target->friends[i] == author) { friend = 1; break; } } if (friend == 0) { return 1; } // allocate space for a new post Post *new_post = malloc(sizeof(Post)); // allocate space for the time time_t *now = malloc(sizeof(time_t)); if (new_post != NULL) { strcpy(new_post->author, author->name); new_post->contents = contents; time(now); new_post->date = now;; new_post->next = target->first_post; target->first_post = new_post; return 0; } else { free(new_post); free(now); perror("Cannot allocate space"); exit(3); } }
valgrind tells me that line 32 is causing memory leak. what can I do to fix this?
null
63c6485f5f004bf4898cfbe4
2023-02-23T23:51:53.568Z
Help Write Code
v1
null
null
null
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Notice that there is one struct rule_node for each rule in the original makefile and an additional struct rule_node for each dependency that is not also a target. You must follow this design. You are are also required to have the head of the list be the first rule in the original makefile. However, depending on how you do your parsing, the struct rule_node elements may come in a different order in your linked list of rules. You will see that the actions are stored in a struct action_node that has a member args. This array has the format required by the second parameter to execvp. Use the man page for execvp to understand this format. In particular, notice that the first element is the executable name, subsequent elements are the arguments for that executable, and these are followed by an extra NULL pointer element. Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. return NULL; }
63c6485f5f004bf4898cfbe4
2023-02-24T19:57:33.697Z
Help Fix Code
v1
null
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(strtok(line, " \t\n")); // Parse the dependencies and create a new Dependency node for each of them Dependency *dep_head = NULL, *dep_curr = NULL; char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (dep_curr == NULL) { dep_head = dep; } else { dep_curr->next_dep = dep; } dep_curr = dep; dep_str = strtok(NULL, " \t\n"); } rule->dependencies = dep_head; // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
when i run this to make a makefile, the first letter of every target is missing and there are two colons instead of one.
null
63c6485f5f004bf4898cfbe4
2023-02-24T20:08:25.846Z
Help Fix Code
v1
null
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(strtok(line, " \t\n")); // Parse the dependencies and create a new Dependency node for each of them Dependency *dep_head = NULL, *dep_curr = NULL; char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (dep_curr == NULL) { dep_head = dep; } else { dep_curr->next_dep = dep; } dep_curr = dep; dep_str = strtok(NULL, " \t\n"); } rule->dependencies = dep_head; // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
the first letter in the target is missing and there are two colons showing up after the target instead of one. i did not use strtok(line, " \t\n") + 1 or strtok(NULL, " \t\n") + 1
null
63c6485f5f004bf4898cfbe4
2023-02-24T20:10:40.614Z
Help Fix Code
v1
null
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(strtok(line, " \t\n")); // Parse the dependencies and create a new Dependency node for each of them Dependency *dep_head = NULL, *dep_curr = NULL; char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (dep_curr == NULL) { dep_head = dep; } else { dep_curr->next_dep = dep; } dep_curr = dep; dep_str = strtok(NULL, " \t\n"); } rule->dependencies = dep_head; // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
the first letter in the target is not supposed to be missing and there is supposed to be one colon not two colons showing up after the target
null
63c6485f5f004bf4898cfbe4
2023-02-24T20:11:33.317Z
Question from Code
v1
how do i changing strtok() to parse the target until a colon.
/* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *head = NULL, *current = NULL; while (fgets(line, MAXLINE, fp)) { if (strlen(line) == MAXLINE - 1 && line[MAXLINE - 2] != '\n') { fprintf(stderr, "ERROR: Line too long\n"); exit(1); } // Ignore empty lines and comments if (line[0] == '\n' || line[0] == '#') { continue; } // If the line starts with a tab, it's an action line if (line[0] == '\t') { if (current == NULL) { fprintf(stderr, "ERROR: Action line without target\n"); exit(1); } Action *act = (Action *) malloc(sizeof(Action)); act->args = (char **) malloc(sizeof(char *) * MAXLINE); // Parse the command into an array of strings char *arg = strtok(line, " \t\n"); int i = 0; while (arg != NULL) { act->args[i] = strdup(arg); i++; if (i >= MAXLINE - 1) { fprintf(stderr, "ERROR: Too many arguments in action line\n"); exit(1); } arg = strtok(NULL, " \t\n"); } act->args[i] = NULL; act->next_act = NULL; // Attach the action to the current rule if (current->actions == NULL) { current->actions = act; } else { Action *act_curr = current->actions; while (act_curr->next_act != NULL) { act_curr = act_curr->next_act; } act_curr->next_act = act; } } else { // It's a target line if (current != NULL && current->target == NULL) { fprintf(stderr, "ERROR: Empty target\n"); exit(1); } else { // Parse the target name and create a new Rule node for this target Rule *rule = (Rule *) malloc(sizeof(Rule)); rule->target = strdup(strtok(line, " \t\n")); // Parse the dependencies and create a new Dependency node for each of them Dependency *dep_head = NULL, *dep_curr = NULL; char *dep_str = strtok(NULL, " \t\n"); while (dep_str != NULL) { Dependency *dep = (Dependency *) malloc(sizeof(Dependency)); dep->rule = (Rule *) malloc(sizeof(Rule)); dep->rule->target = strdup(dep_str); dep->next_dep = NULL; // Attach the dependency to the current rule if (dep_curr == NULL) { dep_head = dep; } else { dep_curr->next_dep = dep; } dep_curr = dep; dep_str = strtok(NULL, " \t\n"); } rule->dependencies = dep_head; // Attach the rule to the list of rules rule->actions = NULL; rule->next_rule = NULL; if (head == NULL) { head = rule; } else { current->next_rule = rule; } current = rule; } } } return head; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-25T00:32:29.390Z
Question from Code
v1
why am i getting a segmentation fault
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0; while (fgets(line, MAXLINE, fp) != NULL) { line_num++; if (is_comment_or_empty(line)) { continue; } // Remove trailing newline line[strlen(line) - 1] = '\0'; // Check if line is a target if (line[0] == '\t') { // If no rule has been created yet, this is an error if (cur_rule == NULL) { fprintf(stderr, "ERROR: action line before target line at line %d", line_num); exit(1); // If no action has been created yet, create one } else if (cur_action == NULL) { cur_action = malloc(sizeof(Action)); cur_action->args = parse_args(line); cur_action->next_act = NULL; cur_rule->actions = cur_action; // Otherwise, add to the end of the list of actions } else { cur_action->next_act = malloc(sizeof(Action)); cur_action = cur_action->next_act; cur_action->args = parse_args(line); cur_action->next_act = NULL; } // Otherwise, line is a target } else { // If a rule has already been created, add it to the list of rules if (cur_rule != NULL) { cur_rule->next_rule = malloc(sizeof(Rule)); cur_rule = cur_rule->next_rule; // Otherwise, create the first rule } else { cur_rule = malloc(sizeof(Rule)); rules = cur_rule; } // Parse the target cur_rule->target = parse_target(line); cur_rule->dependencies = NULL; cur_rule->actions = NULL; cur_rule->next_rule = NULL; // Parse the dependencies char *dep_line = parse_dependencies(line); if (dep_line != NULL) { cur_dep = malloc(sizeof(Dependency)); cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; cur_rule->dependencies = cur_dep; while ((dep_line = parse_dependencies(NULL)) != NULL) { cur_dep->next_dep = malloc(sizeof(Dependency)); cur_dep = cur_dep->next_dep; cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; } // If there are no dependencies, set cur_dep to NULL } else { cur_dep = NULL; } // Set cur_action to NULL cur_action = NULL; } } return rules; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-25T00:33:14.712Z
Question from Code
v1
why am i getting a segmentation fault? cur_rule and cur_action variables are initialized to NULL
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0; while (fgets(line, MAXLINE, fp) != NULL) { line_num++; if (is_comment_or_empty(line)) { continue; } // Remove trailing newline line[strlen(line) - 1] = '\0'; // Check if line is a target if (line[0] == '\t') { // If no rule has been created yet, this is an error if (cur_rule == NULL) { fprintf(stderr, "ERROR: action line before target line at line %d", line_num); exit(1); // If no action has been created yet, create one } else if (cur_action == NULL) { cur_action = malloc(sizeof(Action)); cur_action->args = parse_args(line); cur_action->next_act = NULL; cur_rule->actions = cur_action; // Otherwise, add to the end of the list of actions } else { cur_action->next_act = malloc(sizeof(Action)); cur_action = cur_action->next_act; cur_action->args = parse_args(line); cur_action->next_act = NULL; } // Otherwise, line is a target } else { // If a rule has already been created, add it to the list of rules if (cur_rule != NULL) { cur_rule->next_rule = malloc(sizeof(Rule)); cur_rule = cur_rule->next_rule; // Otherwise, create the first rule } else { cur_rule = malloc(sizeof(Rule)); rules = cur_rule; } // Parse the target cur_rule->target = parse_target(line); cur_rule->dependencies = NULL; cur_rule->actions = NULL; cur_rule->next_rule = NULL; // Parse the dependencies char *dep_line = parse_dependencies(line); if (dep_line != NULL) { cur_dep = malloc(sizeof(Dependency)); cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; cur_rule->dependencies = cur_dep; while ((dep_line = parse_dependencies(NULL)) != NULL) { cur_dep->next_dep = malloc(sizeof(Dependency)); cur_dep = cur_dep->next_dep; cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; } // If there are no dependencies, set cur_dep to NULL } else { cur_dep = NULL; } // Set cur_action to NULL cur_action = NULL; } } return rules; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-25T00:34:49.220Z
Question from Code
v1
how do i fix the seg fault?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0; while (fgets(line, MAXLINE, fp) != NULL) { line_num++; if (is_comment_or_empty(line)) { continue; } // Remove trailing newline line[strlen(line) - 1] = '\0'; // Check if line is a target if (line[0] == '\t') { // If no rule has been created yet, this is an error if (cur_rule == NULL) { fprintf(stderr, "ERROR: action line before target line at line %d", line_num); exit(1); // If no action has been created yet, create one } else if (cur_action == NULL) { cur_action = malloc(sizeof(Action)); cur_action->args = parse_args(line); cur_action->next_act = NULL; cur_rule->actions = cur_action; // Otherwise, add to the end of the list of actions } else { cur_action->next_act = malloc(sizeof(Action)); cur_action = cur_action->next_act; cur_action->args = parse_args(line); cur_action->next_act = NULL; } // Otherwise, line is a target } else { // If a rule has already been created, add it to the list of rules if (cur_rule != NULL) { cur_rule->next_rule = malloc(sizeof(Rule)); cur_rule = cur_rule->next_rule; // Otherwise, create the first rule } else { cur_rule = malloc(sizeof(Rule)); rules = cur_rule; } // Parse the target cur_rule->target = parse_target(line); cur_rule->dependencies = NULL; cur_rule->actions = NULL; cur_rule->next_rule = NULL; // Parse the dependencies char *dep_line = parse_dependencies(line); if (dep_line != NULL) { cur_dep = malloc(sizeof(Dependency)); cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; cur_rule->dependencies = cur_dep; while ((dep_line = parse_dependencies(NULL)) != NULL) { cur_dep->next_dep = malloc(sizeof(Dependency)); cur_dep = cur_dep->next_dep; cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; } // If there are no dependencies, set cur_dep to NULL } else { cur_dep = NULL; } // Set cur_action to NULL cur_action = NULL; } } return rules; }
null
null
63c6485f5f004bf4898cfbe4
2023-02-25T00:35:51.405Z
Question from Code
v1
how do i initialize cur_rule to NULL before the first iteration of the loop.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "pmake.h" /* Read from the open file fp, and create the linked data structure that represents the Makefile contained in the file. See the top of pmake.h for the specification of Makefile contents. */ Rule *parse_file(FILE *fp) { char line[MAXLINE]; Rule *rules = NULL; Rule *cur_rule = NULL; Action *cur_action = NULL; Dependency *cur_dep = NULL; int line_num = 0; while (fgets(line, MAXLINE, fp) != NULL) { line_num++; if (is_comment_or_empty(line)) { continue; } // Remove trailing newline line[strlen(line) - 1] = '\0'; // Check if line is a target if (line[0] == '\t') { // If no rule has been created yet, this is an error if (cur_rule == NULL) { fprintf(stderr, "ERROR: action line before target line at line %d", line_num); exit(1); // If no action has been created yet, create one } else if (cur_action == NULL) { cur_action = malloc(sizeof(Action)); cur_action->args = parse_args(line); cur_action->next_act = NULL; cur_rule->actions = cur_action; // Otherwise, add to the end of the list of actions } else { cur_action->next_act = malloc(sizeof(Action)); cur_action = cur_action->next_act; cur_action->args = parse_args(line); cur_action->next_act = NULL; } // Otherwise, line is a target } else { // If a rule has already been created, add it to the list of rules if (cur_rule != NULL) { cur_rule->next_rule = malloc(sizeof(Rule)); cur_rule = cur_rule->next_rule; // Otherwise, create the first rule } else { cur_rule = malloc(sizeof(Rule)); rules = cur_rule; } // Parse the target cur_rule->target = parse_target(line); cur_rule->dependencies = NULL; cur_rule->actions = NULL; cur_rule->next_rule = NULL; // Parse the dependencies char *dep_line = parse_dependencies(line); if (dep_line != NULL) { cur_dep = malloc(sizeof(Dependency)); cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; cur_rule->dependencies = cur_dep; while ((dep_line = parse_dependencies(NULL)) != NULL) { cur_dep->next_dep = malloc(sizeof(Dependency)); cur_dep = cur_dep->next_dep; cur_dep->rule = find_rule(rules, dep_line); cur_dep->next_dep = NULL; } // If there are no dependencies, set cur_dep to NULL } else { cur_dep = NULL; } // Set cur_action to NULL cur_action = NULL; } } return rules; }
null
null