CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   Programming in C (http://www.cfd-online.com/Forums/main/4502-programming-c.html)

Tony March 6, 2002 13:37

Programming in C
 
Hi there,

I am very grateful if somebody can help me in the following questions:

I am doing some programming in MS C++ 6.0 for free-surface flows. I have some matrices whose dimensions are variable (say adding or subtracting a few columns). The memory is thus reallocated at every time steps. However, I found that the memory leaks and becomes ˇ°not enoughˇ± eventually for long-time runs. It seems to me that the reallocation may not free up the previously allocated memory.

My questions are (1) does realloc function really leak memory? (2) What should I do?

Appreciate your help.

Tony

andy March 6, 2002 15:15

Re: Programming in C
 
>> (1) does realloc function really leak memory?

Depends what you call a leak and what algoithms are being used by the system malloc/free. When you request a chunk of memory the algorithm may pick one from the pool that is a bit bigger than required. After heavy use this can lead to fragmentation with small unusable bits of memory all over the place unless malloc/free invokes some compaction - assuming you have invoked free() correctly I think we can assume MS C++ 6.0 does not do this.

>> (2) What should I do?

* use Fortran

* never use the system malloc/free heavily for any serious computation. These are general purpose routines which are neither efficient in memory use or speed but some compromise. They allocate memory without knowing anything about the details of your computation whose memory use is probably quite strongly "structured".

* allocate a big chunk of memory using malloc. Allocate memory for your arrays from it - you can almost certainly do this without fragmentation and faster than the system routines.

Tony March 6, 2002 18:48

Re: Programming in C
 
Hi Andy,

Thanks kindly.

If I understand your suggestions correctly, I should malloc the maximum possible dimensions for arrays at the beginning. By doing this, I can avoid reallocating memory from time to time. This surely will speed up the operations with some bit of wasting the total memory, right?

Thanks again

Tony

Paul March 7, 2002 05:14

Re: Programming in C
 
Dear Tony, There is a discussion on the same topic in Fortran90 maillist recently. In Fortran90, we also have the allocate/deallocate pair to dynamically use the storage. HOWEVER, the machine DOESNOT free the space declared by allocate PHYSICALLY. It just put this spase aside can MAY not be used when you allocate a new array. Therefore, your memory may monotonely increase. In my program, I always declare a possiblly largest array and using another parameter to tag the true size.


andy March 7, 2002 08:06

Re: Programming in C
 
>> I can avoid reallocating memory from time to time. This surely will speed up the operations with some bit of wasting the total memory, right?

Yes but you can always reduce the "waste" at the cost of increased computation until you find the right balance. The key point is to use your knowledge of your data structures in order to gain substantial improvements over the general purpose system routines which, inevitably, lack this information.

Tony March 7, 2002 13:40

Re: Programming in C
 
Thank you very much, Paul and Andy.

I am glad that I am not alone. I think that is the only way to go - to declare a possiblly largest array.

Tony


All times are GMT -4. The time now is 17:16.