ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

The following function is from a program to add two polynomials.

Code:

int* add(int A[], int B[], int m, int n)
{
int* sum = malloc(sizeof(int));
/* Initialize the sum polynomial */
for (int i = 0; i < m; i++)
sum[i] = A[i];
/*Take every term of first polynomial */
for (int i = 0; i < n; i++)
sum[i] += B[i];
return sum;
*/free() here??? */
}

I can not work out where to use free(). If I put free(sum) after return sum, and the program compiles and adds two polynomials. However splint tells me:

Code:

poly_add.c:26:10: Dead storage sum passed as out parameter to free: sum
Memory is used after it has been released (either by passing as an only param
or assigning to an only global). (Use -usereleased to inhibit warning)
poly_add.c:25:12: Storage sum released
poly_add.c:26:5: Unreachable code: free(sum)
This code will never be reached on any possible execution. (Use -unreachable
to inhibit warning)

But freeing the memory before the function return does not seem correct.

Last edited by amikoyan; 02-26-2023 at 05:44 AM.
Reason: missing words

The caller of add() needs to call free(), because the caller will be using the malloc'd return value.

Furthermore, your allocation is too small, you should have

Code:

int* sum = malloc(max(n, m) * sizeof(int));
// where max something like
int max(int n, int m) { if (n > m) return n; else return m; }
// or
#define max(n, m) (((n) > (m))? (n) : (m))

yes, the usual way is to malloc before calling this function and you need to pass this variable to the function too, so you will never need to malloc/free inside.
The other way is a bit different, you need to use and return a struct, not int *, in that case this will be solved automatically. And the struct should contain int[N]. where N is the maximal size of A and B.

The caller of add() needs to call free(), because the caller will be using the malloc'd return value.

Furthermore, your allocation is too small, you should have

Code:

int* sum = malloc(max(n, m) * sizeof(int));
// where max something like
int max(int n, int m) { if (n > m) return n; else return m; }

Thank you ntubski, I corrected to this.

Code:

int max(int m, int n) { return (m > n) ? m : n; }
/*A[] represents coefficients of first polynomial
* B[] represents coefficients of second polynomial
* m and n are sizes of A[] and B[] respectively */
int* add(int A[], int B[], int m, int n)
{
int* sum = malloc(max(n, m) * sizeof(int));

int max(int m, int n) { return (m > n) ? m : n; }
/*A[] represents coefficients of first polynomial
* B[] represents coefficients of second polynomial
* m and n are sizes of A[] and B[] respectively */
int* add(int A[], int B[], int m, int n)
{
int* sum = malloc(max(n, m) * sizeof(int));

Again, this is the same problem. You cannot free this in the function add, therefore either you need to malloc outside, not in the function add or implement it in another way.

Code:

int *sum = malloc(max(n, m) * sizeof(int));
call add
do whatever you want with sum
free(sum);
void add(int A[], int B[], int m, int n, int *sum)
/* Initialize the sum polynomial */
for (int i = 0; i < m; i++)
sum[i] = A[i];
/*Take every term of first polynomial */
for (int i = 0; i < n; i++)
sum[i] += B[i];
}

By the way, this function is incorrect, will not work properly if m < n.

yes, the usual way is to malloc before calling this function and you need to pass this variable to the function too, so you will never need to malloc/free inside.

This took me a bit longer, but

Code:

int* sum1 = malloc(max(n, m) * sizeof(int));

is now in main() not int* add(). I made some poor choices with variable names and had to rename sum to sum1 because I also used sum in main(). This confused me and caused quite a hold up.

Quote:

The other way is a bit different, you need to use and return a struct, not int *, in that case this will be solved automatically. And the struct should contain int[N]. where N is the maximal size of A and B.

I will try this with a struct later. Probably another day - my brain is full!

Again, this is the same problem. You cannot free this in the function add, therefore either you need to malloc outside, not in the function add or implement it in another way.

Code:

int *sum = malloc(max(n, m) * sizeof(int));
call add
do whatever you want with sum
free(sum);

I was busy posting my last reply and didn't see this. I have used malloc outside the function now.

Quote:

Code:

void add(int A[], int B[], int m, int n, int *sum)
/* Initialize the sum polynomial */
for (int i = 0; i < m; i++)
sum[i] = A[i];
/*Take every term of first polynomial */
for (int i = 0; i < n; i++)
sum[i] += B[i];
}

By the way, this function is incorrect, will not work properly if m < n.

Sigh - so my work is not finished today. I will think some more

Because addition does not care which way you do it (6 + 3 is the same as 3 + 6), a property called 'commutative' - so google tells me. I added this if...else

Code:

/* Initialize the product polynomial */
if (m >= n) {
for (int i = 0; i < m; i++)
sum1[i] = A[i];
/*Take every term of first polynomial */
for (int i = 0; i < n; i++)
sum1[i] += B[i]; }
else {
for (int i = 0; i < m; i++)
sum1[i] = B[i];
/*Take every term of first polynomial */
for (int i = 0; i < n; i++)
sum1[i] += A[i];
}
return sum1;
}

You could also considering swapping the polynomials if m is smaller:

Code:

if (m < n) {
int tlen = m;
m = n;
n = tlen;
int* tPoly = B;
B = A;
A = tPoly;
}
for (int i = 0; i < m; i++)
sum1[i] = A[i];
for (int i = 0; i < n; i++)
sum1[i] += B[i];

You could also considering swapping the polynomials if m is smaller:

Code:

if (m < n) {
int tlen = m;
m = n;
n = tlen;
int* tPoly = B;
B = A;
A = tPoly;
}
for (int i = 0; i < m; i++)
sum1[i] = A[i];
for (int i = 0; i < n; i++)
sum1[i] += B[i];

Sorry ntubski for the long delay in responding to your suggestion, it has been a busy month.

I like your suggestion and will experiment with it in my code. Thanks for the help.

void add(int A[], int B[], int m, int n, int *sum)
/* Initialize the sum polynomial */
memset (sum, 0, max(m, n));
/*Take every term of first polynomial */
for (int i = 0; i < m; i++)
sum[i] += A[i];
/*Take every term of second polynomial */
for (int i = 0; i < n; i++)
sum[i] += B[i];
}

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.