Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with these 5 questions (9-13) Thank you very much This and the follo

ID: 3886143 • Letter: P

Question

Please help with these 5 questions (9-13) Thank you very much

This and the following questions provide code and you have to answer whether or not there is a bug in that code If yes, give the number of the line where the bug is If there are bugs in more than one line or the code is wrong because of a combination of lines, list all these line numbers in increasing order, separated with a comma and with no extra spaces (e.g. 1 If the code is wrong because there is a memory leak (allocated memory was not freed) answer with the word leak If the code is correct (no bug), answer with the word none Example of correct answer formats: 1,5,6,7 one leak Examples of incorrect answer formats 5,6,7,1 (not in increasing order) 1567 (separated by spaces, not by commas) intmem 1 in(änt x) line 1 / line 2 / line 3 return x; void mem_1) i // line 4 // line 5 int x = 10; int * = mem 1 in (x) ; printf("+p = %d ". *p); // line 6 Iine // 7 QUESTION 10 / Allocate dynamic memory, set it to 25, return the pointer to it. int mem 31 line1 // line 2 int *p = 25; print f("p=ed " , return Pi //line 3 +P ); // line 4 Give the line(s) with bugs or none

Explanation / Answer

9.

int* mem_1_in(int x){   //Function takes an integer x, and returns an integer address as output.
   x = x + 2;           //x value is increased by 2.
   return &x;           //Address of x is returned.
                       //Here x is local(stack) variable, and returning this address will result in memory leak.
}

void mem_1() {           //Function takes no input, and returns nothing.
   int x = 10;           //x is an integer variable declared and initialized to 10.
   int *p = mem_1_in(x);   //p, an integer pointer, and is supposed to hold the return address from mem_1_in() function.
   printf("*p = %d ", *p);   //Prints the value at address p.
}
//So, the answer for this is: leak

10.

#include <stdio.h>
int* mem_3() {   //Function takes no input, and returns an integer pointer.
   int *p = 25;   //Here p is an integer pointer which is not initialized, but is trying to store a value of 25 in the addressed location, which results in memory leak.
   printf("p = %d ", *p);   //Prints the value at address p.
   return p;       //Returns the address in p.
}

11.

void mem_4() {   //Function takes no input, and returns nothing.
   double d;   //d is declared as type double variable.
   double *p;   //p is declared as double pointer type variable.
   p = &d;       //p is assigned the address of d.
   *p = 7;       //The value at address p, i.e., the value in variable d is updated to 7.
}
//Everything works fine here.
//So, the answer for this is: none

12.

void mem_5() {       //Function takes no input, and returns nothing.
   int* p;           //p is declared as integer pointer.
   p = (int*) malloc(sizeof(int));   //An memory space for integer is assigned, and the address is stored in p.
   p = 7;       //p is a pointer variable, but you're trying to store the value 7 into it.
   free(p);   //p is a pointer variable and can be freed, once assigned. This causes memory leak.
}
//So, the answer is: leak