LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2023, 05:42 AM   #1
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Rep: Reputation: 171Reputation: 171
Using free() after malloc() in C.


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
 
Old 02-26-2023, 07:12 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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))
 
4 members found this post helpful.
Old 02-26-2023, 07:53 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
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.
 
2 members found this post helpful.
Old 02-26-2023, 08:44 AM   #4
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by ntubski View Post
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));

Last edited by amikoyan; 02-26-2023 at 08:49 AM.
 
Old 02-26-2023, 09:03 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
Quote:
Originally Posted by amikoyan View Post
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));
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.
 
2 members found this post helpful.
Old 02-26-2023, 09:23 AM   #6
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by pan64 View Post
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!

Thanks pan64.
 
Old 02-26-2023, 09:32 AM   #7
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by pan64 View Post
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

Last edited by amikoyan; 02-26-2023 at 09:35 AM.
 
Old 02-26-2023, 10:04 AM   #8
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
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;    
}

Last edited by amikoyan; 02-26-2023 at 10:06 AM.
 
Old 02-26-2023, 10:08 AM   #9
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
Thank you pan64 and ntubski.

Last edited by amikoyan; 02-26-2023 at 11:57 AM.
 
Old 02-27-2023, 12:02 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
glad to help you
 
Old 02-27-2023, 07:52 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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];
 
1 members found this post helpful.
Old 03-24-2023, 02:19 AM   #12
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 318

Original Poster
Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by ntubski View Post
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.
 
Old 03-24-2023, 02:29 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
I still think it is overcomplicated:
Code:
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];
}
 
Old 03-25-2023, 03:56 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Also one could check for the degree:
Code:
int d=max(m,n);
while (d>=0 && result[d]==0) --d;
Here constant zero polynom has degree -1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] compiler claims memory that was already malloc-ed was not malloc-ed andrew.comly Programming 6 06-08-2020 10:10 AM
IO write using O_DIRECT flag with and without memset() after malloc() naik_yogesh Linux - Software 0 04-01-2009 06:28 AM
malloc/free in C h/w Programming 12 02-26-2004 01:13 PM
malloc/free and segfault - advanced question iTux Programming 3 12-10-2003 04:51 PM
Is my malloc/free thinking correct? registering Programming 6 06-18-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration