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
63c6479f5f004bf4898cf545
2023-03-31T21:24:41.321Z
General Question
v2
client: connect: Connection refused how to fix
null
null
null
63c6479f5f004bf4898cf545
2023-03-31T21:28:58.340Z
General Question
v2
what steps can i take to fix the error client: connect: Connection refused
null
null
null
63c6479f5f004bf4898cf545
2023-04-05T19:21:59.212Z
Question from Code
v2
how to change the error: initialization discards ‘const’ qualifier from pointer target type error
char *list_users(const User *curr) { // Get the length of the returned string int num_char = 0; User *curr_user = curr;
null
null
63c6479f5f004bf4898cf545
2023-04-05T19:29:49.417Z
Question from Code
v2
help me fix this error
friends.c : 251 : 9 : error : format not a string literal and no format arguments[-Werror = format - security] 251 | num_bytes_written += snprintf(user_buf + num_bytes_written, num_char - num_bytes_written, print_post(curr)); // Append the post contents
null
null
63c6479f5f004bf4898cf545
2023-04-06T13:51:02.998Z
Question from Code
v2
will this handle partial reads, read one full line and then do something with the full line?
char *read(int client_index, struct sockname *users) { int fd = users[client_index].sock.fd; char buf[BUFSIZE + 1]; // Read user input and handle partial reads int inbuf = 0; // Number of bytes in buf int room = sizeof(buf); // How many bytes left in the buf char *after = buf; // Pointer to the position after the data int nbytes; int command_read = 1; // Store if a full line has been read while ((nbytes = read(fd, after, room)) > 0 || command_read == 1) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read buf[where - 2] = '\0'; // Null terminate the buffer // Update inbuf inbuf -= where; // Move everything after the full memmove(buf, buf + (sizeof(char) * where), inbuf); command_read = 0; } // Update after and room after = &buf[inbuf]; room = BUFSIZE - inbuf; } close(fd); // Check for recieved username if (users[client_index].username == NULL) { users[client_index].username = malloc(sizeof(char) * strlen(buf) + 1); if (users[client_index].username == NULL) { perror("malloc"); free(users[client_index].username); exit(1); } // Set the username strcpy(users[client_index].username, buf); users[client_index].username[strlen(buf) + 1] = '\0'; return NULL; // Return NULL if username was set } else { // It is a command int cmd_len = strlen(buf) + 1; char *command = malloc(sizeof(char) * cmd_len); if (command == NULL) { perror("malloc"); exit(1); } strcpy(command, buf); command[length] = '\0'; return command; // Return the command } }
null
null
63c6479f5f004bf4898cf545
2023-04-06T20:28:55.085Z
Question from Code
v2
why is this stuck in an infinite loop
char *read_input(int fd) { char buf[INPUT_BUFFER_SIZE + 1]; // Read user input and handle partial reads int inbuf = 0; // Number of bytes in buf int room = sizeof(buf); // How many bytes left in the buf char *after = buf; // Pointer to the position after the data int nbytes; int command_read = 1; // Store if a full line has been read while ((nbytes = read(fd, after, room)) > 0 || command_read == 1) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read buf[where - 2] = '\0'; // Null terminate the buffer // Update inbuf inbuf -= where; // Move everything after the full memmove(buf, buf + (sizeof(char) * where), inbuf); command_read = 0; } // Update after and room after = &buf[inbuf]; room = INPUT_BUFFER_SIZE - inbuf; // Check for closed connection if (nbytes == 0) { close(fd); return NULL; } } close(fd); // Return the input char *input = malloc(sizeof(char) * (strlen(buf) + 1)); if (input == NULL) { perror("malloc"); exit(1); } strcpy(input, buf); input[strlen(buf)] = '\0'; return input; }
null
null
63c6479f5f004bf4898cf545
2023-04-07T00:47:01.713Z
General Question
v2
How do you use EWOULDBLOCK or EAGAIN to make sure read does not block
null
null
null
63c6479f5f004bf4898cf545
2023-04-07T01:38:13.872Z
General Question
v2
do i need to deal with the remaining data in buffer if I'm reading one line doing partial reads
null
null
null
63c6479f5f004bf4898cf545
2023-04-07T01:39:51.103Z
Question from Code
v2
can i delete the code that deals with the remaining data
// Read user input and handle partial reads int inbuf = 0; // Number of bytes in buf int room = sizeof(buf); // How many bytes left in the buf char *after = buf; // Pointer to the position after the data int nbytes; while ((nbytes = read(fd, after, room)) > 0) { inbuf += nbytes; int where; if ((where = find_network_newline(buf, inbuf)) > 0) { // When full line is read buf[where - 2] = '\0'; // NULL terminate buff // Handle remaining data in buffer int remaining = inbuf - where; char *rest = malloc(sizeof(char) * (remaining + 1)); if (rest == NULL) { perror("malloc"); exit(1); } memcpy(rest, buf + where, remaining); rest[remaining] = '\0'; // Return the input char *input = malloc(sizeof(char) * (strlen(buf) + 1)); if (input == NULL) { perror("malloc"); exit(1); } strcpy(input, buf); input[strlen(buf)] = '\0'; // Free the rest and return free(rest); return input; }
null
null
63c6479f5f004bf4898cf545
2023-04-07T01:48:48.794Z
Question from Code
v2
am i passing in the correct user name into process_args
while (curr_client != NULL) { if (curr_client->client_soc > -1 && FD_ISSET(curr_client->client_soc, &all_fds)) { char *input = read_input(curr_client->client_soc); if (input == NULL) { // If connection closed printf("Client %d disconnected\n", client_count); } else if (read_from(client_count, users, input) == 1) { // If input is a username if (find_user(input, users_list) == NULL) { // Check for existing or new user // Write a welcome message char *welcome_msg; if (curr_client->welcome_status == 0) { welcome_msg = "Welcome\r\nEnter commands\r\n> "; // If first time user } else { welcome_msg = "Welcome back\r\nEnter commands\r\n> "; // If returning user } int write_error = write(curr_client->client_soc, welcome_msg, strlen(welcome_msg)); // Write the message to the client if (write_error < 0) { // Error Checking perror("write"); exit(1); } curr_client->welcome_status = 1; // Update to flag that user recieved a welcome message } } else { // If input is a command // Process the arguments char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv, curr_client->client_soc); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &users_list, users[client_count].username, curr_client->client_soc) == -1) { break; // can only reach if quit command was entered } // Print the prompt arrow char *prompt_arrow = "> "; int write_error = write(curr_client->client_soc, prompt_arrow, strlen(prompt_arrow)); if (write_error < 0) { perror("write"); exit(1); } } } client_count++; // Update the client count --INSERT--
null
null
63c6479f5f004bf4898cf545
2023-04-07T01:54:26.837Z
Question from Code
v2
how will the server know which user is using it
while (curr_client != NULL) { if (curr_client->client_soc > -1 && FD_ISSET(curr_client->client_soc, &all_fds)) { char *input = read_input(curr_client->client_soc); if (input == NULL) { // If connection closed printf("Client %d disconnected\n", client_count); } else if (read_from(client_count, users, input) == 1) { // If input is a username if (find_user(input, users_list) == NULL) { // Check for existing or new user // Write a welcome message char *welcome_msg; if (curr_client->welcome_status == 0) { welcome_msg = "Welcome\r\nEnter commands\r\n> "; // If first time user } else { welcome_msg = "Welcome back\r\nEnter commands\r\n> "; // If returning user } int write_error = write(curr_client->client_soc, welcome_msg, strlen(welcome_msg)); // Write the message to the client if (write_error < 0) { // Error Checking perror("write"); exit(1); } curr_client->welcome_status = 1; // Update to flag that user recieved a welcome message } } else { // If input is a command // Process the arguments char *cmd_argv[INPUT_ARG_MAX_NUM]; int cmd_argc = tokenize(input, cmd_argv, curr_client->client_soc); if (cmd_argc > 0 && process_args(cmd_argc, cmd_argv, &users_list, users[client_count].username, curr_client->client_soc) == -1) { break; // can only reach if quit command was entered } // Print the prompt arrow char *prompt_arrow = "> "; int write_error = write(curr_client->client_soc, prompt_arrow, strlen(prompt_arrow)); if (write_error < 0) { perror("write"); exit(1); } } } client_count++; // Update the client count --INSERT--
null
null
63c6479f5f004bf4898cf545
2023-04-10T23:26:03.572Z
Help Write Code
v2
null
null
null
a function called read_input that handles partial reads and returns 1 full line (newline terminated) of input
63c647a05f004bf4898cf54a
2023-01-23T20:39:05.298Z
General Question
v1
what is a pointer
null
null
null
63c647a05f004bf4898cf54a
2023-01-24T22:09:39.163Z
Help Fix Code
v1
null
void fib(int **arr, int n){ *arr = malloc(sizeof(int)*(n)); for (int x = 0; x < n; x++){ if (x == 0){ *arr[x] = 0; } if (x == 1){ *arr[x] = 1; } }
I want to assign the second value of the array to 1.
null
63c647a05f004bf4898cf54a
2023-01-24T22:11:25.646Z
Help Fix Code
v1
null
void fib(int **arr, int n){ *arr = malloc(sizeof(int)*(n)); for (int x = 0; x < n; x++){ if (x == 0){ *arr[x] = 0; } if (x == 1){ *arr[x] = 1; } }
I'm getting a cygwin_exception::open_stackdumpfile error
null
63c647a05f004bf4898cf54a
2023-01-24T22:16:30.746Z
Explain Code
v1
null
void fib(int **arr, int n){ *arr = malloc(sizeof(int)*(n)); for (int x = 0; x < n; x++){ if (x == 0){ *arr[x] = 0; } else if (x == 1){ *arr[x] = 1; } else { *arr[x] = *arr[x-1] + *arr[x-2]; printf("%d\n", *arr[x]); } } }
null
null
63c647a05f004bf4898cf54a
2023-02-10T02:53:55.521Z
General Question
v1
How do i cast a constant struct pointer to a struct pointer?
null
null
null
63c647a05f004bf4898cf54a
2023-02-10T02:56:49.239Z
Question from Code
v1
Am I casting a constant to head?
User *find_user(const char *name, const User *head) { while (head != NULL){ if (strncmp(name, head->name, MAX_NAME) == 0){ return (const User*)head; } head = head->next; } return NULL; }
null
null
63c647a05f004bf4898cf54a
2023-02-10T03:05:09.922Z
Question from Code
v1
Why am i getting a segmentation fault
int create_user(const char *name, User **user_ptr_add) { if (strlen(name) >= MAX_NAME){ return 2; } while (*user_ptr_add != NULL){ if (strncmp(name, (*user_ptr_add)->name, MAX_NAME) == 0){ return 1; } if((*user_ptr_add)->next == NULL){ break; } *user_ptr_add = (*user_ptr_add)->next; } User *new_user = malloc(sizeof(User)); strncpy(new_user->name, name, MAX_NAME); new_user->name[MAX_NAME-1] = '\0'; (*user_ptr_add)->next = new_user; return 0;
null
null
63c647a05f004bf4898cf54f
2023-01-19T18:34:07.339Z
Help Fix Code
v1
null
int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (i=0; i<=size; i++) { sum += **score_card[i]; } return sum; }
null
null
63c647a05f004bf4898cf54f
2023-01-19T18:34:52.537Z
Help Fix Code
v1
null
int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (i=0; i<=size; i++) { sum += *score_card[i]; } return sum; }
null
null
63c647a05f004bf4898cf54f
2023-01-19T18:38:31.354Z
Help Fix Code
v1
null
int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (i=0; i<=size; i++) { sum += *score_card[i]; } return sum; } /* * NOTE: don't change the main function! * The command line arguments are a sequence of integers that will be * used to initialize the array score_card. * * Sample usage: * $ gcc -Wall -std=gnu99 -g -o score_card score_card.c * $ ./score_card 10 -3 4 * Sum: 11 */ int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c647a05f004bf4898cf54f
2023-01-19T19:49:23.743Z
Help Fix Code
v1
null
int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (int i=0; i<=size; i++) { sum += *score_card[i]; } return sum; } /* * NOTE: don't change the main function! * The command line arguments are a sequence of integers that will be * used to initialize the array score_card. * * Sample usage: * $ gcc -Wall -std=gnu99 -g -o score_card score_card.c * $ ./score_card 10 -3 4 * Sum: 11 */ int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c647a05f004bf4898cf54f
2023-01-19T20:58:57.094Z
Explain Code
v1
null
int main(int argc, char **argv) { int size = argc - 1; int *score_card[size]; for (int i = 0; i < size; i++) { // NOTE: We haven't covered malloc yet, so don't worry about this line. score_card[i] = malloc(sizeof(int)); *(score_card[i]) = strtol(argv[i + 1], NULL, 10); } printf("Sum: %d\n", sum_card(score_card, size)); return 0; }
null
null
63c647a05f004bf4898cf54f
2023-01-19T21:39:25.965Z
Explain Code
v1
null
int sum_card(int **score_card, int size) { // TODO: write the body of sum_card according to its description. int sum = 0; for (int i=0; i<=size; i++) { sum += score_card[i]; } return sum; }
null
null
63c647a35f004bf4898cf568
2023-01-31T04:06:04.090Z
Question from Code
v1
Is this a valid way to truncate a string
strncpy(s, s, n);
null
null
63c647a35f004bf4898cf568
2023-02-13T21:55:52.254Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { typedef struct user { char name[32]; char profile_pic[32]; struct post *first_port; struct user *friends[10]; struct user *next; } User; User this_user; this_user.next = NULL; /* Need to check if name can be fully copied over. */ if (strlen(name) > sizeof(this_user.name) - 1) { return 2;} /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ if (*user_ptr_add == NULL) { strncpy(this_user.name, name, sizeof(this_user.name)); this_user.name[31] = '\0'; *user_ptr_add = &this_user; return 0; } /* Case 2: linked list already contains users. */ User *curr = *user_ptr_add; /* Find the last node <- its next is NULL. */ while (curr->next != NULL) { /* Check for duplicate names. */ if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } curr->next = &this_user; strncpy(this_user.name, name, sizeof(this_user.name)); this_user.name[31] = '\0'; return -1; }
Create a new user with the given name. Insert it at the tail of the list of users whose head is pointed to by *user_ptr_add. Return: - 0 if successful - 1 if a user by this name already exists in this list - 2 if the given name cannot fit in the 'name' array (don't forget about the null terminator)
null
63c647a35f004bf4898cf568
2023-02-13T22:06:18.020Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; /* Need to check if name can be fully copied over. */ if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ if (*user_ptr_add == NULL) { strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; *user_ptr_add = this_user; return 0; } /* Case 2: linked list already contains users. */ User *curr = *user_ptr_add; /* Find the last node <- its next is NULL. */ while (curr->next != NULL) { /* Check for duplicate names. */ if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } curr->next = this_user; strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; return -1; }
Create a new user with the given name. Insert it at the tail of the list of users whose head is pointed to by *user_ptr_add. Return: - 0 if successful - 1 if a user by this name already exists in this list - 2 if the given name cannot fit in the 'name' array (don't forget about the null terminator)
null
63c647a35f004bf4898cf568
2023-02-13T22:07:35.683Z
Question from Code
v1
I got an error: Undefined symbols for architecture arm64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) How do I resolve this?
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; /* Need to check if name can be fully copied over. */ if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ if (*user_ptr_add == NULL) { strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; *user_ptr_add = this_user; return 0; } /* Case 2: linked list already contains users. */ User *curr = *user_ptr_add; /* Find the last node <- its next is NULL. */ while (curr->next != NULL) { /* Check for duplicate names. */ if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } curr->next = this_user; strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; return -1; }
null
null
63c647a35f004bf4898cf568
2023-02-14T00:51:04.073Z
Explain Code
v1
null
void list_users(const User *curr) { while (curr != NULL) { printf(curr->name); curr = curr->next; } }
null
null
63c647a35f004bf4898cf568
2023-02-14T00:51:42.488Z
Help Fix Code
v1
null
void list_users(const User *curr) { while (curr != NULL) { printf(curr->name); curr = curr->next; } }
Print the usernames of all users in the list starting at curr. Names should be printed to standard output, one per line. Is there anything to be fixed?
null
63c647a35f004bf4898cf568
2023-02-14T00:52:34.852Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User *)head; while (curr != NULL) { if (strcmp(curr->name, name) == 0){ return &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. NOTE: You'll likely need to cast a (const User *) to a (User *) to satisfy the prototype without warnings. Is there anything to fix?
null
63c647a35f004bf4898cf568
2023-02-14T00:53:46.285Z
Help Fix Code
v1
null
User *find_user(const char *name, const User *head) { User *curr = (User *)head; while (curr != NULL) { if (strcmp(curr->name, name) == 0){ return 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. NOTE: You'll likely need to cast a (const User *) to a (User *) to satisfy the prototype without warnings. Is there anything to fix?
null
63c647a35f004bf4898cf568
2023-02-14T01:52:28.153Z
Help Fix Code
v1
null
int update_pic(User *user, const char *filename) { /* Check if filename is too long. */ if (strlen(filename) > sizeof(user->profile_pic) - 1) { return 2; } elif (fopen(filename, "r") == NULL) { return 1; } else { user->profile_pic = filename; return 0; } }
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.
null
63c647a35f004bf4898cf568
2023-02-14T01:56:17.495Z
Explain Code
v1
null
int update_pic(User *user, const char *filename) { /* Check if filename is too long. */ if (strlen(filename) > sizeof(user->profile_pic) - 1) { return 2; } else if (fopen(filename, "r") == NULL) { return 1; } else { strncpy(user->profile_pic, filename, sizeof(user->profile_pic)); return 0; } }
null
null
63c647a35f004bf4898cf568
2023-02-14T01:57:20.258Z
Question from Code
v1
If the file does not exist, will fopen() also return NULL?
int update_pic(User *user, const char *filename) { /* Check if filename is too long. */ if (strlen(filename) > sizeof(user->profile_pic) - 1) { return 2; } else if (fopen(filename, "r") == NULL) { return 1; } else { strncpy(user->profile_pic, filename, sizeof(user->profile_pic)); return 0; } }
null
null
63c647a35f004bf4898cf568
2023-02-14T02:07:55.799Z
Help Fix Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; this_user->first_post = NULL; /* Need to check if name can be fully copied over. */ if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ if (*user_ptr_add == NULL) { strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; *user_ptr_add = this_user; return 0; } /* Case 2: linked list already contains users. */ User *curr = *user_ptr_add; /* Find the last node <- its next is NULL. */ while (curr->next != NULL) { /* Check for duplicate names. */ if (strcmp(curr->name, name) == 0) { return 1; } curr = curr->next; } curr->next = this_user; strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; 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) However, my code is failing in the case where the name is already in the list. I think the while loop is not working. It does not loop from the head of the linked list.
null
63c647a35f004bf4898cf568
2023-02-14T02:20:03.693Z
Question from Code
v1
I have this pointer to the head of a linked list, how do I traverse the linked list?
User **user_ptr_add
null
null
63c647a35f004bf4898cf568
2023-02-14T02:24:21.196Z
Explain Code
v1
null
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; this_user->first_post = NULL; /* Need to check if name can be fully copied over. */ if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} /* Case 1: I am the first user. user_ptr_add is NULL? the linked list is empty. */ if (*user_ptr_add == NULL) { strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; *user_ptr_add = this_user; return 0; } /* Case 2: linked list already contains users. */ User *curr = *user_ptr_add; /* Find the last node <- its next is NULL. */ while (curr != NULL) { /* Check for duplicate names. */ if (strcmp(curr->name, name) == 0) { printf("%s\n", curr->name); return 1; } // if (curr->next == NULL) { // curr->next = this_user; // } curr = curr->next; } curr = this_user; strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; return 0; }
null
null
63c647a35f004bf4898cf568
2023-02-14T03:32:54.407Z
Question from Code
v1
Is friends a pointer to an array that stores 1o user structs?
#define MAX_NAME 32 // Max username and profile_pic filename lengths #define MAX_FRIENDS 10 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;
null
null
63c647a35f004bf4898cf568
2023-02-14T03:35:04.694Z
General Question
v1
if I have an array of pointers, how can I determine its size?
null
null
null
63c647a35f004bf4898cf568
2023-02-14T23:21:38.318Z
Question from Code
v1
How do i initialize a time_t
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *this_post = malloc(sizeof(Post)); strcpy(this_post->author, author->name); this_post->contents = contents;
null
null
63c647a35f004bf4898cf568
2023-02-14T23:22:31.525Z
Question from Code
v1
Is this the correct way to initialize a time_t?
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *this_post = malloc(sizeof(Post)); strcpy(this_post->author, author->name); this_post->contents = contents; this_post->time_t = time(NULL);
null
null
63c647a35f004bf4898cf568
2023-02-14T23:24:14.102Z
Question from Code
v1
How do I initialize a time_t? I did that and got this error: incompatible integer to pointer conversion assigning to 'time_t *' (aka 'long *') from 'time_t' (aka 'long')
typedef struct post { char author[MAX_NAME]; char *contents; time_t *date; struct post *next; } Post; Post *this_post = malloc(sizeof(Post)); strcpy(this_post->author, author->name); this_post->contents = contents; this_post->date = time(NULL);
null
null
63c647a35f004bf4898cf568
2023-02-15T17:23:38.274Z
Help Fix Code
v1
null
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 delete_user(const char *name, User **user_ptr_del) { User *this_user = find_user(name, *user_ptr_del); // Case 1: User with this name does not exist if (this_user == NULL) { return 1; } // Case 0: Delete user // Remove user from each friend's friend list int this_user_idx; for (int i = 0; i < sizeof(this_user->friends)/sizeof(this_user->friends[0]); i++) { // Find the index of this_user User *frd = this_user->friends[i]; for (int j = 0; j < sizeof(frd->friends)/sizeof(frd->friends[0]); j++) { if (frd->friends[j] == this_user) { this_user_idx = j; } } printf("%d", this_user_idx); }
I keep getting a seg fault in line 22 where I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.
null
63c647a35f004bf4898cf568
2023-02-15T17:27:05.488Z
Help Fix Code
v1
null
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 delete_user(const char *name, User **user_ptr_del) { User *this_user = find_user(name, *user_ptr_del); // Case 1: User with this name does not exist if (this_user == NULL) { return 1; } // Case 0: Delete user // Remove user from each friend's friend list int this_user_idx; for (int i = 0; i < MAX_FRIENDS; i++) { // Find the index of this_user User *frd = this_user->friends[i]; for (int j = 0; j < MAX_FRIENDS; j++) { if (frd->friends[j] == this_user) { this_user_idx = j; } } printf("%d", this_user_idx); }
I keep getting a seg fault in line 22 where I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.
null
63c647a35f004bf4898cf568
2023-02-15T17:28:29.429Z
Question from Code
v1
I keep getting a seg fault in line 22. I want to take this_user's friend list, loop through each friend, and find this_user's index in the friend's friend list.
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 delete_user(const char *name, User **user_ptr_del) { User *this_user = find_user(name, *user_ptr_del); // Case 1: User with this name does not exist if (this_user == NULL) { return 1; } // Case 0: Delete user // Remove user from each friend's friend list int this_user_idx; for (int i = 0; i < MAX_FRIENDS; i++) { // Find the index of this_user User *frd = this_user->friends[i]; for (int j = 0; j < MAX_FRIENDS; j++) { if (frd->friends[j] == this_user) { this_user_idx = j; } } printf("%d", this_user_idx); }
null
null
63c647a35f004bf4898cf568
2023-02-15T21:01:22.117Z
Question from Code
v1
I want to free the memory in the heap but I am getting a memory leak.
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; Post *curr_post = this_user->first_post; while (curr_post != NULL) { Post* next_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); curr_post = next_post; } free(this_user);
null
null
63c647a35f004bf4898cf568
2023-02-15T21:03:26.305Z
Question from Code
v1
Still getting a memory leak
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; Post *curr_post = this_user->first_post; while (curr_post != NULL) { Post* next_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); curr_post = next_post; } free(this_user->next); free(this_user);
null
null
63c647a35f004bf4898cf568
2023-02-15T21:07:43.306Z
Question from Code
v1
Do I need to free the friends array that is an array of pointers pointing to User structs
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; Post *curr_post = this_user->first_post; while (curr_post != NULL) { Post* next_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); curr_post = next_post; } free(this_user->next); free(this_user);
null
null
63c647a35f004bf4898cf568
2023-02-15T21:08:39.696Z
Question from Code
v1
Do I need to free the next pointer in this_user? It points to the next user struct in the linked list.
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; Post *curr_post = this_user->first_post; while (curr_post != NULL) { Post* next_post = curr_post->next; free(curr_post->contents); free(curr_post->date); free(curr_post); curr_post = next_post; } free(this_user->next); free(this_user);
null
null
63c647a35f004bf4898cf568
2023-02-15T23:27:35.592Z
Question from Code
v1
Is this how to allocate space for a User?
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; User *this_user = malloc(sizeof(User));
null
null
63c647a35f004bf4898cf568
2023-02-15T23:50:40.175Z
Question from Code
v1
I think I called malloc twice but I don't know where
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; this_user->first_post = NULL; strcpy(this_user->profile_pic, "\0"); for (int i = 0; i < MAX_FRIENDS; i++) { this_user->friends[i] = NULL; } // Case 2: Check if name can be fully copied over. if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} // Case 1: User exists in linked list. if (find_user(name, *user_ptr_add) != NULL) { return 1; } strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; // Case 0: (a) Linked list is empty if (*user_ptr_add == NULL) { *user_ptr_add = this_user; } else { // Case 0: (b) Linked list is not empty User *curr = *user_ptr_add; while (curr->next != NULL) { curr = curr->next; } curr->next = this_user; } return 0; }
null
null
63c647a35f004bf4898cf568
2023-02-15T23:56:27.511Z
Question from Code
v1
Did I implicitly called malloc more than once?
int create_user(const char *name, User **user_ptr_add) { User *this_user = malloc(sizeof(User)); this_user->next = NULL; this_user->first_post = NULL; strcpy(this_user->profile_pic, "\0"); for (int i = 0; i < MAX_FRIENDS; i++) { this_user->friends[i] = NULL; } // Case 2: Check if name can be fully copied over. if (strlen(name) > sizeof(this_user->name) - 1) { return 2;} // Case 1: User exists in linked list. if (find_user(name, *user_ptr_add) != NULL) { return 1; } strncpy(this_user->name, name, sizeof(this_user->name)); this_user->name[31] = '\0'; // Case 0: (a) Linked list is empty if (*user_ptr_add == NULL) { *user_ptr_add = this_user; } else { // Case 0: (b) Linked list is not empty User *curr = *user_ptr_add; while (curr->next != NULL) { curr = curr->next; } curr->next = this_user; } return 0; }
null
null
63c647a35f004bf4898cf568
2023-02-16T01:07:09.375Z
Question from Code
v1
What happens to my this_user->profile_pic after this line of code?
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; strcpy(this_user->profile_pic, "\0");
null
null
63c647a35f004bf4898cf568
2023-02-17T04:59:51.349Z
Question from Code
v1
does this code have the same implementation as strncpy?
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:01:33.343Z
Question from Code
v1
Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:02:59.804Z
Question from Code
v1
Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[-1] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:04:11.712Z
Question from Code
v1
Does this code do what it is supposed to do: Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated.
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity; i++) { dest[i] = src[i]; } dest[-1] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:05:01.449Z
Help Fix Code
v1
null
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity; i++) { dest[i] = src[i]; } dest[-1] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. How can I fix the code?
null
63c647a35f004bf4898cf568
2023-02-17T05:06:08.699Z
Help Fix Code
v1
null
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[-1] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. How can I fix the code?
null
63c647a35f004bf4898cf568
2023-02-17T05:06:58.068Z
Help Fix Code
v1
null
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[capacity] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
Write the copy function to perform exactly as strncpy does, with one exception: your copy function will guarantee that dest is always null-terminated. How can I fix the code?
null
63c647a35f004bf4898cf568
2023-02-17T05:07:46.571Z
Question from Code
v1
How do I add a null terminator to the end of the string?
char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[capacity] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:08:31.261Z
Question from Code
v1
How do I add a null terminator to the end of the string?
/* 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. */ char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[capacity] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:10:00.366Z
Help Fix Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { memset(dest, '\0', capacity); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[capacity/sizeof(dest)] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
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. Does this code do what it is supposed to do?
null
63c647a35f004bf4898cf568
2023-02-17T05:21:36.308Z
Question from Code
v1
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. Does this code do what it is supposed to do?
/* 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. */ char *copy(char *dest, const char *src, int capacity) { int n = capacity/sizeof(dest)[0]; memset(dest, '\0', n); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[n] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:22:24.126Z
Question from Code
v1
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. Does this code do what it is supposed to do?
/* 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. */ char *copy(char *dest, const char *src, int capacity) { int n = capacity/sizeof(dest)[0]; memset(dest, '\0', n); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < n && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[n] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
null
null
63c647a35f004bf4898cf568
2023-02-17T05:23:41.111Z
Help Fix Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { int n = capacity/sizeof(dest)[0]; memset(dest, '\0', n); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < n && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[n] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
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.
null
63c647a35f004bf4898cf568
2023-02-17T05:24:16.044Z
Help Fix Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { int n = capacity/sizeof(dest)[0]; memset(dest, '\0', n); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < n && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[n] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
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.
null
63c647a35f004bf4898cf568
2023-02-17T05:24:47.834Z
Help Fix Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { int n = capacity/sizeof(dest)[0]; memset(dest, '\0', n); // Case 1: dest < src; cannot fully copy over, need to null terminate for (int i = 0; i < n && src[i] != '\0'; i++) { dest[i] = src[i]; } dest[n] = '\0'; // Case 2: dest > src; have space left in dest // Already memset so no need to worry return dest; }
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.
null
63c647a35f004bf4898cf568
2023-02-17T05:55:45.606Z
Help Fix Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { // Case 1: dest < src; cannot fully copy over, need to null terminate int i; for (i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } for ( ; i < capacity; i++) { dest[i] = '\0';} dest[capacity - 1] = '\0'; // Case 2: dest > src; have space left in dest return dest; }
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.
null
63c647a35f004bf4898cf568
2023-02-17T05:56:39.200Z
Explain Code
v1
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. */ char *copy(char *dest, const char *src, int capacity) { // Case 1: dest < src; cannot fully copy over, need to null terminate int i; for (i = 0; i < capacity && src[i] != '\0'; i++) { dest[i] = src[i]; } for ( ; i < capacity; i++) { dest[i] = '\0';} dest[capacity - 1] = '\0'; // Case 2: dest > src; have space left in dest return dest; }
null
null
63c647a35f004bf4898cf568
2023-03-03T20:25:12.960Z
Question from Code
v1
I am trying to make it such that only the parent can call fork(), but I am stuck.
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int pid; for (int i = 0; i < iterations; i++) { if (i = 0) { int n = fork(); if (n != 0) { pid = getppid(); } } 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
63c647a35f004bf4898cf568
2023-03-03T20:26:42.554Z
Question from Code
v1
How do I modify this code so that only the parent can call fork()?
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
63c647a35f004bf4898cf568
2023-03-03T20:32:19.497Z
Explain Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int pid; int n; for (int i = 0; i < iterations; i++) { // if (i = 0) { // int n = fork(); // if (n != 0) { // pid = getppid(); // } // } if (n != 0) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647a35f004bf4898cf568
2023-03-03T20:44:16.913Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int pid; int n; for (int i = 0; i < iterations; i++) { if (n != 0) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
the new children do not create additional processes. Only the original parent calls fork. But currently, I get one extra fork
null
63c647a35f004bf4898cf568
2023-03-03T20:50:46.768Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int pid; int n; for (int i = 0; i < iterations; i++) { if (n != 0) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
I want to make sure only the original parent calls fork.
null
63c647a35f004bf4898cf568
2023-03-03T21:24:36.724Z
Question from Code
v1
I want to make it such that only child calls fork().
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) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647a35f004bf4898cf568
2023-03-03T21:30:11.166Z
Help Fix Code
v1
null
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int n; fork(); for (int i = 0; i < iterations; i++) { if (n == 0) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
I would like to make sure only child calls fork() but now it only calls it once and terminates when i = 2. So I only get one process from the child instead of 2.
null
63c647a35f004bf4898cf568
2023-03-03T21:31:50.085Z
Question from Code
v1
I would like to make sure only child calls fork() but now it only calls it once and terminates when i = 2. So I only get one process from the child instead of 2.
int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: forkloop <iterations>\n"); exit(1); } int iterations = strtol(argv[1], NULL, 10); int n; fork(); for (int i = 0; i < iterations; i++) { if (n == 0) { n = fork(); } if (n < 0) { perror("fork"); exit(1); } printf("ppid = %d, pid = %d, i = %d\n", getppid(), getpid(), i); } return 0; }
null
null
63c647a55f004bf4898cf57c
2023-02-15T01:58:50.180Z
Question from Code
v1
what is the vaue of ptr
char *ptr;
null
null
63c647a55f004bf4898cf57c
2023-02-15T01:59:42.411Z
Question from Code
v1
what is the value of s
char s;
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:00:39.520Z
General Question
v1
logical or in c
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:18:53.144Z
General Question
v1
how to initialize a empty string?
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:20:16.416Z
Question from Code
v1
what is the value of s
char s[3] ="";
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:51:48.170Z
General Question
v1
logical not in c
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:52:27.507Z
General Question
v1
what is not in C
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:53:31.899Z
Explain Code
v1
null
bool t = not false;
null
null
63c647a55f004bf4898cf57c
2023-02-15T02:55:03.239Z
General Question
v1
is operator not different than logical operator !
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:04:54.012Z
General Question
v1
how to declare a boolean type variable
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:07:55.139Z
General Question
v1
value of false
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:08:12.748Z
General Question
v1
what type is false
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:09:33.059Z
Explain Code
v1
null
0 and 0 == false;
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:10:53.292Z
Explain Code
v1
null
0 || 1 == true;
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:11:38.902Z
General Question
v1
format specifier for boolean type
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T03:58:12.307Z
General Question
v1
what is being compared when use == to compare two pointers
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T18:30:02.047Z
General Question
v1
what are switch stateemnts
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T18:57:42.381Z
General Question
v1
what does fclose return
null
null
null
63c647a55f004bf4898cf57c
2023-02-15T19:20:13.002Z
Question from Code
v1
how to initialize user.profile_pic to an empty array
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User;
null
null
63c647a55f004bf4898cf57c
2023-02-15T19:22:02.579Z
Explain Code
v1
null
typedef struct user { char name[MAX_NAME]; char profile_pic[MAX_NAME]; struct post *first_post; struct user *friends[MAX_FRIENDS]; struct user *next; } User; User user1; char user1.profile_pic[20];
null
null