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
63c647bb5f004bf4898cf63a
2023-04-02T21:05:48.528Z
Question from Code
v2
Why could the printf statements on lines 4 and 9 causing a segmentation fault?
while (i) { fd_set select_fds = all_fds; printf("waiting...\n"); if (select(max_fd + 1, &select_fds, NULL, NULL, NULL) == -1) { perror("server: select"); exit(1); } printf("done waiting\n"); if (FD_ISSET(client_fd, &select_fds)) { if (readn(client_fd, buffer, BUFSIZE - 1) == 0) { printf("client disconnected\n"); break; } char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(buffer, cmd_argv); printf("cmd_argc: %d, cmd_argv[0]: %s | cmd_argv[1]: %s\n", cmd_argc, cmd_argv[0], cmd_argv[1]); fflush(stdout); printf("user: %s\n", user_list->name); fflush(stdout); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &user_list, user_name) == -1) { break; } printf("> "); } i = 0; } return 0; }
null
null
63c647bb5f004bf4898cf63a
2023-04-02T21:10:55.803Z
General Question
v2
Is it possible for a program to only work as intended when it is run through the gdb debugger?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-02T23:52:02.934Z
General Question
v2
what does the FD_CLR macro do?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-03T16:38:58.644Z
General Question
v2
How do you use strncat to safely concatenate two strings?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-03T18:01:24.513Z
General Question
v2
can I use snprintf on initialized memory?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-03T18:01:59.458Z
General Question
v2
can I use snprintf on uninitialized memory?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-03T19:53:09.247Z
Question from Code
v2
does this Makefile have any redundancies?
PORT = 51345 CFLAGS = -DPORT =\$(PORT) - g - std = gnu99 - Wall - Werror OFLAGS = -g - std = gnu99 - Wall - Werror all : friend_server friend_server : friend_server.o friend_socket.o friends.o partial_io.o clients.o gcc ${CFLAGS} - o $ @$ ^ friend_server.o : friend_server.c friend_socket.h friends.h partial_io.h gcc ${CFLAGS} - c $ < %.o : %.c friend_socket.h friends.h partial_io.h gcc ${OFLAGS} - c $ < clean : rm - f *.o friend_server
null
null
63c647bb5f004bf4898cf63a
2023-04-06T04:45:46.156Z
General Question
v2
can you run strlen() on an integer?
null
null
null
63c647bb5f004bf4898cf63a
2023-04-06T04:50:53.653Z
General Question
v2
is / integer division?
null
null
null
63c647bc5f004bf4898cf63f
2023-01-22T08:49:29.188Z
General Question
v1
is there a built in function to remove element at specific index of an array
null
null
null
63c647bc5f004bf4898cf63f
2023-01-22T10:24:07.206Z
Question from Code
v1
why does this for loop print new twice
char line[31]; int in; char new[9]; for (int i = 1; i < 10; i++) { new[i-1] = line[i]; printf("%s\n", new); }
null
null
63c647bc5f004bf4898cf63f
2023-01-22T10:26:15.357Z
Question from Code
v1
why does this for loop print new twice.
char line[31]; int in; char new[9]; for (int i = 1; i < 10; i++) { new[i-1] = line[i]; } printf("%s\n", new);
null
null
63c647bc5f004bf4898cf63f
2023-01-22T10:29:35.448Z
Question from Code
v1
Why does this code print new twice. For example, output was "rwx-------rwx------" but expected output is rwx-------
char line[31]; int in; char new[9]; for (int i = 1; i < 10; i++) { new[i-1] = line[i]; } printf("%s\n", new);
null
null
63c647bc5f004bf4898cf63f
2023-01-22T10:30:45.774Z
Help Fix Code
v1
null
char line[31]; int in; char new[9]; for (int i = 1; i < 10; i++) { new[i-1] = line[i]; } printf("%s\n", new);
output is rwx-------rwx------, expected output is rwx-------
null
63c647bc5f004bf4898cf63f
2023-01-22T10:38:28.465Z
General Question
v1
The string new is not properly terminated with a null character. -> This is fixed by adding \0 to the end of the string. How would I do that?
null
null
null
63c647bc5f004bf4898cf63f
2023-01-27T07:48:44.749Z
Help Fix Code
v1
null
int main(int argc, char** argv) { char line[11]; int size; int count = 0; int int_size = strtol(argv[1], NULL, 10); char new[10]; if (!(argc == 2 || argc == 3)) { fprintf(stderr, "USAGE: count_large size [permissions]\n"); return 1; } else if (argc == 3) { // Skip first line scanf("%*s, %*d"); while(scanf("%10s %*d %*s %*s %d %*s %*d %*s %*s",line, &size) != EOF) { // Checks if first index is '-' because we ignore if 'd' if (line[0] == '-') { // input elements of line into new array without first element for (int i = 0; i < 10; i++){ new[i] = line[i + 1]; } if ((check_permissions(new, argv[2]) == 0) && (size > int_size)) { count += 1; } } } printf("%d\n", count); } else { scanf("%*s, %*d"); while(scanf("%*s %*d %*s %*s %d %*s %*d %*s %*s", &size) != EOF) { if (size > int_size) { count += 1; } } printf("%d\n", count); } return 0; }
EXC_BAD_ACCESS (code=1, address=0x0) in line 5.
null
63c647bc5f004bf4898cf63f
2023-02-11T19:31:19.614Z
General Question
v1
is there a contains function? like if array contains item boolean
null
null
null
63c647bc5f004bf4898cf63f
2023-02-11T22:26:10.436Z
Question from Code
v1
do i also need to free target->first_post->date?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *(new_post->date) = time(NULL); new_post->next = target->first_post; target->first_post = new_post; target->first_post->date = (new_post->date); free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-11T22:28:18.478Z
Question from Code
v1
is there anything else i should free?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *(new_post->date) = time(NULL); new_post->next = target->first_post; target->first_post = new_post; target->first_post->date = (new_post->date); free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-11T22:29:42.686Z
Question from Code
v1
so freeing free(new_post->date); free(target->first_post->date); free(new_post->contents); free(new_post); would be sufficient?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *(new_post->date) = time(NULL); new_post->next = target->first_post; target->first_post = new_post; target->first_post->date = (new_post->date); free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-11T22:30:41.631Z
Question from Code
v1
so freeing free(target->first_post->date); free(new_post); would be sufficient?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *(new_post->date) = time(NULL); new_post->next = target->first_post; target->first_post = new_post; target->first_post->date = (new_post->date); free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-11T22:31:37.645Z
Question from Code
v1
you stated that I should free new_post->contents and new_post->date, would it be somewhere else outside of this function?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *(new_post->date) = time(NULL); new_post->next = target->first_post; target->first_post = new_post; target->first_post->date = (new_post->date); free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:19:44.540Z
Question from Code
v1
do i need to free() anything
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:20:54.337Z
Question from Code
v1
do i need to free() anything?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:21:51.720Z
Question from Code
v1
do i need to free() anything else?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { user->friends[MAX_FRIENDS - 1] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) | (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1 + 1] = user_2; user_2->friends[friends_name2 + 1] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; free(new_post); free(date); 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:24:11.704Z
Question from Code
v1
do i need to free() anything?/
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { user->friends[MAX_FRIENDS - 1] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) | (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1 + 1] = user_2; user_2->friends[friends_name2 + 1] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; free(new_post); free(date); 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:24:51.746Z
Question from Code
v1
what do i free() specifically?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { user->friends[MAX_FRIENDS - 1] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) | (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1 + 1] = user_2; user_2->friends[friends_name2 + 1] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; free(new_post); free(date); 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:29:59.103Z
Question from Code
v1
Anything else i need to free()?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { user->friends[MAX_FRIENDS - 1] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) | (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1 + 1] = user_2; user_2->friends[friends_name2 + 1] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post->date) free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T06:30:57.515Z
Question from Code
v1
specifically, what do i need to still free()
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { user->friends[MAX_FRIENDS - 1] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) | (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) | (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1 + 1] = user_2; user_2->friends[friends_name2 + 1] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post->date) free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T07:25:31.771Z
Question from Code
v1
I am receiving memory leaks, where else can I use free()?
#include "friends.h" #include <string.h> #include <stdio.h> #include <stdlib.h> void init(User *user) { for (int i = 0; i < MAX_FRIENDS; i++) { user->friends[i] = 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) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(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) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)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) { printf("User List\n"); while (curr != NULL) { 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) { if (strlen(filename) >= 33) { return 2; } if (fopen(filename, "r") == NULL) { return 1; } else { strcpy(user->profile_pic, filename); 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 count_friends(const char *name, User *head){ int count = 0; User *user_1 = find_user(name, head); for (int i = 0; i < MAX_FRIENDS; i++) { if (user_1->friends[i] != NULL) { count ++; } } return count; } int make_friends(const char *name1, const char *name2, User *head) { User *user_1 = find_user(name1, head); User *user_2 = find_user(name2, head); int friends_name1 = count_friends(name1, head); int friends_name2 = count_friends(name2, head); if ((user_1 == NULL) || (user_2 == NULL)) { return 4; } if (strcmp(name1, name2) == 0) { return 3; } for (int i = 0; i < MAX_FRIENDS; i++) { if ((user_1->friends[i] != user_2) & ((friends_name1 == MAX_FRIENDS) || (friends_name2 == MAX_FRIENDS))) { return 2; } if (user_1->friends[i] == user_2) { return 1; } } user_1->friends[friends_name1] = user_2; user_2->friends[friends_name2] = user_1; 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; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_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 is_friends(const User *name1, User *name2) { int i = 0; int friends = 0; while (i < MAX_FRIENDS) { if (name1 == name2->friends[i]) { friends = 1; } i++; } return friends; } int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; 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. */ void remove_friends(const char *name, User **user_ptr_r){ struct user *last = *user_ptr_r; while (last != NULL) { for (int i = 0; i < MAX_FRIENDS; i++) { if (last->friends[i] != NULL && strcmp(last->friends[i]->name, name) == 0) { // find name wanted to be deleted last->friends[i] = NULL; } } if (strcmp(last->name, name) == 0) { last->friends[MAX_FRIENDS - 1] = NULL; } last = last->next; } } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post->date) free(post); post = next_post; } // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T07:31:55.308Z
Question from Code
v1
do i need to free() anything in this function?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T08:17:24.023Z
Question from Code
v1
Receive still reachable byts, but i am closing the file how can I fix?
int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_post->next; } } printf("------------------------------------------\n"); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T08:24:25.411Z
Question from Code
v1
i am receiving 472 still reachable byts in this code even though I'm closing file, why?
int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { fclose(file); printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_post->next; } } printf("------------------------------------------\n"); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T08:28:08.526Z
Question from Code
v1
Receiving still reachable byts why? whats a fix
int print_user(const User *user) { if (user == NULL) { return 1; } int c; FILE *file; file = fopen(user->profile_pic, "r"); if (file != NULL) { while ((c = fgetc(file)) != EOF) printf("%c", c); fclose(file); printf("\nName: %s\n", user->name); }else { fclose(file); printf("Name: %s\n", user->name); } printf("------------------------------------------\n"); printf("Friends:\n"); for (int i = 0; i < 10; i++) { if (user->friends[i] != NULL) { printf("%s\n", user->friends[i]->name); } } printf("------------------------------------------\n"); printf("Posts: \n"); Post *cur_post = user->first_post; while (cur_post != NULL) { if (cur_post->next != NULL) { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n\n", cur_post->contents); printf("===\n"); cur_post = cur_post->next; printf("\n"); } else { printf("From: %s\n", cur_post->author); char *date_str = ctime(cur_post->date); printf("Date: %s\n", date_str); printf("%s\n", cur_post->contents); cur_post = cur_post->next; } } printf("------------------------------------------\n"); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T09:09:28.856Z
Question from Code
v1
I am receiving memory leaks, due to this code how would I fix?
int make_post(const User *author, User *target, char *contents) { if ((author == NULL) || (target == NULL)) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } new_post->contents = malloc(strlen(contents) + 1); if (new_post->contents == NULL) { perror("malloc"); return 2; } strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror free(new_post->contents); free(new_post); return 2; } *date = time(NULL); new_post->date = date; new_post->next = target->first_post; target->first_post = new_post; return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T09:15:33.457Z
Question from Code
v1
date is receiving junk when i am printing, whats a fix
int make_post(const User *author, User *target, char *contents) { if ((author == NULL) || (target == NULL)) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } new_post->contents = malloc(strlen(contents) + 1); if (new_post->contents == NULL) { perror("malloc"); return 2; } strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); time_t *date = malloc(sizeof(time_t)); if (date == NULL) { perror("malloc"); // Look up the documentation for perror free(new_post->contents); free(new_post); return 2; } *date = time(NULL); time_t = date; new_post->date = *date; new_post->next = target->first_post; target->first_post = new_post; return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T15:03:02.789Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } new_post->contents = malloc(strlen(contents) + 1); if (new_post->contents == NULL) { perror("malloc"); return 2; } strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } time(new_post->date); new_post->next = target->first_post; target->first_post = new_post; time(new_post->date); new_post->next = target->first_post; target->first_post = new_post; return 0; }
create post
null
63c647bc5f004bf4898cf63f
2023-02-14T15:04:22.914Z
Help Fix Code
v1
null
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } new_post->contents = malloc(strlen(contents) + 1); if (new_post->contents == NULL) { perror("malloc"); return 2; } strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } time(new_post->date); new_post->next = target->first_post; target->first_post = new_post; free(new_post->date); free(new_post->contents); free(new_post); 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.
null
63c647bc5f004bf4898cf63f
2023-02-14T15:19:05.961Z
Question from Code
v1
why does free(new_user) cause problems
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) > 33) { return 2; } struct user *new_user = malloc(sizeof(struct user)); strcpy(new_user->name, name); new_user->next = NULL; struct user *last = *user_ptr_add; while (last != NULL) { if (strcmp(last->name, name) == 0) { return 1; } last = last->next; } if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { last = *user_ptr_add; while (last->next != NULL) { last = last->next; } last->next = new_user; } init(new_user); free(new_user); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T16:11:09.608Z
Question from Code
v1
I receive a segmentation error if i use free() in this code, but if I don't use free I have a memory leak. How would I fix this?
int make_post(const User *author, User *target, char *contents) { if (author == NULL | target == NULL) { return 2; } if (!(is_friends(author, target))) { return 1; } Post *new_post = malloc(sizeof(Post)); if (new_post == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } strcpy(new_post->author, author->name); new_post->contents = contents; new_post->date = malloc(sizeof(time_t)); if (new_post->date == NULL) { perror("malloc"); // Look up the documentation for perror return 2; } time(new_post->date); new_post->next = target->first_post; target->first_post = new_post; free(new_post->date); free(new_post); return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T19:04:19.816Z
Question from Code
v1
for this code I am receiving a segmentation error, how can i fix
void cleanup(Post **pst) { free((*pst)->date); free((*pst)->contents); free((*pst)); *pst = NULL; } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; while (post != NULL) { Post *next_post = post->next; free(post->contents); free(post->date); free(post); post = next_post; } post = NULL; // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T19:05:18.807Z
Question from Code
v1
for this code I am receiving a segmentation error, how can i fix
void cleanup(Post **pst) { free((*pst)->date); free((*pst)->contents); free((*pst)); *pst = NULL; } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; cleanup(&post); // If key was not present in linked list if (temp == NULL){ return 1; } remove_friends(name, user_ptr_del); prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T19:09:55.895Z
Question from Code
v1
for this code I am receiving a segmentation error, how can i fix?
void cleanup(Post **pst) { free((*pst)->date); free((*pst)->contents); free((*pst)); *pst = NULL; } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; cleanup(&post); // If key was not present in linked list if (temp == NULL){ return 1; } prev->next = temp->next; free(temp); // Free memory return 0; }
null
null
63c647bc5f004bf4898cf63f
2023-02-14T19:10:43.507Z
Help Fix Code
v1
null
void cleanup(Post **pst) { free((*pst)->date); free((*pst)->contents); free((*pst)); *pst = NULL; } int delete_user(const char *name, User **user_ptr_del) { struct user *temp = *user_ptr_del, *prev; if (find_user(name, temp) == NULL) { return 1; } if (temp != NULL && strcmp(temp->name, name) == 0) { *user_ptr_del = temp->next; // Changed head free(temp); // free old head return 0; } while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next; } Post *post = temp->first_post; cleanup(&post); // If key was not present in linked list if (temp == NULL){ return 1; } prev->next = temp->next; free(temp); // Free memory 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.
null
63c647bc5f004bf4898cf63f
2023-02-15T15:22:44.242Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { const User *curr = head; while (curr != NULL) { if (strcmp(curr->name, name) == 0) { return (User *)curr; } curr = curr->next; } return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647bc5f004bf4898cf63f
2023-03-04T00:08:03.879Z
General Question
v1
how to cast deserialized object without unchecked type error?
null
null
null
63c647bc5f004bf4898cf63f
2023-04-06T00:32:50.134Z
Question from Code
v2
how can i write a newline without creating new write()
write(client->sock_fd, users->name, strlen(users->name)
null
null
63c647c15f004bf4898cf671
2023-01-27T05:24:21.591Z
Help Fix Code
v1
null
/* 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) { // nested array -> [[elements at even indicies],[elems at odd indicies]] //int *even_array = (int *) malloc(sizeof(int) * (length/2 + length%2)); // int *even_array = malloc(sizeof(int)*(length/2 + length%2)); // int *odd_array = (int *) malloc(sizeof(int) * (length/ 2)); // int *odd_array = malloc(sizeof(int)*(length/2)); int **final_array = malloc(sizeof(int *) * 2); final_array[0] = malloc(sizeof(int)*(length/2 + length%2)); final_array[1] = malloc(sizeof(int)*(length/2)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { final_array[0][i/2] = s[i]; } else { final_array[1][i/2] = s[i]; } } //int **final_array = (int **) malloc(sizeof(int *) * 2); // int **final_array = malloc(sizeof(int *) * 2); // final_array[0] = even_array; // final_array[1] = odd_array; return final_array; } /* 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 = (int *) malloc(sizeof(int) * size); int *arr = malloc(sizeof(int *)*size); for (int i = 0; i < size; i++) { arr[i] = strtol(strs[i + 1], NULL, 10); } return arr; }
gives me errors
null
63c647c15f004bf4898cf671
2023-01-27T05:26:16.571Z
Help Fix Code
v1
null
/* 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) { // nested array -> [[elements at even indicies],[elems at odd indicies]] //int *even_array = (int *) malloc(sizeof(int) * (length/2 + length%2)); // int *even_array = malloc(sizeof(int)*(length/2 + length%2)); // int *odd_array = (int *) malloc(sizeof(int) * (length/ 2)); // int *odd_array = malloc(sizeof(int)*(length/2)); int **final_array = malloc(sizeof(int *) * 2); final_array[0] = malloc(sizeof(int)*(length/2 + length%2)); final_array[1] = malloc(sizeof(int)*(length/2)); for (int i = 0; i < length; i++) { if (i % 2 == 0) { final_array[0][i/2] = s[i]; } else { final_array[1][i/2] = s[i]; } } //int **final_array = (int **) malloc(sizeof(int *) * 2); // int **final_array = malloc(sizeof(int *) * 2); // final_array[0] = even_array; // final_array[1] = odd_array; return final_array; } /* 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 = (int *) malloc(sizeof(int) * size); int *arr = malloc(sizeof(int)*size); for (int i = 0; i < size; i++) { arr[i] = strtol(strs[i + 1], NULL, 10); } return arr; }
I get errors
null
63c647c15f004bf4898cf671
2023-01-28T02:34:53.098Z
Help Fix Code
v1
null
char season[] = 'winter'; char holiday[20] = "Valentine's Day";
Define a string literal called season that has the value "winter".
null
63c647c15f004bf4898cf671
2023-01-28T02:35:56.111Z
Help Fix Code
v1
null
char season[] = "winter"; char holiday[20] = "Valentine's Day";
Define a string literal called season that has the value "winter".
null
63c647c15f004bf4898cf671
2023-02-11T23:56:44.403Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *tail, *new_user; // if entered user already exists in this list for (tail = *user_ptr_add; tail != NULL; tail = tail->next) { if (strcmp(tail->name, name) == 0) { return 1; } } // if given name cannot fit in the 'name' array if(strlen(name) >= MAX_NAME) { return 2; } // allocating space for new user new_user = malloc(sizeof(User)); if (new_user == NULL) { perror("Space for user not allocated properly"); } // init a new user strncpy(new_user->name, name, MAX_NAME - 1); new_user->name[MAX_NAME - 1] = '\0'; new_user->first_post = NULL; for (int i = 0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } new_user->next = NULL; // inserting new user at tail of linked list if (*user_ptr_add == NULL) { *user_ptr_add = new_user; } else { tail->next = new_user; } return 0; }
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
63c647c25f004bf4898cf676
2023-02-14T21:00:25.993Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = malloc(sizeof(User)); curr_user = (User *)head; //TODO: try without typecast int user_is_null; if(curr_user == NULL){ user_is_null = 1; }else{ user_is_null = 0; } printf("%d\n", user_is_null); //traverse the list of users while(user_is_null < 1){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; if(curr_user == NULL){ user_is_null = 1; }else{ user_is_null = 0; } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T21:03:32.642Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = malloc(sizeof(User)); curr_user = (User *)head; //TODO: try without typecast int user_is_null = 0; if(curr_user == NULL){ user_is_null = 1; } printf("%d\n", user_is_null); //traverse the list of users while(user_is_null < 1){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; if(curr_user == NULL){ user_is_null = 1; }else{ user_is_null = 0; } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T21:10:29.613Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = head->next; //TODO: try without typecast if(strcmp(head->name, name) == 0){ return (User *)head; } //traverse the list of users while(curr_user != NULL){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T21:12:07.578Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = head->next; //TODO: try without typecast if(head != NULL && strcmp(head->name, name) == 0){ return (User *)head; } //traverse the list of users while(curr_user != NULL){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; } printf("gonna return NULL"); return NULL; }
The head pointer is not checked for the name. -> This is fixed by adding a check for the head pointer.
null
63c647c25f004bf4898cf676
2023-02-14T21:13:26.090Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = head->next; //TODO: try without typecast if(head != NULL && strcmp(head->name, name) == 0){ return (User *)head; } //traverse the list of users while(curr_user != NULL){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T22:00:34.671Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { if(head == NULL){ printf("got in!"); return NULL; }else{ if(strcmp(head->name, name) == 0){ return (User *)head; }else{ User *curr_user = head->next; //traverse the list of users while(curr_user){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ User *user_to_return = malloc(sizeof(User)); user_to_return = curr_user; return user_to_return; } curr_user = curr_user->next; } } } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T22:03:03.465Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { if(head == NULL){ printf("got in!"); return NULL; }else{ if(strcmp(head->name, name) == 0){ return (User *)head; }else{ User *curr_user = head->next; //traverse the list of users while(curr_user){ //check if the given name and the name of the current user are the same if(strcmp(name, curr_user->name) == 0){ return curr_user; } curr_user = curr_user->next; } } } printf("gonna return NULL"); return NULL; }
Return a pointer to the user with this name in the list starting with head. Return NULL if no such user exists.
null
63c647c25f004bf4898cf676
2023-02-14T22:21:25.946Z
General Question
v1
can i compare unsigned long and int
null
null
null
63c647c25f004bf4898cf676
2023-02-14T22:22:57.682Z
Help Fix Code
v1
null
//first check if the name is too long if((int)strlen(name) > MAX_NAME){ return 2; }
return 2 if the string name is longer than the value stored in MAX_NAME
null
63c647c25f004bf4898cf676
2023-02-15T04:15:26.978Z
General Question
v1
how do i use gdb
null
null
null
63c647c25f004bf4898cf676
2023-02-15T04:18:36.286Z
General Question
v1
how do i use valgrand
null
null
null
63c647c25f004bf4898cf676
2023-02-15T05:25:24.392Z
General Question
v1
are pointers in functions freed after the function executes
null
null
null
63c647c25f004bf4898cf676
2023-02-15T05:27:16.423Z
General Question
v1
are pointers created in functions freed after the function executes
null
null
null
63c647c25f004bf4898cf676
2023-02-15T05:27:45.212Z
General Question
v1
are pointers created in function in the heap
null
null
null
63c647c25f004bf4898cf67b
2023-02-15T14:46:36.582Z
General Question
v1
does fgets work on files that are .ascii in c
null
null
null
63c647c25f004bf4898cf67b
2023-02-15T14:47:23.198Z
General Question
v1
how do i read the contents of an ascii file and print the contents line by line in c?
null
null
null
63c647c55f004bf4898cf694
2023-01-19T04:05:18.648Z
General Question
v1
What is the return value when the input stream to scanf is closed?
null
null
null
63c647c55f004bf4898cf694
2023-01-19T04:21:24.298Z
General Question
v1
is this a valid syntax else if (0 <= temp <= 9)
null
null
null
63c647c55f004bf4898cf694
2023-01-19T04:21:55.202Z
General Question
v1
Is this a valid syntax in C else if (0 <= temp <= 9)
null
null
null
63c647c55f004bf4898cf694
2023-01-20T02:41:04.073Z
Question from Code
v1
what is being compared here?
while (scanf("%d", &input) != "EOF") {
null
null
63c647c55f004bf4898cf694
2023-01-20T02:41:35.592Z
Question from Code
v1
what is the return type of scanf
while (scanf("%d", &input) != "EOF") {
null
null
63c647c55f004bf4898cf694
2023-01-20T02:42:32.793Z
General Question
v1
in C, how to convert int to string
null
null
null
63c647c55f004bf4898cf694
2023-01-20T02:43:46.081Z
General Question
v1
how to compare an int with a string
null
null
null
63c647c55f004bf4898cf694
2023-01-20T02:44:29.557Z
General Question
v1
how would i compare the return value of scanf with a string, without compilation error
null
null
null
63c647ca5f004bf4898cf6bc
2023-01-18T01:34:35.466Z
General Question
v1
What would be an example of how to use scanf
null
null
null
63c647ca5f004bf4898cf6bc
2023-01-18T01:53:31.579Z
General Question
v1
does a c file need a main
null
null
null
63c647ca5f004bf4898cf6bc
2023-01-18T01:58:26.689Z
General Question
v1
what is stdout used for
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-10T19:48:44.430Z
General Question
v1
how to allocate space with malloc
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T04:33:38.674Z
General Question
v1
How to change a file name in C
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T04:34:23.646Z
General Question
v1
generate a detailed documentation of `rename` with usage examples and explanations
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T04:35:31.031Z
General Question
v1
generate a detailed documentation of `rename(old_name, new_name)` with usage examples and explanations
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T04:36:16.426Z
General Question
v1
How to check if a file can be opened?
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T16:27:13.543Z
General Question
v1
How to iterate through an ascii code.
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T16:28:02.820Z
General Question
v1
How to iterate through an ascii file to print out everything
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T18:03:59.830Z
General Question
v1
how to print a time_t?
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T18:06:50.897Z
General Question
v1
how to print a blank line?
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T18:42:51.839Z
General Question
v1
how to make a variable with the current time
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T19:16:17.715Z
Help Fix Code
v1
null
nt create_user(const char *name, User **user_ptr_add) { User **curr = user_ptr_add; if(strlen(name) >= MAX_NAME){ return 2; } if(user_ptr_add == NULL){ User *newbie = (User *) malloc(sizeof(User)); strcpy(newbie->name, name); user_ptr_add = newbie; // add user with name to head of list return 0; } while(curr->next != NULL){ //traverse through link list if(curr->name == name){ //check for case 1 at each node return 1; } curr = curr->next //traversalstep } //create new user at curr next curr->next = (User *) malloc(sizeof(User)); curr->next->name = name; return 0; }
fix newbie so it can be added to the User stored at user_ptr_add
null
63c647ca5f004bf4898cf6bc
2023-02-14T19:35:57.194Z
General Question
v1
how to check if a file exitsts
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T19:36:24.822Z
General Question
v1
how to check if a file can be opened
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T20:03:13.039Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; if(strlen(name) >= MAX_NAME){ return 2; } if(user_ptr_add == NULL){ User *newbie = (User *) malloc(sizeof(User)); strcpy(newbie->name, name); user_ptr_add = &newbie; // add user with name to head of list return 0; } while(curr->next != NULL){ //traverse through link list if(curr->name == name){ //check for case 1 at each node return 1; } curr = curr->next; //traversalstep } //create new user at curr next curr->next = (User *) malloc(sizeof(User)); strcpy(curr->next->name, name); return 0; }
Remove address boundary error
null
63c647ca5f004bf4898cf6bc
2023-02-14T20:07:36.898Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; if(strlen(name) >= MAX_NAME){ return 2; } if(user_ptr_add == NULL){ User *newbie = (User *) malloc(sizeof(User)); strcpy(newbie->name, name); *user_ptr_add = newbie; // add user with name to head of list return 0; } while(curr->next != NULL){ //traverse through link list if(curr->name == name){ //check for case 1 at each node return 1; } curr = curr->next; //traversalstep } //create new user at curr next curr->next = (User *) malloc(sizeof(User)); strcpy(curr->next->name, name); return 0; }
Remove address boundary error
null
63c647ca5f004bf4898cf6bc
2023-02-14T20:09:41.823Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; if(strlen(name) >= MAX_NAME){ return 2; } if(user_ptr_add == NULL){ User *newbie = (User *) malloc(sizeof(User)); strcpy(newbie->name, name); *user_ptr_add = newbie; // add user with name to head of list return 0; } while(curr->next != NULL){ //traverse through link list if(strcmp(curr->name, name)){ //check for case 1 at each node return 1; } curr = curr->next; //traversalstep } //create new user at curr next curr->next = (User *) malloc(sizeof(User)); strcpy(curr->next->name, name); return 0; }
Remove address boundary error
null
63c647ca5f004bf4898cf6bc
2023-02-14T21:30:57.675Z
Question from Code
v1
is this code modifying the *user stored at *user_ptr_add
int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; if(strlen(name) >= MAX_NAME){ return 2; } if(user_ptr_add == NULL){ User *newbie = (User *) malloc(sizeof(User)); strcpy(newbie->name, name); *user_ptr_add = newbie; // add user with name to head of list return 0; } while(curr != NULL){ //traverse through link list if(strcmp(curr->name, name)){ //check for case 1 at each node return 1; } curr = curr->next; //traversalstep } //create new user at curr curr = (User *) malloc(sizeof(User)); strcpy(curr->name, name); for(int i = 0; i<10; i++){ curr->friends[i] = NULL; } strcpy(curr->profile_pic, ""); curr->first_post = NULL; curr->next = NULL; return 0; }
null
null
63c647ca5f004bf4898cf6bc
2023-02-14T21:32:29.420Z
Question from Code
v1
Why is curr always null?
void list_users(const User *curr) { printf("User List\n"); while(curr != NULL){ printf(" %s\n", curr->name); curr = curr->next; } }
null
null
63c647ca5f004bf4898cf6bc
2023-02-15T01:21:17.424Z
Help Fix Code
v1
null
int delete_user(const char *name, User **user_ptr_del) { User *leaver = find_user(name, *user_ptr_del); User *curr = *user_ptr_del; if(leaver == NULL || curr == NULL){ return 1; } if(curr == leaver){ *user_ptr_del = curr->next; } for(int i = 0; i < 10; i++){ if(curr->next->friends[i]==leaver){ curr->next->friends[i] = NULL; } } while(curr->next != NULL){ if(curr->next == leaver){ User *a = curr->next; free(a); curr->next = curr->next->next; } else{ for(int i = 0; i < 10; i++){ if(curr->next->friends[i]==leaver){ curr->next->friends[i] = NULL; } } } curr = curr->next; } return 1; }
fix segmentation fault
null
63c647ca5f004bf4898cf6bc
2023-02-15T01:22:49.678Z
General Question
v1
how do you remove an element from an array inside a struct
null
null
null
63c647ca5f004bf4898cf6bc
2023-02-15T01:48:27.702Z
General Question
v1
how to make an array of the same type as: struct user *friends[MAX_FRIENDS];
null
null
null