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
63c647635f004bf4898cf351
2023-01-18T04:30:17.303Z
Question from Code
v1
assign the amount in rain_today to the space rainfall points to
int main(int argc, char **argv) { float *rainfall; float rain_today; }
null
null
63c647635f004bf4898cf351
2023-02-07T21:51:18.185Z
Question from Code
v1
Why am I getting segmentation fault
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } User *curr = *user_ptr_add; while (curr->next != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; } User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); (new_user->name)[MAX_NAME - 1] = '\0'; curr->next = new_user; new_user->next = NULL; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-07T22:17:08.664Z
Question from Code
v1
strcmp is not working
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } // make new user User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); (new_user->name)[MAX_NAME - 1] = '\0'; User *curr = *user_ptr_add; if (curr == NULL) { curr = new_user; new_user->next = NULL; return 0; } while (curr->next != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; } curr->next = new_user; new_user = NULL; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-07T22:17:57.317Z
Question from Code
v1
is curr->name a string
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } // make new user User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); (new_user->name)[MAX_NAME - 1] = '\0'; User *curr = *user_ptr_add; if (curr == NULL) { curr = new_user; new_user->next = NULL; return 0; } while (curr->next != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; } curr->next = new_user; new_user = NULL; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-07T22:18:59.859Z
Question from Code
v1
how do I know whether name and curr->name are the same
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } // make new user User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); (new_user->name)[MAX_NAME - 1] = '\0'; User *curr = *user_ptr_add; if (curr == NULL) { curr = new_user; new_user->next = NULL; return 0; } while (curr->next != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; } curr->next = new_user; new_user = NULL; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-07T22:22:40.387Z
Question from Code
v1
what does strcmp(name, curr->name) do
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME) { return 2; } // make new user User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, sizeof(MAX_NAME - 1)); (new_user->name)[MAX_NAME - 1] = '\0'; User *curr = *user_ptr_add; if (curr == NULL) { curr = new_user; new_user->next = NULL; return 0; } while (curr->next != NULL) { if (strcmp(name, curr->name) == 0) { return 1; } curr = curr->next; } curr->next = new_user; new_user = NULL; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-08T04:53:26.759Z
Question from Code
v1
why do I get error with new_post.author = author->name
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } struct post new_post; new_post.author = author->name; new_post.contents = contents; time_t curtime; new_post.date = &curtime; new_post.next = target->first_post->next; target->first_post = &new_post; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-08T04:57:44.821Z
Question from Code
v1
how to fix error for new_post->author = author->name
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } Post *new_post = malloc(sizeof(Post)); new_post->author = author->name; new_post->contents = contents; time_t curtime; new_post->date = &curtime; new_post->next = target->first_post->next; target->first_post = new_post; return 0; }
null
null
63c647635f004bf4898cf351
2023-02-08T05:05:09.640Z
Question from Code
v1
why am i getting segmentation fault
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } Post *new_post = malloc(sizeof(Post)); strcpy(new_post->author, author->name); new_post->contents = contents; time_t curtime; new_post->date = &curtime; new_post->next = target->first_post->next; target->first_post = new_post; return 0; } int check_friends(User *author, User *target) { int friends = 1; int i = 0; while (i < MAX_FRIENDS) { if (author->friends[i] == NULL) { break; } if (strcmp(author->friends[i]->name, target->name) == 0) { friends = 0; } i++; } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-08T05:07:14.885Z
Question from Code
v1
why am I getting segmentation fault
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } Post *new_post = malloc(sizeof(Post)); strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); time_t curtime; new_post->date = &curtime; new_post->next = target->first_post->next; target->first_post = new_post; return 0; } int check_friends(User *author, User *target) { int friends = 1; int i = 0; while (i < MAX_FRIENDS) { if (author->friends[i] == NULL) { break; } if (strcmp(author->friends[i]->name, target->name) == 0) { friends = 0; } i++; } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-08T05:11:57.321Z
Question from Code
v1
how to fix error in ew_post->author = malloc(sizeof(char) * MAX_NAME);
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } Post *new_post = malloc(sizeof(Post)); new_post->author = malloc(sizeof(char) * MAX_NAME); strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); time_t curtime; new_post->date = &curtime; new_post->next = target->first_post->next; target->first_post = new_post; return 0; } int check_friends(User *author, User *target) { int friends = 1; int i = 0; while (i < MAX_FRIENDS) { if (author->friends[i] == NULL) { break; } if (strcmp(author->friends[i]->name, target->name) == 0) { friends = 0; } i++; } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-08T05:13:45.509Z
Question from Code
v1
error in new_post->author = malloc(MAX_NAME * sizeof(char));
int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } if (check_friends((User *)author, target) == 1) { return 1; } Post *new_post = malloc(sizeof(Post)); new_post->author = malloc(MAX_NAME * sizeof(char)); strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); time_t curtime; new_post->date = &curtime; new_post->next = target->first_post->next; target->first_post = new_post; return 0; } int check_friends(User *author, User *target) { int friends = 1; int i = 0; while (i < MAX_FRIENDS) { if (author->friends[i] == NULL) { break; } if (strcmp(author->friends[i]->name, target->name) == 0) { friends = 0; } i++; } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-12T00:50:28.733Z
Question from Code
v1
does this work
int check_friends(User *user1, User *user2) { int friends = 1; for (int i = 0; i < MAX_FRIENDS; i++) { if (strcmp((user1->friends[i])->name, user2->name) == 0) { friends = 0; break; } } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-12T00:51:08.355Z
Question from Code
v1
why am i getting segmentation fault
int check_friends(User *user1, User *user2) { int friends = 1; for (int i = 0; i < MAX_FRIENDS; i++) { if (strcmp((user1->friends[i])->name, user2->name) == 0) { friends = 0; break; } } return friends; }
null
null
63c647635f004bf4898cf351
2023-02-13T01:44:42.104Z
Question from Code
v1
why do I have unitialised values
void delete_from_friends(User *user) { for (int i = 0; i < MAX_FRIENDS; i++) { if (user->friends[i] == NULL) {} else { for (int j = 0; j < MAX_FRIENDS; j++) { User *friends_friend = user->friends[i]->friends[j]; if (friends_friend == NULL) {} else if (strcmp(friends_friend->name, user->name) == 0) { user->friends[i]->friends[j] = NULL; break; } } } } }
null
null
63c647635f004bf4898cf351
2023-02-13T01:47:04.905Z
Question from Code
v1
why do i have an error with if (friends_friend == NULL) {}
void delete_from_friends(User *user) { for (int i = 0; i < MAX_FRIENDS; i++) { if (user->friends[i] == NULL) {} else { for (int j = 0; j < MAX_FRIENDS; j++) { User *friends_friend = user->friends[i]->friends[j]; if (friends_friend == NULL) {} else if (strcmp(friends_friend->name, user->name) == 0) { user->friends[i]->friends[j] = NULL; break; } } } } }
null
null
63c647635f004bf4898cf351
2023-02-22T00:43:09.546Z
Question from Code
v1
Is there a segmentation fault in this
Rule *parse_file(FILE *fp) { // Implement this function and remove the stubbed return statement below. Rule *first_rule_ptr = NULL; Rule *last_rule_ptr = NULL; char str[MAXLINE]; while (fgets(str, MAXLINE, fp) != NULL) { if (str[0] == ' ' || str[0] == '\t' || str[0] == '\n') {} else if (is_comment_or_empty(str)) {} else if (is_target_line(str)) { Rule *new_rule = init_rule(str); last_rule_ptr = new_rule; if (first_rule_ptr == NULL) {first_rule_ptr = new_rule;} else {insert_rule(&first_rule_ptr, &new_rule);} } else { // is action line add_actions(&last_rule_ptr, str); } } fseek(fp, 0, SEEK_SET); while (fgets(str, MAXLINE, fp) != NULL) { if (is_target_line(str)) { add_dependencies(&first_rule_ptr, str); } } return first_rule_ptr; }
null
null
63c647635f004bf4898cf351
2023-02-22T00:44:31.528Z
Question from Code
v1
Is there a segmentation fault in this
Rule *init_rule(char *line) { Rule *new_rule = malloc(sizeof(Rule)); new_rule->dependencies = NULL; new_rule->actions = NULL; new_rule->next_rule = NULL; char *ptr = strtok(line, " :"); new_rule->target = malloc((sizeof(char) * strlen(ptr)) + 1); strcpy(new_rule->target, ptr); new_rule->target[strlen(ptr)] = '\0'; return new_rule; }
null
null
63c647635f004bf4898cf351
2023-02-22T00:45:42.588Z
Question from Code
v1
Is there a segmentation fault in this
void add_dependencies(Rule **rules_head, char *line) { char *ptr = strtok((char *)line, " :"); // target name Rule *curr = *rules_head; while (curr->next_rule != NULL) { if (strcmp(curr->target, ptr) == 0) {break;} else {curr = curr->next_rule;} } ptr = strtok(NULL, " "); while (ptr != NULL) { Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = find_rule(&curr, ptr); if (curr->dependencies == NULL) { curr->dependencies = new_dep; new_dep->next_dep = NULL; } else { new_dep->next_dep = curr->dependencies; curr->dependencies = new_dep; } ptr = strtok(NULL, " "); } }
null
null
63c647635f004bf4898cf351
2023-02-22T00:49:50.319Z
Question from Code
v1
Is there a segmentation fault in this
Rule *find_rule(Rule **rules_ptr, char *name) { Rule *curr = *rules_ptr; while(curr->next_rule != NULL) { if (strcmp(curr->target, name) == 0) { break; } curr = curr->next_rule; } if (strcmp(curr->target, name) != 0) { // rule doesn't exist curr->next_rule = init_rule(name); curr = curr->next_rule; } return curr; }
null
null
63c647635f004bf4898cf351
2023-02-22T00:57:51.378Z
Question from Code
v1
Where are the segmentation faults
int is_target_line(const char *line); Rule *init_rule(char *line); void insert_rule(Rule **rules_head, Rule **new_rule); void add_actions(Rule **last_rule_ptr, char *line); void add_dependencies(Rule **rules_head, char *line); Rule *find_rule(Rule **rules_ptr, char *name); /* 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) { // Implement this function and remove the stubbed return statement below. Rule *first_rule_ptr = NULL; Rule *last_rule_ptr = NULL; char str[MAXLINE]; while (fgets(str, MAXLINE, fp) != NULL) { if (str[0] == ' ' || str[0] == '\t' || str[0] == '\n') {} else if (is_comment_or_empty(str)) {} else if (is_target_line(str)) { Rule *new_rule = init_rule(str); last_rule_ptr = new_rule; if (first_rule_ptr == NULL) {first_rule_ptr = new_rule;} else {insert_rule(&first_rule_ptr, &new_rule);} } else { // is action line add_actions(&last_rule_ptr, str); } } fseek(fp, 0, SEEK_SET); while (fgets(str, MAXLINE, fp) != NULL) { if (is_target_line(str)) { add_dependencies(&first_rule_ptr, str); } } return first_rule_ptr; } /* Return 1 if the line is a target line, as defined on the assignment handout. * Return 0 otherwise. */ int is_target_line(const char *line) { if (strstr(line, " : ") != NULL) { return 1; } return 0; } /* Initialize a new rule node with the specified target name * from line (in target line format). Initialize everything else to NULL. * Return a pointer to this new node. */ Rule *init_rule(char *line) { Rule *new_rule = malloc(sizeof(Rule)); new_rule->dependencies = NULL; new_rule->actions = NULL; new_rule->next_rule = NULL; new_rule->target = NULL; char *ptr = strtok(line, " :"); new_rule->target = malloc((sizeof(char) * strlen(ptr)) + 1); strcpy(new_rule->target, ptr); new_rule->target[strlen(ptr)] = '\0'; return new_rule; } /* Add dependencies to target from line (in target line format). * Point each dependency to its corresponding rule node in the * rules list pointed to by rules_head. */ void add_dependencies(Rule **rules_head, char *line) { char *ptr = strtok((char *)line, " :"); // target name Rule *curr = *rules_head; while (curr->next_rule != NULL) { if (strcmp(curr->target, ptr) == 0) {break;} else {curr = curr->next_rule;} } ptr = strtok(NULL, " "); while (ptr != NULL) { Dependency *new_dep = malloc(sizeof(Dependency)); new_dep->rule = NULL; new_dep->next_dep = NULL; new_dep->rule = find_rule(&curr, ptr); if (curr->dependencies == NULL) { curr->dependencies = new_dep; new_dep->next_dep = NULL; } else { new_dep->next_dep = curr->dependencies; curr->dependencies = new_dep; } ptr = strtok(NULL, " "); } } /* Return the rule node with the given target name * in the rules list pointed to by rules_ptr. * If no such rule exists, create it and return it. */ Rule *find_rule(Rule **rules_ptr, char *name) { Rule *curr = *rules_ptr; while(curr->next_rule != NULL) { if (strcmp(curr->target, name) == 0) { break; } curr = curr->next_rule; } if (strcmp(curr->target, name) != 0) { // rule doesn't exist curr->next_rule = init_rule(name); curr = curr->next_rule; } return curr; } /* Insert the new rule to the tail of the rules list pointed to * by rules_head. * * Precondition: rules list pointed to by rules_head is not empty */ void insert_rule(Rule **rules_head, Rule **new_rule) { Rule *curr = *rules_head; while (curr->next_rule != NULL) { curr = curr->next_rule; } curr->next_rule = *new_rule; } /* Add actions in line (in action line format) to * the rule node pointed to by last_rule_ptr */ void add_actions(Rule **last_rule_ptr, char *line) {}
null
null
63c647645f004bf4898cf35b
2023-01-25T21:27:31.768Z
Explain Code
v1
null
void fib(int **fib_sequence, int count){ *fib_sequence = malloc(sizeof(int)*count); if (count == 1) { *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; for (int i = 2; i < count; i++) { *fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1]; } } }
null
null
63c647645f004bf4898cf35b
2023-01-25T21:28:35.214Z
General Question
v1
why do I get a segmentation fault
null
null
null
63c647645f004bf4898cf35b
2023-01-25T21:30:17.991Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ *fib_sequence = malloc(sizeof(int)*count); if (count == 1) { *fib_sequence[0] = 0; } else { *fib_sequence[0] = 0; *fib_sequence[1] = 1; for (int i = 2; i < count; i++) { *fib_sequence[i] = *fib_sequence[i-2] + *fib_sequence[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-25T21:36:05.238Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ int *cur = *fib_sequence cur = malloc(sizeof(int)*count); if (count == 1) { cur[0] = 0; } else { cur[0] = 0; cur[1] = 1; for (int i = 2; i < count; i++) { cur[i] = cur[i-2] + cur[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-25T21:37:03.455Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ int *cur = *fib_sequence cur = malloc(sizeof(int)*count); if (count == 1) { *cur[0] = 0; } else { *cur[0] = 0; *cur[1] = 1; for (int i = 2; i < count; i++) { *cur[i] = *cur[i-2] + *cur[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-25T21:38:00.702Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ int *cur = *fib_sequence cur = malloc(sizeof(int)*count); if (count == 1) { cur[0] = 0; } else { cur[0] = 0; cur[1] = 1; for (int i = 2; i < count; i++) { cur[i] = *cur[i-2] + *cur[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-25T21:39:23.197Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ int *cur = *fib_sequence *cur = malloc(sizeof(int)*count); if (count == 1) { cur[0] = 0; } else { cur[0] = 0; cur[1] = 1; for (int i = 2; i < count; i++) { cur[i] = cur[i-2] + cur[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-25T21:40:29.438Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ int *cur = **fib_sequence cur = malloc(sizeof(int)*count); if (count == 1) { cur[0] = 0; } else { cur[0] = 0; cur[1] = 1; for (int i = 2; i < count; i++) { cur[i] = cur[i-2] + cur[i-1]; } } }
generate the first n values in the Fibonacci sequence
null
63c647645f004bf4898cf35b
2023-01-30T21:59:11.935Z
Help Fix Code
v1
null
int populate_array(int sin, int *sin_array) { for (int i = 8; i >= 0; i--){ sin_array[i] = sin % 10; sin = sin / 10; } if (sin != 0) { return 1; } else { return 0; } }
It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer.
null
63c647645f004bf4898cf35b
2023-01-30T22:00:26.564Z
Help Fix Code
v1
null
int populate_array(int sin, int *sin_array) { for (int i = 8; i >= 0; i--){ sin_array[i] = sin % 10; sin = sin / 10; } if (sin == 0) { return 0; } else { return 1; } }
It takes an integer and an integer array as its arguments, and returns an integer. This function's job is to populate the given integer array so that it contains the 9 digits of the given integer, in the same order as in the integer.
null
63c647645f004bf4898cf35b
2023-01-30T22:19:32.496Z
Help Fix Code
v1
null
int check_sin(int *sin_array) { int sin_total = 0; for (int i = 0; i < 9; i++){ if (i % 2 == 0){ int result = sin_array[i] * 2; //multiplies every second digit by 2 if (result >= 10) { sin_total += 1 + (result % 10); } else { sin_total += result; } } else { sin_total += sin_array[i]; } } if (sin_total % 10 == 0){ return 0; } else { return 1; } }
it takes an 9 element integer array representing a candidate SIN number. It returns 0 if the number given is a valid Canadian SIN number, and 1 otherwise.
null
63c647645f004bf4898cf35b
2023-02-10T21:56:14.388Z
Explain Code
v1
null
User *find_user(const char *name, const User *head) { User *curr_user = (User *) head; while (curr_user != NULL) { if (strcmp(curr_user->name, name) == 0) { return curr_user; } } return NULL; }
null
null
63c647645f004bf4898cf35b
2023-02-10T22:09:34.049Z
Explain Code
v1
null
void list_users(const User *curr) { User *curr_user = (User *) curr; while (curr_user != NULL) { printf("%s\n", curr_user->name); curr_user = curr_user->next; } }
null
null
63c647645f004bf4898cf35b
2023-02-14T19:06:00.310Z
General Question
v1
how to determine the number of elements in an array
null
null
null
63c647645f004bf4898cf35b
2023-02-15T04:49:52.485Z
General Question
v1
Do we have to assign values to all attributes of a struct when creating a new instance of it
null
null
null
63c647645f004bf4898cf35b
2023-02-15T21:10:37.196Z
General Question
v1
when allocating space for a next pointer in a linked list struct what is the size of the allocation
null
null
null
63c647645f004bf4898cf35b
2023-02-17T04:06:47.317Z
Explain Code
v1
null
char *copy(char *dest, const char *src, int capacity) { int null_count = 0; for (int i = 0; i < capacity; i++) { if (src[i] == '\0') { // When src is less than or equal to capacity to be copied null_count += (capacity - i); break; } } if (null_count != 0) { for (int j = 0; j < capacity - null_count; j++) { dest[j] = src[j]; } for (int k = capacity - null_count; k < capacity; k++) { dest[k] = '\0'; } } else { for (int j = 0; j < capacity - 1; j++) { dest[j] = src[j]; } dest[capacity - 1] = '\0'; } for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; for ( ; i < capacity; i++) { dest[i] = '\0'; } } return dest; }
null
null
63c647645f004bf4898cf35b
2023-02-17T04:09:24.340Z
Explain Code
v1
null
char *copy(char *dest, const char *src, int capacity) { for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; for ( ; i < capacity; i++) { dest[i] = '\0'; } } return dest; }
null
null
63c647645f004bf4898cf35b
2023-02-17T04:10:50.648Z
Explain Code
v1
null
char *copy(char *dest, const char *src, int capacity) { int null_count = 0; for (int i = 0; i < capacity; i++) { if (src[i] == '\0') { // When src is less than or equal to capacity to be copied null_count += (capacity - i); break; } } if (null_count != 0) { for (int j = 0; j < capacity - null_count; j++) { dest[j] = src[j]; } for (int k = capacity - null_count; k < capacity; k++) { dest[k] = '\0'; } } else { for (int j = 0; j < capacity - 1; j++) { dest[j] = src[j]; } dest[capacity - 1] = '\0'; } }
null
null
63c647645f004bf4898cf35b
2023-02-28T04:01:01.804Z
General Question
v1
is the sizeof(int**) equal to sizeof(int*)
null
null
null
63c647645f004bf4898cf35b
2023-03-08T18:27:19.096Z
Help Write Code
v1
null
null
null
Get parent process to wait for all child process to complete
63c647645f004bf4898cf35b
2023-04-02T19:38:32.471Z
General Question
v2
when a message with newlines is sent to a client from a server should the message end with one network newline or does the network newline replace all newline characters in the message
null
null
null
63c647645f004bf4898cf35b
2023-04-06T17:22:07.297Z
General Question
v2
When to use memset vs when to malloc
null
null
null
63c647645f004bf4898cf35b
2023-04-06T20:39:25.321Z
General Question
v2
how to copy a char array to a char pointer
null
null
null
63c647645f004bf4898cf35b
2023-04-06T23:45:58.815Z
General Question
v2
how to use select
null
null
null
63c647645f004bf4898cf35b
2023-04-24T02:38:51.049Z
Help Fix Code
v2
print list
int *make_list() { int list[3] = {1, 2, 3}; return list; } int main() { int *l = make_list(); int i; for (i = 0; i < 3; i++) { printf("%d\n", l[i]); } return 0; }
null
null
63c647645f004bf4898cf35b
2023-04-24T02:48:05.134Z
General Question
v2
does initializing a char array include the null terminator
null
null
null
63c647645f004bf4898cf35b
2023-04-24T02:51:51.169Z
Help Fix Code
v2
store literal dynamically
char *word = malloc(6 * sizeof(char)); *word = "hello";
null
null
63c647645f004bf4898cf35b
2023-04-24T17:54:28.518Z
General Question
v2
can strcpy alter string literal
null
null
null
63c647645f004bf4898cf35b
2023-04-24T20:11:20.699Z
General Question
v2
how to guarantee order of output for child processes when forking
null
null
null
63c647645f004bf4898cf35b
2023-04-25T03:31:11.151Z
Explain Code
v2
null
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h> int main(int argc, char **argv) { int ret; int sum = 0; int status; for (int i = 1; i < argc; i++) { ret = fork(); if (ret < 0) { perror("fork"); exit(1); } else if (ret == 0) { int len = strlen(argv[i]); printf("I am child %d and I'm returning %d\n", i, len); exit(len); } } for (int i = 1; i < argc; i++) { if ((ret = wait(&status)) == -1) { perror("wait"); exit(1); } if (WIFEXITED(status)) { sum += WEXITSTATUS(status); } } printf("The length of all the args is %d\n", sum); return 0; }
null
null
63c647645f004bf4898cf35b
2023-04-25T03:35:19.295Z
General Question
v2
what is dup2 used for
null
null
null
63c647645f004bf4898cf35b
2023-04-25T21:39:20.796Z
Help Fix Code
v2
print hello world
char *s = "hello world"; char *t = malloc(sizeof(s)); strcpy(t, s); printf("%s", t);
null
null
63c647645f004bf4898cf35b
2023-04-25T21:42:09.850Z
Question from Code
v2
what happens if we use sizeof(s) in malloc instead of strlen(s) + 1
char *s = "hello world"; char *t = malloc(sizeof(s)); strcpy(t, s); printf("%s", t);
null
null
63c647665f004bf4898cf36f
2023-03-02T03:02:51.468Z
Question from Code
v1
modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647665f004bf4898cf36f
2023-03-02T03:04:17.403Z
Help Fix Code
v1
null
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
modify the program so that the new children do not create additional processes. Only the original parent calls fork. Keep the printf call for all processes.
null
63c647665f004bf4898cf36f
2023-03-02T03:06:20.878Z
Question from Code
v1
modify the program so that the new children do not create additional processes. Only the original parent calls fork ITERATIONS times. Keep the printf call for all processes.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); for (int i = 0; i < iterations; i++) { int n = fork(); if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647665f004bf4898cf36f
2023-03-14T01:27:57.390Z
General Question
v2
how to fork children processes in parallel and make the parent wait for all the childern
null
null
null
63c647665f004bf4898cf36f
2023-03-14T01:30:03.763Z
Help Write Code
v2
null
null
null
fork 5 child proccess in parallel and make the parent process wait until all child processes have terminated
63c647665f004bf4898cf36f
2023-03-14T02:21:51.085Z
General Question
v2
how to fork childern sequentially and not in parallel
null
null
null
63c647665f004bf4898cf36f
2023-03-14T04:24:42.482Z
General Question
v2
execvp: No such file or directory why am i getting this error
null
null
null
63c647665f004bf4898cf374
2023-01-22T06:21:22.141Z
Question from Code
v1
Why does this compiling and executing this result in a segmentation fault?
int main(int argc, char **argv) { // TODO: Verify that command line arguments are valid. if (argc != 2) { return 2; //returning with code 2 } // TODO: Parse arguments and then call the two helpers in sin_helpers.c // to verify the SIN given as a command line argument. int sin_number = strtol(argv[1], NULL, 10); int sin_array[9]; if (populate_array(sin_number, sin_array) == 0) { //the parsed argument was a 9 digit integer if (check_sin(sin_array) == 0) { //in addition, if valid sin number then printf("Valid SIN\n"); return 0; } else { printf("Invalid SIN\n"); return 1; } } return 0; }
null
null
63c647665f004bf4898cf374
2023-01-22T06:23:39.600Z
Question from Code
v1
Does calling this function with appropriate parameters result in a segmentation fault?
int populate_array(int sin, int *sin_array) { //setting up dummy variables to test whether sin is a 9 digit in int dummy = sin; int count = 0; while (dummy != 0) { dummy = dummy / 10; count = count + 1; } if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int return 1; } else{ for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array sin_array[i] = sin % 10; sin = sin / 10; } return 0; } }
null
null
63c647665f004bf4898cf374
2023-01-22T06:36:16.925Z
Question from Code
v1
Will executing main result in a segmentation fault?
#include <stdio.h> int check_sin(int *sin_array) { if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid return 0; } else { int result[9]; int sum = 0; for (int i = 0; i < 9; i++) { if (i % 2 == 0) { //even indices get multiplied by 1 result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i } else { //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum //are the added digits } } for (int i = 0; i < 9; i++) { sum = sum + result[i]; } if (sum % 10 == 0) { return 0; } return 1; } } int main() { int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; printf("%d/n", check_sin(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:39:17.378Z
Question from Code
v1
Does executing main result in a segmentation fault?
#include <stdio.h> int populate_array(int sin, int *sin_array) { //setting up dummy variables to test whether sin is a 9 digit in int dummy = sin; int count = 0; while (dummy != 0) { dummy = dummy / 10; count = count + 1; } if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int return 1; } else{ for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array sin_array[i] = sin % 10; sin = sin / 10; } return 0; } } int main() { int sin_array[9]; printf("%d/n", check_sin(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:40:04.712Z
Question from Code
v1
Does executing this code result in a segmentation fault?
#include <stdio.h> int populate_array(int sin, int *sin_array) { //setting up dummy variables to test whether sin is a 9 digit in int dummy = sin; int count = 0; while (dummy != 0) { dummy = dummy / 10; count = count + 1; } if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int return 1; } else{ for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array sin_array[i] = sin % 10; sin = sin / 10; } return 0; } } int main() { int sin_array[9]; printf("%d/n", populate_array(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:41:09.042Z
Question from Code
v1
Does executing this code result in a segmentation fault?
#include <stdio.h> int populate_array(int sin, int *sin_array) { //setting up dummy variables to test whether sin is a 9 digit in int dummy = sin; int count = 0; while (dummy != 0) { dummy = dummy / 10; count = count + 1; } if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int return 1; } else{ for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array sin_array[i] = sin % 10; sin = sin / 10; } return 0; } } int main() { int sin_array[9]; printf("%d/n", populate_array(810620716, sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:51:03.766Z
Question from Code
v1
Why does executing this produce a segmentation fault?
#include <stdio.h> int populate_array(int sin, int *sin_array) { //setting up dummy variables to test whether sin is a 9 digit in int dummy = sin; int count = 0; while (dummy != 0) { dummy = dummy / 10; count = count + 1; } if (count != 9) { //10 didn't divide into sin exactly 9 times, thus the number is not a 9 digit int return 1; } else{ for (int i = 8; i >= 0; i--) { //sin is a 9 digit int, so populate array sin_array[i] = sin % 10; sin = sin / 10; } return 0; } } int main() { int sin_array[9]; printf("%d/n", check_sin(sin_array)); } Yes, because the populate_array function is not called. Will executing main result in a segmentation fault? #include <stdio.h> int check_sin(int *sin_array) { if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid return 0; } else { int result[9]; int sum = 0; for (int i = 0; i < 9; i++) { if (i % 2 == 0) { //even indices get multiplied by 1 result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i } else { //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum //are the added digits } } for (int i = 0; i < 9; i++) { sum = sum + result[i]; } if (sum % 10 == 0) { return 0; } return 1; } } int main() { int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; printf("%d/n", check_sin(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:52:22.825Z
Question from Code
v1
Why does running this produce a segmentation fault?
#include <stdio.h> int check_sin(int *sin_array) { if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid return 0; } else { int result[9]; int sum = 0; for (int i = 0; i < 9; i++) { if (i % 2 == 0) { //even indices get multiplied by 1 result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i } else { //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum //are the added digits } } for (int i = 0; i < 9; i++) { sum = sum + result[i]; } if (sum % 10 == 0) { return 0; } return 1; } } int main() { int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; printf("%d/n", check_sin(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-01-22T06:54:07.434Z
Help Fix Code
v1
null
#include <stdio.h> int check_sin(int *sin_array) { if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid return 0; } else { int result[9]; int sum = 0; for (int i = 0; i < 9; i++) { if (i % 2 == 0) { //even indices get multiplied by 1 result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i } else { //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum //are the added digits } } for (int i = 0; i < 9; i++) { sum = sum + result[i]; } if (sum % 10 == 0) { return 0; } return 1; } } int main() { int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; printf("%d/n", check_sin(sin_array)); }
Outputs 1
null
63c647665f004bf4898cf374
2023-01-22T06:56:43.358Z
Question from Code
v1
Why does running this result in a segmentation fault?
#include <stdio.h> int check_sin(int *sin_array) { if (sin_array[0] == 0) { //assignment simplification: if it starts with 0 its invalid return 1; } else { int result[9]; int sum = 0; for (int i = 0; i < 9; i++) { if (i % 2 == 0) { //even indices get multiplied by 1 result[i] = sin_array[i]; //the result at i is just going to be the digit since i * 1 = i } else { //odd indices get multiplied by 2, and if its a 2 digit number than add the 2 digits and output it to result[i] result[i] = ((sin_array[i] * 2) / 10) + ((sin_array[i] * 2) % 10); //if the product is < 10, the division is 0 and the modulo will be the digit, so the sum is just the digit //if the product is >= 10, the division is the first digit of the product, the modulo is the second, so the sum //are the added digits } } for (int i = 0; i < 9; i++) { sum = sum + result[i]; } if (sum % 10 == 0) { return 0; } return 1; } } int main() { int sin_array[9] = {8, 1, 0, 6, 2, 0, 7, 1, 6}; printf("%d/n", check_sin(sin_array)); }
null
null
63c647665f004bf4898cf374
2023-04-23T00:17:23.387Z
Question from Code
v2
Would the lines of code compile and execute without error always?
int x; int *p = &x; printf("%d\n", *p);
null
null
63c647665f004bf4898cf374
2023-04-23T00:19:52.704Z
Question from Code
v2
Would the code compile and execute without error all the time?
char arr[11] = "Basket"; strcat(arr, "ball"); printf("%s\n", arr);
null
null
63c647665f004bf4898cf374
2023-04-23T00:28:26.919Z
Question from Code
v2
Would this code always compile and execute without error?
char str[] = "CSC209"; char *s = "CSC209S"; s[6] = '\0'; printf("%d\n", strcmp(str, s));
null
null
63c647665f004bf4898cf374
2023-04-23T00:50:03.928Z
Question from Code
v2
Does this code always compile and run without error
char *get_extension(char •str) { char ext[4]; char *p; if ((p = strchr(str, '.')) != NULL) { strncpy(ext, p, 4); ext[3] = '\0'; return ext; } else { return NULL; } } printf("%s\n", get_extension("run.c"));
null
null
63c647665f004bf4898cf374
2023-04-23T00:51:58.020Z
Question from Code
v2
Does running this code cause any errors such as segmentation fault
char *get_extension(char *str) { char ext[4]; char *p; if ((p = strchr(str, '.')) != NULL) { strncpy(ext, p, 4); ext[3] = '\0'; return ext; } else { return NULL; } } printf("%s\n", get_extension("run.c"));
null
null
63c647675f004bf4898cf37e
2023-02-14T19:02:19.184Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { // Checks if the name fits into the array. if (strlen(name) >= MAX_NAME) { return 2; } else ( User *curr = *user_ptr_add; User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, curr->name) == 0) { curr = curr -> next; next = curr -> next; } else { return 1; } } // Creates the new user and adds it to the list. User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); curr->next = new_user; new_user->next = NULL; ) 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) */
null
63c647675f004bf4898cf37e
2023-02-14T19:04:12.190Z
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) { curr = curr -> next; } else { return &curr; } } return 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. */
null
63c647675f004bf4898cf37e
2023-02-14T19:08:05.009Z
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) { curr = curr -> next; } else { return curr; } } return 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. */
null
63c647675f004bf4898cf37e
2023-02-14T23:06:09.413Z
Question from Code
v1
Why is my code giving a segmentation fault?
#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; /* * 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) { // Checks if the name fits into the array. if (strlen(name) >= MAX_NAME) { return 2; } else { User *curr = *user_ptr_add; User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = curr -> next; next = curr -> next; } else { return 1; } } // Creates the new user and adds it to the list. User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); strcpy(new_user->profile_pic, ""); curr->next = new_user; new_user->next = NULL; return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-14T23:07:27.395Z
Question from Code
v1
why is this giving me a segmentation fault
#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; /* * 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) { // Checks if the name fits into the array. if (strlen(name) >= MAX_NAME) { return 2; } else { User *curr = *user_ptr_add; User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = curr -> next; next = curr -> next; } else { return 1; } } // Creates the new user and adds it to the list. User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); curr->next = new_user; new_user->next = NULL; return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-14T23:14:30.147Z
Explain 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; /* * 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) { // Checks if the name fits into the array. if (strlen(name) >= MAX_NAME) { return 2; } else { User *curr = *user_ptr_add; User *next = curr -> next; // Creates the new user and adds it to the list. User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); if (curr == NULL) { curr = new_user; new_user->next = NULL; } else if (strcmp(name, curr->name) == 0) { return 1; } // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = curr -> next; next = curr -> next; } else { return 1; } } curr->next = new_user; new_user->next = NULL; return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-14T23:15:04.222Z
Question from Code
v1
why is this giving me a segmentation fault?
#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; /* * 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) { // Checks if the name fits into the array. if (strlen(name) >= MAX_NAME) { return 2; } else { User *curr = *user_ptr_add; User *next = curr -> next; // Creates the new user and adds it to the list. User *new_user = malloc(sizeof(User)); strcpy(new_user -> name, name); if (curr == NULL) { curr = new_user; new_user->next = NULL; } else if (strcmp(name, curr->name) == 0) { return 1; } // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = curr -> next; next = curr -> next; } else { return 1; } } curr->next = new_user; new_user->next = NULL; return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-15T09:08:50.730Z
Question from Code
v1
WHY AM I GETTING A SEGMENTATION FAULT
#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; int print_user(const User *user) { if (user == NULL) { return 1; } else { // Print the profile_pic FILE *profile_pic; profile_pic = fopen(user->profile_pic, "r"); char line[100]; if (profile_pic != NULL) { while (fgets(line, 100, profile_pic) != NULL) { printf("%s", line); } } fclose(profile_pic); // Prints the name printf("\nName: %s\n", user->name); printf("------------------------------------------\n"); // Prints the friends 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"); return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-15T09:29:12.204Z
Question from Code
v1
why am i getting a segmentation fault (core dumped)
#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; int create_user(const char *name, User **user_ptr_add) { User *curr = *user_ptr_add; User *new_user = malloc(sizeof(User)); // Check if memory allocation was successful; exits if not. if (new_user == NULL) { exit(1); } if (strlen(name) >= MAX_NAME) { free(new_user); return 2; } else if (curr == NULL) { *user_ptr_add = new_user; new_user->next = *user_ptr_add; } else { User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = next; next = curr -> next; } else { free(new_user); return 1; } } curr->next = new_user; new_user->next = NULL; } new_user->profile_pic[0] = '\0'; strcpy(new_user->name, name); for (int i=0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } new_user->first_post = NULL; return 0; } int print_user(const User *user) { if (user == NULL) { return 1; } else { // Print the profile_pic FILE *profile_pic; profile_pic = fopen(user->profile_pic, "r"); char line[100]; if (profile_pic != NULL) { while (fgets(line, 100, profile_pic) != NULL) { printf("%s", line); } } fclose(profile_pic); // Prints the name printf("\nName: %s\n", user->name); printf("------------------------------------------\n"); // Prints the friends 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"); return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-15T09:30:05.608Z
Question from Code
v1
why am i getting a segmentation fault (core dumped)
#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; int print_user(const User *user) { if (user == NULL) { return 1; } else { // Print the profile_pic FILE *profile_pic; profile_pic = fopen(user->profile_pic, "r"); char line[100]; if (profile_pic != NULL) { while (fgets(line, 100, profile_pic) != NULL) { printf("%s", line); } } fclose(profile_pic); // Prints the name printf("\nName: %s\n", user->name); printf("------------------------------------------\n"); // Prints the friends 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"); return 0; } }
null
null
63c647675f004bf4898cf37e
2023-02-15T09:40:08.793Z
Question from Code
v1
why does my while loop run forever
#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; /* * 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; User *new_user = malloc(sizeof(User)); // Check if memory allocation was successful; exits if not. if (new_user == NULL) { exit(1); } if (strlen(name) >= MAX_NAME) { free(new_user); return 2; } else if (curr == NULL) { *user_ptr_add = new_user; new_user->next = *user_ptr_add; } else { User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) != 0) { curr = curr -> next; next = curr -> next; } else { free(new_user); return 1; } } curr->next = new_user; new_user->next = NULL; } new_user->profile_pic[0] = '\0'; strcpy(new_user->name, name); for (int i=0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } new_user->first_post = NULL; return 0; }
null
null
63c647675f004bf4898cf37e
2023-02-15T09:43:16.813Z
Question from Code
v1
why is my while loop running forever
#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; /* * 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; User *new_user = malloc(sizeof(User)); // Check if memory allocation was successful; exits if not. if (new_user == NULL) { exit(1); } if (strlen(name) >= MAX_NAME) { free(new_user); return 2; } else if (curr == NULL) { *user_ptr_add = new_user; new_user->next = *user_ptr_add; } else { User *next = curr -> next; // While loop to check if the name is already in the list. while (next != NULL) { if (strcmp(name, next->name) == 0) { free(new_user); return 1; } curr = curr -> next; next = next -> next; } curr->next = new_user; new_user->next = NULL; } new_user->profile_pic[0] = '\0'; strcpy(new_user->name, name); for (int i=0; i < MAX_FRIENDS; i++) { new_user->friends[i] = NULL; } new_user->first_post = NULL; return 0; }
null
null
63c647675f004bf4898cf37e
2023-02-15T10:02:56.564Z
Help Fix Code
v1
null
#include <time.h> #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; int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } else { // Create and allocate a new post. Post* new_post = malloc(sizeof(Post)); // Check if memory allocation was successful; exits if not. if (new_post == NULL) { exit(1); } // Check if users are friends if (check_friends((User*) author, target) == 0) { strcpy(new_post->author, author->name); strcpy(new_post->contents, contents); // Grab the date. time_t date = time(NULL); time(&date); new_post->date = &date; // Change the posts in author's struct. Post* old_post = target->first_post; if (old_post == NULL) { target->first_post = new_post; target->next = NULL; } else { target->first_post = new_post; new_post->next = old_post; } return 0; } else { free(new_post); return 1; } } }
/* * 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 */ I am currently getting a segmentation fault (core dumped).
null
63c647675f004bf4898cf37e
2023-02-15T11:39:02.020Z
Help Fix Code
v1
null
#include <time.h> #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; int make_post(const User *author, User *target, char *contents) { if (author == NULL || target == NULL) { return 2; } else { // Create and allocate a new post. Post* new_post = malloc(sizeof(Post)); // Check if memory allocation was successful; exits if not. if (new_post == NULL) { exit(1); } // Check if users are friends if (check_friends((User*) author, target) == 0) { strcpy(new_post->author, author->name); new_post->contents = contents; // Grab the current time (using time(NULL)) and assign to new_post. time_t curr_time = time(NULL); time_t *date = malloc(sizeof(time_t)); // If memory could not be allocated to date, exit and free the new_post memory. if (date == NULL) { free(new_post); exit(1); } *date = curr_time; new_post->date = date; // Change the posts in author's struct. Post* old_post = target->first_post; if (old_post == NULL) { target->first_post = new_post; target->next = NULL; } else { target->first_post = new_post; new_post->next = old_post; } return 0; } else { free(new_post); return 1; } } } /* * 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* user = find_user(name, *user_ptr_del); if (user == NULL) { return 1; } User *curr_user = *user_ptr_del; while (curr_user != NULL) { // Check if the current user in the list is friends with the deleted user. if (check_friends(user, curr_user) == 0) { for (int i=0; i < MAX_FRIENDS; i++) { if (curr_user->friends[i] == user) { curr_user->friends[i] = NULL; } user->friends[i] = NULL; } } } // Remove all posts from the user's profile. Post *curr_post = user->first_post; Post *next_post = curr_post->next; if (curr_post != NULL) { while (next_post != NULL) { // Free all allocated memory from the current post. free(curr_post->contents); free(curr_post->date); free(curr_post->next); free(curr_post); curr_post = next_post; next_post = curr_post->next; } } // Check if user is first in the linked list. if (user == *user_ptr_del) { // Check if the deleted user is the only user. if (user->next == NULL) { *user_ptr_del = NULL; } else { *user_ptr_del = user->next; user->next = NULL; } } else { User* prev_user = *user_ptr_del; User* curr_user = prev_user->next; User* next_user = curr_user->next; // Iterate until curr_user is the deleted user. while (curr_user != user) { prev_user = prev_user->next; curr_user = curr_user->next; next_user = next_user->next; } prev_user->next = next_user; curr_user->next = NULL; } // Free all alocated memory from the user. free(user->next); free(user->first_post); free(user); 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 */ My while loop is running forever.
null
63c647675f004bf4898cf37e
2023-02-15T19:19:56.804Z
Question from Code
v1
Why am I having a memory leakage from this?
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; int print_user(const User *user) { if (user == NULL) { return 1; } else { // Prints the profile_pic FILE *pic; if (user->profile_pic[0] != '\0') { pic = fopen(user->profile_pic, "r"); char line[100]; if (pic != NULL) { while (fgets(line, 100, pic) != NULL) { printf("%s", line); } printf("\n"); } } fclose(pic); }
null
null
63c647675f004bf4898cf37e
2023-02-15T19:20:56.252Z
Question from Code
v1
Why am I having a memory leakage? It seems like the pic is being closed by fclose(pic);
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; int print_user(const User *user) { if (user == NULL) { return 1; } else { // Prints the profile_pic FILE *pic; if (user->profile_pic[0] != '\0') { pic = fopen(user->profile_pic, "r"); char line[100]; if (pic != NULL) { while (fgets(line, 100, pic) != NULL) { printf("%s", line); } printf("\n"); } } fclose(pic); }
null
null
63c647675f004bf4898cf37e
2023-02-15T19:36:02.092Z
Question from Code
v1
Why am I getting a memory leakage? In function update_pic, the file is being closed using fclose(image);
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; /* * 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) >= MAX_NAME) { return 2; } else { FILE* image; image = fopen(filename, "r"); if (image == NULL) { return 1; } else { strcpy(user->profile_pic, filename); return 0; } fclose(image); } } int print_user(const User *user) { if (user == NULL) { return 1; } else { // Prints the profile_pic FILE *pic; if (user->profile_pic[0] != '\0') { pic = fopen(user->profile_pic, "r"); char line[100]; if (pic != NULL) { while (fgets(line, 100, pic) != NULL) { printf("%s", line); } printf("\n"); } } fclose(pic); }
null
null
63c647675f004bf4898cf37e
2023-02-17T20:23:48.522Z
Help Write Code
v1
null
null
null
/* Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. Capacity is expected to be the number of bytes of memory allocated to dest. You shoud read the man page to learn how strncpy works. NOTE: You must write this function without using any string functions. The only function that should depend on string.h is memset. */
63c647675f004bf4898cf37e
2023-02-17T22:39:10.196Z
Explain Code
v1
null
#include <stdio.h> #define SIZE 4 #define OVERFLOW 4 int main() { int index = 0; int i; int before[SIZE] = {10, 10, 10, 10}; int a[SIZE] = {0, 0, 0, 0}; int after[SIZE] = {10, 10, 10, 10}; printf("Address of the variables:\n"); for (index = 0; index < SIZE; index++) { printf("%lx -> &after[%d]\n", (unsigned long) &after[index], index); } for (index = 0; index < SIZE; index++) { printf("%lx -> &a[%d]\n", (unsigned long) &a[index], index); } for (index = 0; index < SIZE; index++) { printf("%lx -> &before[%d]\n", (unsigned long) &before[index], index); } printf("%lx -> &i\n", (unsigned long)&i); printf("%lx -> &index\n", (unsigned long)&index); printf("\n"); printf("Initial values:\n"); printf("i = %d\n", i); printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]); printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]); printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]); printf("\n"); for (i = 0; i < OVERFLOW; i++) { a[i] = i * 10; printf("i = %d\n", i); printf("before = {%d, %d, %d, %d}\n", before[0], before[1], before[2], before[3]); printf("a = {%d, %d, %d, %d}\n", a[0], a[1], a[2], a[3]); printf("after = {%d, %d, %d, %d}\n", after[0], after[1], after[2], after[3]); } return 0; }
null
null
63c647695f004bf4898cf38d
2023-01-27T21:06:10.224Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ *fib_sequence = malloc(sizeof(int) * count); if (count >= 1){ (*fib_sequence)[0] = 0; } if (count >= 2){ (*fib_sequence)[1] = 1; } if (count >= 3){ for (int i = 2; i < count; i++){ (*fib_sequence)[i] = (*fib_sequence)[i-2] + (*fib_sequence)[i-1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
Building a dynamic array containing the first n elements of the Fibonacci sequence
null
63c647695f004bf4898cf38d
2023-01-27T21:13:00.344Z
Help Fix Code
v1
null
void fib(int **fib_sequence, int count){ *fib_sequence = malloc(sizeof(int) * count); if (count >= 1){ (*fib_sequence)[0] = 0; } if (count >= 2){ (*fib_sequence)[1] = 1; } if (count >= 3){ for (int i = 2; i < count; i++){ (*fib_sequence)[i] = (*fib_sequence)[i-2] + (*fib_sequence)[i-1]; } } } int main(int argc, char **argv) { /* do not change this main function */ int count = strtol(argv[1], NULL, 10); int *fib_sequence; fib(&fib_sequence, count); for (int i = 0; i < count; i++) { printf("%d ", fib_sequence[i]); } free(fib_sequence); return 0; }
/* * Define a function void fib(...) below. This function takes parameter n * and generates the first n values in the Fibonacci sequence. Recall that this * sequence is defined as: * 0, 1, 1, 2, 3, 5, ... , fib[n] = fib[n-2] + fib[n-1], ... * The values should be stored in a dynamically-allocated array composed of * exactly the correct number of integers. The values should be returned * through a pointer parameter passed in as the first argument. * * See the main function for an example call to fib. * Pay attention to the expected type of fib's parameters. */
null
63c647695f004bf4898cf38d
2023-01-29T23:38:28.773Z
Help Fix Code
v1
null
int populate_array(int sin, int *sin_array) { int temp = sin; int size = 0; while (temp != 0){ temp /= 10; size ++; } if (size != 9) { return 1; } else { for (int i = size - 1; i >= 0; i--){ sin_array[i] = sin % 10; sin /= 10; } return 0; } }
/* * Convert a 9 digit int to a 9 element int array. */
null
63c647695f004bf4898cf38d
2023-01-29T23:39:54.539Z
Explain Code
v1
null
int populate_array(int sin, int *sin_array) { int temp = sin; int size = 0; while (temp != 0){ temp /= 10; size ++; } if (size != 9) { return 1; } else { for (int i = size - 1; i >= 0; i--){ sin_array[i] = sin % 10; sin /= 10; } return 0; } }
null
null