1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Freeing a pointer

Discussion in 'C' started by Plfgoa, Jun 11, 2008.

  1. Plfgoa

    Plfgoa New Member

    Joined:
    Jun 11, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    If a function is returning a pointer and if that pointer was returned by a malloc function , how do I free that pointer ? If I run valgrind , it will show memory leak since "k" was not freed. How do I free "k" in following ?

    Example:

    int * sum(int a, int b)
    {
    int sum;
    int *k=malloc(sizeof(int));
    sum=a+b;
    k=∑
    return k;
    }

    Thank you.

    -PLF
     
  2. gunshot08

    gunshot08 New Member

    Joined:
    Jun 6, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    you use the free() function

    int * sum(int a, int b)
    {
    int sum;
    int *k=malloc(sizeof(int));
    sum=a+b;
    k=∑
    return k;
    free(k);
    }
     
  3. Plfgoa

    Plfgoa New Member

    Joined:
    Jun 11, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanks for the response . Just wanted to know if one uses free() after a return statement will free() ever get called ?

    Thanks.

    -PLF
     

Share This Page