CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

Programming in C

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   March 6, 2002, 13:37
Default Programming in C
  #1
Tony
Guest
 
Posts: n/a
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
  Reply With Quote

Old   March 6, 2002, 15:15
Default Re: Programming in C
  #2
andy
Guest
 
Posts: n/a
>> (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.
  Reply With Quote

Old   March 6, 2002, 18:48
Default Re: Programming in C
  #3
Tony
Guest
 
Posts: n/a
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
  Reply With Quote

Old   March 7, 2002, 05:14
Default Re: Programming in C
  #4
Paul
Guest
 
Posts: n/a
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.

  Reply With Quote

Old   March 7, 2002, 08:06
Default Re: Programming in C
  #5
andy
Guest
 
Posts: n/a
>> 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.
  Reply With Quote

Old   March 7, 2002, 13:40
Default Re: Programming in C
  #6
Tony
Guest
 
Posts: n/a
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
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A book for a beginner wanting to learn programming frank Main CFD Forum 9 May 12, 2014 23:15
Programming a mesh m0t0r0ne Main CFD Forum 6 January 23, 2011 13:59
OpenFoam programming prapanj OpenFOAM 10 March 18, 2010 08:23
Programming in OpenFOAM vinu OpenFOAM 2 July 11, 2009 10:16
new CFD Programming Forum Thinker Main CFD Forum 14 November 19, 2002 17:03


All times are GMT -4. The time now is 03:29.