Question
Consider the statements in lines 4 and 5 are written in different lines. Due to saving spaces they are written in the same line. 1.
Consider the statements in lines 4 and 5 are written in different lines. Due to saving spaces they are written in the same line.
1. void main( ) {
2 char *str1 = malloc(sizeof(char) * 16);
3 char *str2 = malloc(sizeof(char) * 16);
4 str1[0] = 'p'; str1[1] = 'a'; str1[2] = 's'; str1[3] = 's';
5 str1[4] = ','; str1[5] = '\0';
6 printf("%s ", str1);
7 Str2 = str1;
8 str2[0] = 'y';
9 str2[1] = 'e';
10 printf("%s ", str1);
11 printf("%s ", str2);
12 str2 = NULL;
13 printf("%s ", str1);
14 strcpy(str1, "pass the exam!");
15 printf("%s ", str1);
16 free(str1);
17 free(str2);
}
- Draw the status of the memory after line 7. Show where Str1 and Str2 are pointing and draw rectangular array boxes as needed to show the dynamically allocated memory status.
- There is a memory leak on line 7. Explain why?
- What will be the output of the program?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started