freeing allocated memory

From: Curley Q. (curleyq_at_bogus.net)
Date: 04/28/04


Date: Wed, 28 Apr 2004 19:13:22 GMT


   for(;;)
   {
     c = a * b;
     c %= 100000;
     printf("%9d", c);
     d = int2str(c);
     left = midstr(d, 0, 3);
     right = midstr(d, strlen(d)-3, 3);
     free(d);
     a = atoi(left);
     b = atoi(right);
     if(a * b == 0)
       break;
     printf("%6d %6d\n", a, b);
   }
   free(left);
   free(right);

In the code fragment above d is a buffer allocated by a call to
malloc in int2str and freed each pass through the loop. left and
right are also buffers allocated by calls to malloc in midstr,
but they are freed only once when execution breaks out of the
loop. What happens to the memory that is allocated to left and
right in each call to midstr? Is allocated memory getting cleaned
up properly in this code?



Relevant Pages

  • Re: freeing allocated memory
    ... > malloc in int2str and freed each pass through the loop. ... > right are also buffers allocated by calls to malloc in midstr, ... Is allocated memory getting cleaned ...
    (comp.lang.c)
  • Re: why is casting malloc a bad thing?
    ... P.J. Plauger wrote: ... > There's no requirement that a malloc call return a pointer ... Is a piece of memory allocated by malloc required to be aligned ... Subclause 7.10.3 requires allocated memory to be suitably aligned ...
    (comp.lang.c)
  • Re: To use global variables or no????
    ... Why not use allocated memory? ... How does malloc() remember where its skiplist starts? ... The malloc function puts a struct into a skiplist with an auto root node at ... memory is returned to the operating system. ...
    (comp.lang.c)
  • Re: freeing allocated memory
    ... >>In the code fragment above d is a buffer allocated by a call to ... >>malloc in int2str and freed each pass through the loop. ... > This sounds very homework-like, so I'll just tell you that the ...
    (comp.lang.c)
  • Re: Disadvantages of malloc()
    ... need not provide a malloc() implementation. ... each dynamically allocated memory block. ... Dan Pop ...
    (comp.lang.c)