freeing memory

Discussion in 'C Programming' started by Frank Silvermann, Jun 13, 2006.

  1. /* partition2.c */

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
    #define ITERATIONS 10

    int rand_in_range(int, int);
    int * partition(int, int);

    int main(void)
    {
    int m=8, n=3, i;
    int *p;
    /* all declarations in main need to be north of here */
    /* seed timer */
    srand(time(NULL));
    /* call partition and examine returns */
    printf("set has %d elements and %d partitions\n", m, n);
    for(i = 0; i < ITERATIONS; i++)
    {
    p = malloc((n)*sizeof(int));
    p = partition(m,n);
    printf("m in n chunks: %d %d %d\n", p[0], p[1], p[2]);
    /* free p; */
    }
    return 0;
    }


    int rand_in_range(int m, int n)
    {
    /*seed srand in main */
    /* [m, n] is range */
    int roll_again_threshold, divisor, result, tmp, offset, num_results;

    if (m>n) SWAP(m, n);
    offset = m;
    num_results = n - m + 1;

    if (num_results == 1) {
    return m;
    }
    roll_again_threshold = RAND_MAX - RAND_MAX%num_results;
    divisor = roll_again_threshold/num_results;

    do {
    result = rand();
    } while (result >= roll_again_threshold);
    result /= divisor;
    return offset + result;
    }

    int * partition(int m, int n)
    {
    int top_range, i, p;
    int *q;
    /* end declarations */
    /* if n>m bomb out */
    if (n > m) return NULL;
    top_range = m - n;
    q = malloc((n)*sizeof(int));
    for (i=0; i<(n-1); i++)
    {
    p=rand_in_range(0, top_range);
    q = p + 1;
    top_range = top_range - p;
    }
    q[n-1]=top_range + 1;
    return q;
    }
    /* end source */
    I ask my compiler to carve out memory in main and in a subroutine. The
    adage is that if one asks an OS for memory, he must give it back. I
    would think that the requested memory in the subroutine would disappear
    when the return is made. It would also seem that I've created a memory
    leak in main, but when I add a free where it is now commented out, I get
    an error. Happy for any hints. frank
     
    Frank Silvermann, Jun 13, 2006
    #1
    1. Advertising

  2. Frank Silvermann

    SM Ryan Guest

    # I ask my compiler to carve out memory in main and in a subroutine. The
    # adage is that if one asks an OS for memory, he must give it back. I
    # would think that the requested memory in the subroutine would disappear
    # when the return is made. It would also seem that I've created a memory

    What makes heap memory like malloc so useful is precisely that the
    memory is not automatically released when a procedure returns. The
    normal C malloc does not garbage collect, so it is your responsibility
    to keep a pointer to each mallocked block until you free it, and not
    to use the pointer after free.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I have no respect for people with no shopping agenda.
     
    SM Ryan, Jun 13, 2006
    #2
    1. Advertising

  3. Frank Silvermann

    jackdorse Guest

    Frank Silvermann wrote:

    > /* partition2.c */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    > #include <math.h>
    > #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
    > #define ITERATIONS 10
    >
    > int rand_in_range(int, int);
    > int * partition(int, int);
    >
    > int main(void)
    > {
    > int m=8, n=3, i;
    > int *p;
    > /* all declarations in main need to be north of here */
    > /* seed timer */
    > srand(time(NULL));
    > /* call partition and examine returns */
    > printf("set has %d elements and %d partitions\n", m, n);
    > for(i = 0; i < ITERATIONS; i++)
    > {
    > p = malloc((n)*sizeof(int));
    > p = partition(m,n);
    > printf("m in n chunks: %d %d %d\n", p[0], p[1], p[2]);
    > /* free p; */
    > }
    > return 0;
    > }
    >
    >
    > int rand_in_range(int m, int n)
    > {
    > /*seed srand in main */
    > /* [m, n] is range */
    > int roll_again_threshold, divisor, result, tmp, offset, num_results;
    >
    > if (m>n) SWAP(m, n);
    > offset = m;
    > num_results = n - m + 1;
    >
    > if (num_results == 1) {
    > return m;
    > }
    > roll_again_threshold = RAND_MAX - RAND_MAX%num_results;
    > divisor = roll_again_threshold/num_results;
    >
    > do {
    > result = rand();
    > } while (result >= roll_again_threshold);
    > result /= divisor;
    > return offset + result;
    > }
    >
    > int * partition(int m, int n)
    > {
    > int top_range, i, p;
    > int *q;
    > /* end declarations */
    > /* if n>m bomb out */
    > if (n > m) return NULL;
    > top_range = m - n;
    > q = malloc((n)*sizeof(int));
    > for (i=0; i<(n-1); i++)
    > {
    > p=rand_in_range(0, top_range);
    > q = p + 1;
    > top_range = top_range - p;
    > }
    > q[n-1]=top_range + 1;
    > return q;
    > }
    > /* end source */
    > I ask my compiler to carve out memory in main and in a subroutine. The
    > adage is that if one asks an OS for memory, he must give it back. I
    > would think that the requested memory in the subroutine would disappear
    > when the return is made. It would also seem that I've created a memory
    > leak in main, but when I add a free where it is now commented out, I get
    > an error. Happy for any hints. frank





    it should be

    free(p);
     
    jackdorse, Jun 13, 2006
    #3
  4. Frank Silvermann

    Chris Dollin Guest

    Frank Silvermann wrote:


    > {
    > p = malloc((n)*sizeof(int));


    Allocate some memory [1] ...

    > p = partition(m,n);


    .... and then immediately make it inaccessible.

    Easy fix: remove the mallocation.

    > /* free p; */


    free( p );

    --
    Chris "seeker" Dollin
    "I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/
     
    Chris Dollin, Jun 13, 2006
    #4
  5. Chris Dollin wrote:
    > Frank Silvermann wrote:
    >
    >
    >> {
    >> p = malloc((n)*sizeof(int));

    >
    > Allocate some memory [1] ...
    >
    >> p = partition(m,n);

    >
    > .... and then immediately make it inaccessible.
    >
    > Easy fix: remove the mallocation.
    >
    >> /* free p; */

    >
    > free( p );
    >

    Thanks all for replies. Apparently the parens matter. Here is my new
    version of main, with everything else unchanged from upthread:
    int main(void)
    {
    int m=8, n=3, i, j;
    int *p;
    /* all declarations in main need to be north of here */
    /* seed srand with time */
    srand(time(NULL));
    /* call partition and examine returns */
    printf("set has %d elements and %d partitions\n", m, n);
    for(i = 0; i < ITERATIONS; i++)
    {
    p = malloc((n)*sizeof(int));
    p = partition(m,n);
    if (p = NULL)
    {
    printf("failure\n");
    }
    printf("m in n chunks: ");
    /*for (j = 0;j < n; j++)
    {
    printf(" %d", p[j]);
    }
    printf("\n");*/
    free (p);
    }
    return 0;
    }
    /*end source */
    Couple of questions: 1) Do I have a memory leak?
    2) The output is now commented out. It compiles but causes my OS to
    hang. I could send the error report and wait for the Unnamed Vendor to
    get back to me, but I'm not sure when hell is to freeze over. Anyways,
    besides the fact that the commented-out source doesn't produce behavior
    in the ensuing executable that I like, it looks childish, in particular
    in a language that was designed to hop around arrays. So this question
    is, what is an elegant way in C to print a smallish array of ints? frank
     
    Frank Silvermann, Jun 13, 2006
    #5
  6. Frank Silvermann <> writes:
    > Chris Dollin wrote:
    >> Frank Silvermann wrote:
    >>
    >>> {
    >>> p = malloc((n)*sizeof(int));

    >> Allocate some memory [1] ...
    >>
    >>> p = partition(m,n);

    >> .... and then immediately make it inaccessible.
    >> Easy fix: remove the mallocation.
    >>
    >>> /* free p; */

    >> free( p );
    >>

    > Thanks all for replies. Apparently the parens matter.


    Of course they do. free is a function, the parentheses are part of
    the syntax of a function call. (Note that return is a special kind of
    statement, *not* a function; its syntax is "return;" or
    "return expression;", so no parentheses are required. Parentheses are
    allowed because an expression may be enclosed in parentheses:
    "return (expression);" is legal, but "return ();" is not.)

    > Here is my new version of main, with everything else unchanged from
    > upthread:

    [snip]
    > int *p;

    [snip]
    > p = malloc((n)*sizeof(int));
    > p = partition(m,n);

    [snip]

    You have a memory leak. You assign the result of malloc() to p, and
    you then immediately assign another value to p, losing any reference
    to the allocated memory.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 13, 2006
    #6
  7. Frank Silvermann

    Joe Smith Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Frank Silvermann <> writes:
    >> Chris Dollin wrote:
    >>> Frank Silvermann wrote:
    >>>
    >>>> {
    >>>> p = malloc((n)*sizeof(int));
    >>> Allocate some memory [1] ...
    >>>
    >>>> p = partition(m,n);
    >>> .... and then immediately make it inaccessible.
    >>> Easy fix: remove the mallocation.
    >>>
    >>>> /* free p; */
    >>> free( p );
    >>>

    >> Thanks all for replies. Apparently the parens matter.

    >
    > Of course they do. free is a function, the parentheses are part of
    > the syntax of a function call. (Note that return is a special kind of
    > statement, *not* a function; its syntax is "return;" or
    > "return expression;", so no parentheses are required. Parentheses are
    > allowed because an expression may be enclosed in parentheses:
    > "return (expression);" is legal, but "return ();" is not.)

    Why do people put whitespace between the ultimate 'e' and '(' ?


    >> Here is my new version of main, with everything else unchanged from
    >> upthread:

    > [snip]
    >> int *p;

    > [snip]
    >> >> p = partition(m,n);

    > [snip]
    >
    > You have a memory leak. You assign the result of malloc() to p, and
    > you then immediately assign another value to p, losing any reference
    > to the allocated memory.

    I believe that if I remove:
    p = malloc((n)*sizeof(int));
    I might be better off. Furthermore, I suspect that the reason the ensuing
    /*for (j = 0;j < n; j++)
    {
    printf(" %d", p[j]);
    }
    printf("\n");*/
    was in trouble was that I had lost any reference to the allocated memory.
    frank
    ----------
    I'm just sitting here doing time--john lennon, listening to Yoko practice
     
    Joe Smith, Jun 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajesh.Rapaka

    freeing memory

    Rajesh.Rapaka, Apr 20, 2005, in forum: Java
    Replies:
    17
    Views:
    2,874
    Eric Sosman
    Apr 21, 2005
  2. Harald Kirsch

    freeing memory

    Harald Kirsch, Apr 22, 2005, in forum: Java
    Replies:
    0
    Views:
    459
    Harald Kirsch
    Apr 22, 2005
  3. Hassan Iqbal
    Replies:
    3
    Views:
    517
    Al Bowers
    Sep 25, 2003
  4. freeing memory....partially

    , Oct 4, 2003, in forum: C Programming
    Replies:
    12
    Views:
    589
  5. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    635
    Jean-Claude Arbaut
    Jun 15, 2005
Loading...

Share This Page