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

1 Expert Approved Answer
Step: 1 Unlock

Given program is int main allocates 20 bytes of data in heap and returns pointer to first memory loc... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Engineering Questions!