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

In the following code, what is the first line that introduces a memory leak into

ID: 3742929 • Letter: I

Question

In the following code, what is the first line that introduces a memory leak into the program? (Type the line number into the box below.)

1: #include

2: #include

3: #include

4:

5: int main(void)

6: {

7: char *word1 = NULL;

8: char *word2 = NULL;

9:

10: word1 = malloc(sizeof(char) * 10);

11: word2 = malloc(sizeof(char) * 12);

12:

13: word1 = "bramble";

14: word2 = word1;

15: word1 = malloc(sizeof(char) * 10);

16:

17: word1 = NULL;

18: word2 = NULL;

19:

20: return 0;

21: }

it's not at line 14 or 20.

Explanation / Answer

#include #include #include int main(void) { char* word1 = NULL; char* word2 = NULL; word1 = malloc(sizeof(char) * 10); word2 = malloc(sizeof(char) * 12); free(word1); // since you are modifying word1, first free the space given to word1 word1 = "bramble"; free(word2); // since you are modifying word2, first free the space given to word2 word2 = word1; word1 = malloc(sizeof(char) * 10); free(word1); // since you are modifying word1, first free the space given to word1 word1 = NULL; word2 = NULL; return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote