Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored 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