224k views
2 votes
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: int main() {
5:char *word1 = NULL;
6: word1 = malloc(sizeof(char) * 11);
7: word1 = "bramble";
8: char *word2 = NULL:
9: word2 = malloc(sizeof(char) * 11);
10: word2 = word1;
11: return 0;
12: }

1 Answer

1 vote

Answer:

The description of the given code is given below:

Step-by-step explanation:

Memory leak:

In a program, if memory is assigned to the pointer variables but that memory is already released previously then the condition of occurring the memory leakage.

Now the program :

#include <stdio.h> //header file

#include <stdlib.h> //header file

#include <string.h> //header file

int main() //main function

{

char *word1 = NULL; //line 1 (as given in the question)

word1 = malloc(sizeof(char) * 11); //line 2

free(word1); //free the memory of pointer word1 line 3

word1 = "bramble"; //line 4

char *word2 = NULL; //line 5

word2 = malloc(sizeof(char) * 11); //line 6

free(word2); //free the memory of pointer word2 line 7

word2 = word1; //line 8

return 0; //line 9

}

Therefore, line 3 is the first line that introduce a memory leak anf after that line 7 introduce it in a program.

User HMLDude
by
8.5k points