Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

Identify specific IT applications for HR planning.

Answered: 1 week ago