Question: Need help with asprint memory leak need help understand where leak is coming from and possible fixes Note: I did call this function and free

Need help with asprint memory leak need help understand where leak is coming from and possible fixes

Note: I did call this function and free it main but valgrind still shows error. This code basically takes in a singly linked-list with two data coeff and exp. This is basically converting a polynomial store in a linked list converted to readable string. I looking to have it dynamic allocated.

char *Poly_to_string(const Polynomial *p) { char *x = malloc(1); int size; while (p != NULL) { if((p->exp != 0) && (p->exp != 1)) { size = asprintf(&x, "%s%dx^%d + ", x, p->coeff, p->exp); if (size == -1) { exit(-1); } } else if(p->exp == 1) { size = asprintf(&x, "%s%dx + ", x, p->coeff); if (size == -1) { exit(-1); } } else if(!p->exp) { size = asprintf(&x, "%s%d + ", x, p->coeff); if (size == -1) { exit(-1); } } p = p->next; } x[strlen(x) - 3] = '\0'; return x; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!