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
⌀ |
---|---|---|---|---|---|---|---|
63c648c25f004bf4898cff72
|
2023-03-08T20:45:54.701Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[5];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
checkpasswd.c creates a new process to run the validate.c program
| null |
63c648c25f004bf4898cff72
|
2023-03-08T20:47:26.660Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[5];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
checkpasswd.c creates a new process to run the validate.c program
| null |
63c648c25f004bf4898cff72
|
2023-03-08T20:55:41.250Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){
char *arg_Ptr[4];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(./validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
checkpasswd.c creates a new process to run the validate.c program
| null |
63c648c25f004bf4898cff72
|
2023-03-08T21:33:30.242Z
|
Question from Code
|
v1
|
how to get the file path from validate.c
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){ // child
char *arg_Ptr[4];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED){
exit(WEXITSTATUS(status));
}
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-08T21:34:10.025Z
|
Question from Code
|
v1
|
how to get the file path from the file validate.c?
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){ // child
char *arg_Ptr[4];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execv(validate.c, arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED){
exit(WEXITSTATUS(status));
}
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-08T23:24:45.094Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}else if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}
int pid = fork();
if(pid == 0){ // child
char *arg_Ptr[4];
arg_Ptr[0] = "validate.c";
arg_Ptr[1] = user_id;
arg_Ptr[2] = password;
arg_Ptr[3] = NULL;
execvp("validate.c", arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
| null |
63c648c25f004bf4898cff72
|
2023-03-08T23:30:37.501Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}else if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}
int pid = fork();
if(pid == 0){ // child
char *arg_Ptr[] = {"./validate.c", user_id, password, NULL};
// char *arg_Ptr[4];
// arg_Ptr[0] = "./validate.c";
// arg_Ptr[1] = user_id;
// arg_Ptr[2] = password;
// arg_Ptr[3] = NULL;
execvp("validate.c", arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
| null |
63c648c25f004bf4898cff72
|
2023-03-09T00:20:14.020Z
|
Help Fix Code
|
v1
| null |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXLINE 256
#define MAX_PASSWORD 10
#define SUCCESS "Password verified\n"
#define INVALID "Invalid password\n"
#define NO_USER "No such user\n"
int main(void) {
char user_id[MAXLINE];
char password[MAXLINE];
/* The user will type in a user name on one line followed by a password
on the next.
DO NOT add any prompts. The only output of this program will be one
of the messages defined above.
Please read the comments in validate carefully
*/
if(fgets(user_id, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
if(fgets(password, MAXLINE, stdin) == NULL) {
perror("fgets");
exit(1);
}
// TODO
if(strlen(user_id) > MAX_PASSWORD){
printf("user_id too long"); // user_id too long
exit(1);
}else if(strlen(password) > MAX_PASSWORD){
printf("password too long");
exit(1); // password too long
}
int pid = fork();
if(pid == 0){ // child
char *arg_Ptr[] = {"./validate", user_id, password, NULL};
// int dup2(int "validate.c", int newfd);
execvp(arg_Ptr[0], arg_Ptr);
}
int status;
if (wait(&status) >= 0){
if (WEXITED){
exit(WEXITSTATUS(status));
}
}
return 0;
}
|
reads a user id and password from stdin, creates a new process to run the validate program, sends it the user id and password, and prints a message to stdout reporting whether the validation is successful.
| null |
63c648c25f004bf4898cff72
|
2023-03-14T05:09:40.316Z
|
Question from Code
|
v2
|
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position C?
|
int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
// POSITION A
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
// POSITION B
x += 2;
// POSITION C
fprintf(stderr, "outside %d", x);
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-14T05:13:14.789Z
|
Question from Code
|
v2
|
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position B?
|
int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
// POSITION A
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
// POSITION B
x += 2;
// POSITION C
fprintf(stderr, "outside %d", x);
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-14T05:16:20.257Z
|
Question from Code
|
v2
|
What does the program print to stderr if the user presses Ctrl+C at the moment when the program is at position C?
|
int x = 5;
void handler(int sig) {
x += 3;
fprintf(stderr, "inside %d ", x);
}
int main() {
fprintf(stderr, "start ");
// POSITION A
struct sigaction act;
act.sa_handler = handler;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
// POSITION B
x += 2;
// POSITION C
fprintf(stderr, "outside %d", x);
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-15T15:26:08.406Z
|
Help Write Code
|
v2
| null | null | null |
fgets skip empty line
|
63c648c25f004bf4898cff72
|
2023-03-15T16:10:30.006Z
|
General Question
|
v2
|
what is the value of fgets when it reads an empty line
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-15T16:13:19.904Z
|
General Question
|
v2
|
what is the value of fgets when it reads a tab
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-15T16:25:08.698Z
|
General Question
|
v2
|
does the value of fgets have a null terminator at the end
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:18:38.628Z
|
Help Fix Code
|
v2
|
In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
|
/* The purpose of this program is to practice writing signal handling
* functions and observing the behaviour of signals.
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
/* Message to print in the signal handling function. */
#define MESSAGE "%ld reads were done in %ld seconds.\n"
/* Global variables to store number of read operations and seconds elapsed.
*/
long num_reads, seconds;
/* The first command-line argument is the number of seconds to set a timer to run.
* The second argument is the name of a binary file containing 100 ints.
* Assume both of these arguments are correct.
*/
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_CUR);
fread(&read_num, sizeof(int), 1, fp);
printf("%d", read_num);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:20:19.410Z
|
Help Fix Code
|
v2
|
In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
|
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_CUR);
fread(&read_num, sizeof(int), 1, fp);
printf("%d", read_num);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:24:39.382Z
|
Help Fix Code
|
v2
|
In an infinite loop, read an int from a random location in the file, and print it to stderr.
|
for (;;) {
int offset = rand() % 100;
int read_num[5];
fseek(fp, offset, SEEK_CUR);
fread(&read_num, sizeof(int), 1, fp);
fprintf(sterr, "%d", read_num);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:27:41.933Z
|
Help Fix Code
|
v2
|
In an infinite loop, read an int from a random location in the file, and print it to stderr.
|
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_CUR);
fread(&read_num, sizeof(int), 1, fp);
fprintf(sterr, "%d", read_num);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:38:21.245Z
|
Help Write Code
|
v2
| null | null | null |
Now, use sigactionLinks to an external site. to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF.
|
63c648c25f004bf4898cff72
|
2023-03-17T20:45:38.614Z
|
Help Fix Code
|
v2
|
Now, use sigactionLinks to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF.
|
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
struct sigaction newact;
newact.sa_handler = handler;
newact.sa_flags = 0;
sigemptyset(&newact.sa_mask);
sigaction(SIGINT, &newact, NULL);
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_SET);
fread(&read_num, sizeof(int), 1, fp);
fprintf(stderr, "%d", read_num);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:47:40.599Z
|
Help Fix Code
|
v2
|
use sigactionLinks to add a signal handler to your program. Start with something simple that just prints a message to standard out and exits (with termination code 0) when it receives a SIGPROF.
|
void handler(int sig){
printf("SIGPROF received");
exit(0);
}
int main(int argc, char **argv) {
struct sigaction newact;
newact.sa_handler = handler;
newact.sa_flags = 0;
sigemptyset(&newact.sa_mask);
sigaction(SIGINT, &newact, NULL);
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_SET);
fread(&read_num, sizeof(int), 1, fp);
fprintf(stderr, "%d", read_num);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T20:58:29.546Z
|
Help Write Code
|
v2
| null | null | null |
Add code to set up a timer (using setitimer) for s seconds (where s is obtained from the command line). You shouldn't use real time, since your results will be affected if your system is busy. Instead, use ITIMER_PROF to make the itimer send a SIGPROF
|
63c648c25f004bf4898cff72
|
2023-03-17T21:23:26.221Z
|
Help Fix Code
|
v2
|
Add code to set up a timer (using setitimer) for s seconds (where s is obtained from the command line). You shouldn't use real time, since your results will be affected if your system is busy. Instead, use ITIMER_PROF to make the itimer send a SIGPROF
|
struct itimerval it_val;
struct timeval interval_next;
struct timeval interval_current;
interval_next.tv_sec = 0;
interval_next.tv_usec = 0;
interval_current.tv_sec = seconds;
interval_current.tv_usec = 0;
it_val.it_interval = interval_next;
it_val.it_value = interval_current;
setitimer(ITIMER_PROF, &it_val, NULL);
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T21:29:53.347Z
|
Help Fix Code
|
v2
|
Add code to set up a timer (using setitimer) for s seconds (where s is obtained from the command line). You shouldn't use real time, since your results will be affected if your system is busy. Instead, use ITIMER_PROF to make the itimer send a SIGPROF
|
/* The purpose of this program is to practice writing signal handling
* functions and observing the behaviour of signals.
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
/* Message to print in the signal handling function. */
#define MESSAGE "%ld reads were done in %ld seconds.\n"
/* Global variables to store number of read operations and seconds elapsed.
*/
long num_reads, seconds;
void handler(int sig){
fprintf(stdout, MESSAGE, num_reads, seconds);
exit(0);
}
/* The first command-line argument is the number of seconds to set a timer to run.
* The second argument is the name of a binary file containing 100 ints.
* Assume both of these arguments are correct.
*/
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
struct sigaction newact;
newact.sa_handler = handler;
newact.sa_flags = 0;
sigemptyset(&newact.sa_mask);
sigaction(SIGPROF, &newact, NULL);
struct itimerval it_val;
struct timeval interval_next;
struct timeval interval_current;
interval_next.tv_sec = 0;
interval_next.tv_usec = 0;
interval_current.tv_sec = seconds;
interval_current.tv_usec = 0;
it_val.it_interval = interval_next;
it_val.it_value = interval_current;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("setitimer");
exit(1);
}
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_SET);
fread(&read_num, sizeof(int), 1, fp);
fprintf(stderr, "%d", read_num);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-17T21:33:12.671Z
|
Help Fix Code
|
v2
|
Add code to set up a timer (using setitimer) for s seconds (where s is obtained from the command line). You shouldn't use real time, since your results will be affected if your system is busy. Instead, use ITIMER_PROF to make the itimer send a SIGPROF
|
/* The purpose of this program is to practice writing signal handling
* functions and observing the behaviour of signals.
*/
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
/* Message to print in the signal handling function. */
#define MESSAGE "%ld reads were done in %ld seconds.\n"
/* Global variables to store number of read operations and seconds elapsed.
*/
long num_reads, seconds;
void handler(int sig){
fprintf(stdout, MESSAGE, num_reads, seconds);
exit(0);
}
/* The first command-line argument is the number of seconds to set a timer to run.
* The second argument is the name of a binary file containing 100 ints.
* Assume both of these arguments are correct.
*/
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: time_reads s filename\n");
exit(1);
}
seconds = strtol(argv[1], NULL, 10);
FILE *fp;
if ((fp = fopen(argv[2], "r")) == NULL) {
perror("fopen");
exit(1);
}
struct sigaction newact;
newact.sa_handler = handler;
newact.sa_flags = 0;
sigemptyset(&newact.sa_mask);
sigaction(SIGPROF, &newact, NULL);
struct itimerval it_val;
struct timeval interval_next;
struct timeval interval_current;
interval_next.tv_sec = 0;
interval_next.tv_usec = 0;
interval_current.tv_sec = seconds;
interval_current.tv_usec = 0;
it_val.it_interval = interval_next;
it_val.it_value = interval_current;
if (setitimer(ITIMER_PROF, &it_val, NULL) == -1) {
perror("setitimer");
exit(1);
}
/* In an infinite loop, read an int from a random location in the file,
* and print it to stderr.
*/
for (;;) {
int offset = rand() % 100;
int read_num;
fseek(fp, offset, SEEK_SET);
fread(&read_num, sizeof(int), 1, fp);
fprintf(stderr, "%d", read_num);
}
return 1; // something is wrong if we ever get here!
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-21T02:31:40.014Z
|
General Question
|
v2
|
If x is set to 77 (0100 1101 in binary), what is the value of x << 2 in binary?
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-21T02:54:34.491Z
|
General Question
|
v2
|
Which function would you use to find the IP address of a machine, given its name?
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:15:10.495Z
|
General Question
|
v2
|
how to update inbuf
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:19:00.113Z
|
Question from Code
|
v2
|
am I updating the inbuf right?
|
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "socket.h"
#ifndef PORT
#define PORT 30000
#endif
#define BUFSIZE 30
int find_network_newline(const char *buf, int n);
int main() {
// This line causes stdout not to be buffered.
// Don't change this! Necessary for autotesting.
setbuf(stdout, NULL);
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
// Receive messages
char buf[BUFSIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buffer?
int room = sizeof(buf); // How many bytes remaining in buffer?
char *after = buf; // Pointer to position after the data in buf
int nbytes;
while ((nbytes = read(fd, after, room)) > 0) {
// Step 1: update inbuf (how many bytes were just added?)
inbuf = BUFSIZE - room;
int where;
}
close(fd);
printf("The connection is now closed ...\n");
}
free(self);
close(listenfd);
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:20:14.242Z
|
Question from Code
|
v2
|
am I returning the correct value?
|
int find_network_newline(const char *buf, int n) {
for (int i = 0; i < (n - 1); i++) {
if (buf[i] == '\r' && buf[i + 1] == '\n') {
return 1 + (i + 1);
}
}
return -1;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:22:01.985Z
|
Question from Code
|
v2
|
am I printing the correct value, where the print statments Output the full line, not including the "\r\n"
|
while ((where = find_network_newline(buf, inbuf)) > 0) {
// where is now the index into buf immediately after
// the first network newline
// Step 3: Okay, we have a full line.
// Output the full line, not including the "\r\n",
// using print statement below.
// Be sure to put a '\0' in the correct place first;
// otherwise you'll get junk in the output.
buf[where - 1] = '\0';
printf("Next message: %s\n", buf);
| null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:23:15.116Z
|
Help Fix Code
|
v2
|
Output the full line, not including the "\r\n"
|
while ((where = find_network_newline(buf, inbuf)) > 0) {
// where is now the index into buf immediately after
// the first network newline
// Step 3: Okay, we have a full line.
// Output the full line, not including the "\r\n",
// using print statement below.
// Be sure to put a '\0' in the correct place first;
// otherwise you'll get junk in the output.
buf[where-1] = '\0';
printf("Next message: %s\n", buf);
| null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:38:34.427Z
|
Question from Code
|
v2
|
am i updating inbuf and removing the full line from the buffer right?
|
#define BUFSIZE 30
int find_network_newline(const char *buf, int n);
int main() {
setbuf(stdout, NULL);
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
// Receive messages
char buf[BUFSIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buffer?
int room = sizeof(buf); // How many bytes remaining in buffer?
char *after = buf; // Pointer to position after the data in buf
int nbytes;
while ((nbytes = read(fd, after, room)) > 0) {
inbuf += nbytes;
int where;
while ((where = find_network_newline(buf, inbuf)) > 0) {
buf[where - 1] = '\0';
printf("Next message: %s\n", buf);
// Step 4: update inbuf and remove the full line from the buffer
// There might be stuff after the line, so don't just do inbuf = 0.
// You want to move the stuff after the full line to the beginning
// of the buffer. A loop can do it, or you can use memmove.
// memmove(destination, source, number_of_bytes)
int number_of_bytes = strlen(buf) - inbuf;
char move_this[number_of_bytes];
int move_this_index = 0;
int new_after_index = 0;
int after_after = -1;
for (int i = (where + 1); i < strlen(buf); i++) {
if (buf[i] == &after) {
after_after = 0;
}
if (after_after == -1) {
new_after_index += 1;
}
move_this[move_this_index] = buf[i];
move_this_index += 1;
}
memmove(buf, move_this, number_of_bytes);
inbuf = strlen(buf) - new_after_index;
}
}
close(fd);
printf("The connection is now closed ...\n");
}
free(self);
close(listenfd);
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-24T19:43:41.430Z
|
Question from Code
|
v2
|
am I updating after and room correctly?
|
#define BUFSIZE 30
int find_network_newline(const char *buf, int n);
int main() {
// This line causes stdout not to be buffered.
// Don't change this! Necessary for autotesting.
setbuf(stdout, NULL);
struct sockaddr_in *self = init_server_addr(PORT);
int listenfd = set_up_server_socket(self, 5);
while (1) {
int fd = accept_connection(listenfd);
if (fd < 0) {
continue;
}
// Receive messages
char buf[BUFSIZE] = {'\0'};
int inbuf = 0; // How many bytes currently in buffer?
int room = sizeof(buf); // How many bytes remaining in buffer?
char *after = buf; // Pointer to position after the data in buf
int nbytes;
while ((nbytes = read(fd, after, room)) > 0) {
inbuf += nbytes;
int where;
while ((where = find_network_newline(buf, inbuf)) > 0) {
buf[where - 1] = '\0';
printf("Next message: %s\n", buf);
int number_of_bytes = inbuf - (where + 1);
char move_this[number_of_bytes];
memmove(buf, &buf[where + 1], number_of_bytes);
inbuf = number_of_bytes;
}
// Step 5: update after and room, in preparation for the next read.
after = buf[strlen(buf) - inbuf];
room = strlen(buf) - inbuf;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-28T03:39:09.172Z
|
General Question
|
v2
|
shell programming, Variables width and height have been initialized. Calculate the area of a rectangle using width and height, and assign the resulting value to a variable named area.
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-28T03:55:25.973Z
|
General Question
|
v2
|
man page for test
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-28T04:02:35.717Z
|
General Question
|
v2
|
If a command produces output that we want to discard, to which file do we redirect it? Provide the complete path.
| null | null | null |
63c648c25f004bf4898cff72
|
2023-03-28T17:38:48.938Z
|
Question from Code
|
v2
|
Task 1: The task is to There are now two different types of input that we could receive: a username, or a message. If we have not yet received the username, then we should copy buf to username. Otherwise, the input will be a message to write to the socket.
How do I write to the socket?
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username = NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-28T20:24:22.817Z
|
Question from Code
|
v2
|
Task 1: There are now two different types of input that we could receive: a username, or a message. If we have not yet received the username, then we should copy buf to username. Otherwise, the input will be a message to write to the socket.
Did I complete task 1 properly?
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username = NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-28T20:26:15.928Z
|
Question from Code
|
v2
|
Task 1: There are now two different types of input that we could receive: a username, or a message. If we have not yet received the username, then we should copy buf to username. Otherwise, the input will be a message to write to the socket. Did I complete task 1 properly?
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-28T20:27:05.804Z
|
Question from Code
|
v2
|
How do I change the code to broadcast to all connected clients
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:15:44.450Z
|
Question from Code
|
v2
|
What part of the code do I have to change to broadcast to all connected clients?
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:23:32.817Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if users
[client_index].sock_fd == -1 : {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:26:14.054Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS - 1; i++) {
if (users[client_index].sock_fd == -1 && client_index != i) {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:27:36.396Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS - 1; i++) {
if (users[client_index].sock_fd != -1 && client_index != i) {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:30:26.177Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[client_index].sock_fd != -1 && client_index != i) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:34:36.468Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[client_index].sock_fd != -1 && client_index != i) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:37:51.298Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1 && client_index != i) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:42:56.942Z
|
Question from Code
|
v2
|
did i correctly change the code to broadcast to all connected clients?
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1 && client_index != i) {
int num_written = write(users[i].sock_fd, buf, BUF_SIZE);
if (num_written != BUF_SIZE) {
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:44:34.532Z
|
Help Fix Code
|
v2
|
Task 2: Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL){
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
}else{
for (int i = 0; i < MAX_CONNECTIONS; i++){
if (users[i].sock_fd != -1 && client_index != i){
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)){
printf("Error");
return -1;
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:47:08.288Z
|
Question from Code
|
v2
|
Task 1: There are now two different types of input that we could receive: a username, or a message. If we have not yet received the username, then we should copy buf to username. Otherwise, the input will be a message to write to the socket.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:48:13.935Z
|
Question from Code
|
v2
|
Task 2: Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1 && client_index != i) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:49:51.749Z
|
Question from Code
|
v2
|
Task 2: Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:51:55.670Z
|
Explain Code
|
v2
| null |
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1) {
int num_written = write(users[i].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:55:09.522Z
|
Help Fix Code
|
v2
|
Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL){
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
}else{
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)){
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T20:57:51.288Z
|
Question from Code
|
v2
|
Task 2: Change the code to broadcast to all connected clients
*/
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
if (broadcast(client_index, users, buf, strlen(buf)) == -1) {
return -1;
}
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:00:44.952Z
|
Question from Code
|
v2
|
Change the read_from function so that it broadcasts any message received to all connected clients.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
if (broadcast(client_index, users, buf, strlen(buf)) == -1) {
return -1;
}
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:01:12.864Z
|
Help Fix Code
|
v2
|
Change the read_from function so that it broadcasts any message received to all connected clients.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
/* In Lab 10, you focused on handling partial reads. For this lab, you do
* not need handle partial reads. Because of that, this server program
* does not check for "\r\n" when it reads from the client.
*/
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL){
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
}else{
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)){
printf("Error");
return -1;
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
if (broadcast(client_index, users, buf, strlen(buf)) == -1){
return -1;
}
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:05:21.335Z
|
Question from Code
|
v2
|
Task 2: Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1) {
write(users[i].sock_fd, buf, strlen(buf));
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:06:56.054Z
|
Question from Code
|
v2
|
did I correctly Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
int num_written = write(users[client_index].sock_fd, buf, strlen(buf));
if (num_written != strlen(buf)) {
printf("Error");
return -1;
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1 && client_index != i) {
write(users[i].sock_fd, buf, strlen(buf));
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:10:58.967Z
|
Question from Code
|
v2
|
did I correctly Change the code to broadcast to all connected clients
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
int read_from(int client_index, struct sockname *users) {
int fd = users[client_index].sock_fd;
char buf[BUF_SIZE + 1];
int num_read = read(fd, &buf, BUF_SIZE);
buf[num_read] = '\0';
if (users[client_index].username == NULL) {
users[client_index].username = malloc(sizeof(char) * (strlen(buf)));
strcpy(users[client_index].username, buf);
} else {
if (write(users[client_index].sock_fd, buf, strlen(buf)) != strlen(buf)) {
perror("client: write");
close(sock_fd);
exit(1);
}
for (int i = 0; i < MAX_CONNECTIONS; i++) {
if (users[i].sock_fd != -1 && client_index != i) {
if (write(users[i].sock_fd, buf, strlen(buf)) != strlen(buf)) {
perror("client: write");
close(sock_fd);
exit(1);
}
}
}
}
if (num_read == 0 || write(fd, buf, strlen(buf)) != strlen(buf)) {
users[client_index].sock_fd = -1;
return fd;
}
return 0;
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:28:34.475Z
|
Question from Code
|
v2
|
how to Monitor stdin and the socket using select to avoid blocking on either one.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:29:52.976Z
|
Help Fix Code
|
v2
|
Monitor stdin and the socket using select to avoid blocking on either one.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
/*
* We should really send "\r\n" too, so the server can identify partial
* reads, but you are not required to handle partial reads in this lab.
*/
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:47:01.464Z
|
Help Fix Code
|
v2
|
Your task is to update the client so it monitors just two file descriptors: the socket with the server and stdin. Whenever a message is received on either, your program should read it and pass it to the correct output stream.
|
#ifndef PORT
#define PORT 30000
#endif
#define MAX_BACKLOG 5
#define MAX_CONNECTIONS 12
#define BUF_SIZE 128
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
/*
* We should really send "\r\n" too, so the server can identify partial
* reads, but you are not required to handle partial reads in this lab.
*/
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T21:48:44.810Z
|
Help Fix Code
|
v2
|
Your task is to update the client so it monitors just two file descriptors: the socket with the server and stdin. Whenever a message is received on either, your program should read it and pass it to the correct output stream.
|
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
/*
* We should really send "\r\n" too, so the server can identify partial
* reads, but you are not required to handle partial reads in this lab.
*/
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T22:09:22.805Z
|
Help Fix Code
|
v2
|
Monitor stdin and the socket using select to avoid blocking on either one.
|
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
/*
* We should really send "\r\n" too, so the server can identify partial
* reads, but you are not required to handle partial reads in this lab.
*/
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T22:12:26.200Z
|
Help Fix Code
|
v2
|
Monitor stdin and the socket using select to avoid blocking on either one. Update the client so it monitors just two file descriptors: the socket with the server and stdin. Whenever a message is received on either, your program should read it and pass it to the correct output stream.
|
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
/*
* We should really send "\r\n" too, so the server can identify partial
* reads, but you are not required to handle partial reads in this lab.
*/
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-03-31T22:14:39.058Z
|
Question from Code
|
v2
|
Monitor stdin and the socket using select to avoid blocking on either one. Update the client so it monitors just two file descriptors: the socket with the server and stdin. Whenever a message is received on either, your program should read it and pass it to the correct output stream.
|
while (1) {
num_read = read(STDIN_FILENO, buf, BUF_SIZE);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
if (write(sock_fd, buf, num_read) != num_read) {
perror("client: write");
close(sock_fd);
exit(1);
}
num_read = read(sock_fd, buf, sizeof(buf) - 1);
if (num_read == 0) {
break;
}
buf[num_read] = '\0';
printf("[Server] %s", buf);
}
| null | null |
63c648c25f004bf4898cff72
|
2023-04-04T03:43:05.315Z
|
General Question
|
v2
|
Consider the sh script shifter.sh:
echo I live at $1 $2 $3.
shift
echo Now, I live at $0 $1 $2.
Suppose I run the script as follows:
sh shifter.sh 100 University Ave
What does it output?
| null | null | null |
63c648c25f004bf4898cff72
|
2023-04-04T03:51:10.425Z
|
General Question
|
v2
|
In a shell program, what variable has the number of arguments that were supplied to the program?
| null | null | null |
63c648c25f004bf4898cff72
|
2023-04-04T04:02:08.841Z
|
General Question
|
v2
|
Consider the follow code:
subject=CSC
Using subject, write a command to output: Last week of CSC209
| null | null | null |
63c648c25f004bf4898cff72
|
2023-04-04T04:08:08.712Z
|
Question from Code
|
v2
|
Using subject, write a command to output: Last week of CSC209 for shell programming
|
subject = CSC
| null | null |
63c648c55f004bf4898cff90
|
2023-02-10T14:30:42.497Z
|
General Question
|
v1
|
How to allocate memory in heap?
| null | null | null |
63c648c55f004bf4898cff90
|
2023-02-11T01:44:45.659Z
|
Help Fix Code
|
v1
| null |
struct pixel read_pixel_array(FILE *image, int pixel_array_offset, int width, int height) {
//allocate rows
struct pixel** rows = malloc(height * sizeof(struct pixel*));
if (rows == NULL){
exit(1);
}
//alocate each row
for (int i=0; i<height; i++) {
rows[i] = malloc(width * sizeof(struct pixel));
if (rows[i] == NULL){
exit(1);
}
}
//read into memory
fseek(image, pixel_array_offset, SEEK_SET);
for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
unsigned char r;
unsigned char g;
unsigned char b;
fread(&b, sizeof(char), 1, image);
fread(&g, sizeof(char), 1, image);
fread(&r, sizeof(char), 1, image);
struct pixel pix;
pix.blue = b;
pix.green = g;
pix.red = r;
rows[i][j] = pix;
}
}
return rows;
}
|
return an array of arrays of pixels
| null |
63c648c55f004bf4898cff90
|
2023-02-14T17:43:10.202Z
|
Help Fix Code
|
v1
| null |
if (((*user_ptr_add)[0]) == NULL){
}
|
check if the first element of the array pointed to by user_ptr_add is empty
| null |
63c648c55f004bf4898cff90
|
2023-02-14T18:39:03.174Z
|
Help Fix Code
|
v1
| null |
user_pt->name = malloc(sizeof(char)*32);
|
Allocate memory for the name of struct pointed to by user_pt in the heap
| null |
63c648c55f004bf4898cff90
|
2023-02-14T18:40:14.450Z
|
Help Fix Code
|
v1
| null |
user_pt->name = malloc(sizeof(char)*32);
|
allocate space in heap for the name field of the struct pointed to by user_pt
| null |
63c648c55f004bf4898cff90
|
2023-02-14T19:08:19.311Z
|
Explain 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;
| null | null |
63c648c55f004bf4898cff90
|
2023-02-14T19:09:49.534Z
|
Question from Code
|
v1
|
Why is User on line 7 capitalised?
|
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 |
63c648c55f004bf4898cff90
|
2023-02-14T19:12:07.302Z
|
Question from Code
|
v1
|
Why do we use typedef here?
|
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 |
63c648c55f004bf4898cff90
|
2023-02-14T23:55:08.871Z
|
General Question
|
v1
|
how to check if an array element is empty?
| null | null | null |
63c648c55f004bf4898cff90
|
2023-02-15T02:04:39.151Z
|
Help Write Code
|
v1
| null | null | null |
Read file line by line
|
63c648c55f004bf4898cff90
|
2023-02-15T02:35:01.101Z
|
Help Write Code
|
v1
| null | null | null |
convert time_t to string
|
63c648c55f004bf4898cff90
|
2023-02-15T05:47:51.054Z
|
General Question
|
v1
|
How to use valgrind
| null | null | null |
63c648c55f004bf4898cff90
|
2023-02-15T05:54:54.855Z
|
General Question
|
v1
|
can junk values from freed heap memory interfere with values of memory allocated with malloc?
| null | null | null |
63c648c55f004bf4898cff90
|
2023-03-03T18:48:36.097Z
|
General Question
|
v1
|
What header do I need to use the wait function?
| null | null | null |
63c648c55f004bf4898cff90
|
2023-03-03T18:49:06.724Z
|
General Question
|
v1
|
How do I use wait
| null | null | null |
63c648c55f004bf4898cff90
|
2023-03-10T23:20:28.105Z
|
Help Fix Code
|
v2
|
write 10 bytes of each password and user_id to pipe
|
for (int i=0; i<MAXPASSWORD; i++){
if (write(fd[1], user_id+i, 1) == -1){
perror("write");
exit(1);
}
}
for (int i=0; i<MAXPASSWORD; i++){
if (write(fd[1], password+i, 1) == -1){
perror("write");
exit(1);
}
}
| null | null |
63c648c55f004bf4898cff90
|
2023-03-10T23:21:41.793Z
|
General Question
|
v2
|
How do I write to a pipe twice?
| null | null | null |
63c648c55f004bf4898cff90
|
2023-03-14T17:08:27.379Z
|
General Question
|
v2
|
How do I split a string up
| null | null | null |
63c648c55f004bf4898cff90
|
2023-03-15T13:05:56.149Z
|
Explain Code
|
v2
| null |
int (*complex(int index))(char *s, int l);
| null | null |
63c648c65f004bf4898cff95
|
2023-01-17T15:10:22.245Z
|
General Question
|
v1
|
what is a seg fault??
| null | null | null |
63c648c65f004bf4898cff95
|
2023-01-17T15:31:25.689Z
|
Question from Code
|
v1
|
what object does this line of code create?
|
char *result[2];
| null | null |
63c648c65f004bf4898cff95
|
2023-01-17T15:41:16.761Z
|
Question from Code
|
v1
|
what does this code output
|
int x;
printf('\d', x);
| null | null |
63c648c65f004bf4898cff95
|
2023-01-17T15:41:52.306Z
|
Question from Code
|
v1
|
what does this code output
|
int x;
printf("\d", x);
| null | null |
63c648c65f004bf4898cff95
|
2023-01-17T15:48:38.929Z
|
Question from Code
|
v1
|
what is the type of the variable y
|
char *result[2];
x = result[0];
// some hidden code
result[0] = "read only";
y = x[0];
| null | null |
63c648c65f004bf4898cff95
|
2023-01-25T06:05:49.310Z
|
Question from Code
|
v1
|
why does this code produce a segmentation fault?
|
void fib(int **sequence, int num_elements) {
*sequence = malloc(sizeof(int) * num_elements);
*sequence[0] = 0;
if (num_elements >= 1) {
*sequence[1] = 1;
for (int i = 2; i < num_elements; i++) {
*sequence[i] = *sequence[i - 2] + *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;
}
| null | null |
63c648c65f004bf4898cff95
|
2023-01-25T06:15:31.101Z
|
Question from Code
|
v1
|
the code results in a segmentation fault, why did it take place?
|
void fib(int **sequence, int num_elements) {
*sequence = malloc(sizeof(int) * num_elements);
*sequence[0] = 0;
if (num_elements >= 1) {
*sequence[1] = 1;
for (int i = 2; i < num_elements; i++) {
*sequence[i] = *sequence[i - 2] + *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;
}
| null | null |
63c648c65f004bf4898cff95
|
2023-01-25T06:19:26.356Z
|
Help Fix Code
|
v1
| null |
void fib(int **sequence, int n) {
*sequence = malloc(sizeof(int) * n);
*sequence[0] = 0;
if (n >= 1) {
*sequence[1] = 1;
for (int i = 2; i < n; i++) {
*sequence[i] = *sequence[i - 2] + *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;
}
|
code takes in an int n, and generates and prints out the first n elements of the Fibonacci sequence.
| null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.