analyze each program and figure out what is wrong with the memory management in the code and how to fix the code.
#include #include #include #include // Hint: If you run this program repeatedly, with ever-larger numbers as input,// eventually something (slightly) unexpected should happen.intmain(int argc, char *argv[]){if (argc != 2) {fprintf(stderr, "usage: broken2 \n");exit(1);}int size = atoi(argv[1]);int *p;p = malloc(size*sizeof(int));for (int i = 0 ; i < size;="" i++)="">p[i] = i;}int *q;q = malloc(sizeof(p));for (int i = 0 ; i < size;="" i++)="">q[i] = p[i];}free(p);free(q);return 0;}
// Hint: If you run this program repeatedly, with ever-larger numbers as input,// eventually something (slightly) unexpected should happen.intmain(int argc, char *argv[]){if (argc != 2) {fprintf(stderr, "usage: broken2 \n");exit(1);}int size = atoi(argv[1]);int *p;p = malloc(size*sizeof(int));for (int i = 0 ; i < size;="" i++)="">p[i] = i;}int *q;q = malloc(sizeof(p));for (int i = 0 ; i < size;="" i++)="">q[i] = p[i];}free(p);free(q);return 0;}
free(p);free(q);return 0;}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here