Question: There is a memory management issue in the following main function. Please explain the memory management issues in this code and explain the reason behind
There is a memory management issue in the following main function. Please explain the memory management issues in this code and explain the reason behind this.
int main(){ char *p1 = malloc(20); char *p2 = malloc(30); strcpy(p1, "cat"); p2 = p1; printf("%s", p1); free(p1); free(p2); return 0;}
Step by Step Solution
3.42 Rating (152 Votes )
There are 3 Steps involved in it
Given program is int main allocates 20 bytes of data in heap and returns pointer to first memory loc... View full answer
Get step-by-step solutions from verified subject matter experts
