free memory


sunil_josh1
03-13-2004, 06:08 PM
hi,
i am allocating memory for one and two dimension array using malloc. The whole process is going in side a loop (approx. 16000 time) and every times memory is updating. Therefore it is taking lots of memory space. I am intresed to delete that memory space every times when loop is finised. I tried to assige zero value every time before the process taking place but it is not working.
I also used
free(d);
but this is also not working, where d is:
float *d;
d= (float*)malloc((unsigned)(n*sizeof(float)));
with warm regards,
sunil_josh1

Kevin Dolan
03-17-2004, 10:53 AM
Using free(d) should work. Keep in mind, though, that memory management is not instantaneous. It is entirely possible that the computer will go through the entire loop (each time allocating more memory) before it ever gets around to returning that first block of memory to the OS.

One question: Do you really need to reallocate over and over again like this? Why not just allocate the memory once, before the loop, and then reuse the same memory each time? Even if n changes from one pass through the loop to the next, you could just allocate enough for the maximum value of n.

Kevin