User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 427,937 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,926 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 877 | Replies: 5
Reply
Join Date: Jan 2008
Posts: 42
Reputation: Thew is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Light Poster

Delphi SetLength dilemma

  #1  
Jul 6th, 2008
Hello,
does the SetLength function in Delphi called with the second argument 0 free the memory used for the array?
Example:
  1. var
  2. Names: array of String;
  3. i: Byte;
  4.  
  5. SetLength(Names,10);
  6. for i:=0 to 9 do
  7. Names[i] := '...';
  8.  
  9. SetLength(Names,0); // << does this code free memory used by Names array ?
and if this doesn't work, how can I free memory for array...
Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

  #2  
Jul 6th, 2008
All objects in Delphi, including 'string', are allocated on the heap. When you resize a dynamic array the unused objects are automatically destructed for built-in types only -- so in this case, yes.

When you set the length of a dynamic array to zero the dynamic array object itself is also freed and its value becomes nil.

However, deallocating an object does not necessarily mean that Delphi returns memory to the system.

Hope this helps.
Reply With Quote  
Join Date: Jan 2008
Posts: 42
Reputation: Thew is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Light Poster

Re: Delphi SetLength dilemma

  #3  
Jul 6th, 2008
So if I want be sure with deallocating memory, should I use something like FreeMem(Names) ?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

  #4  
Jul 6th, 2008
No. That will cause a system exception.

You have very little control over exactly how much memory is used by your application. I believe there are compiler directives to specify the maximum heap size, but I've never had occasion to use them under Win32.
Reply With Quote  
Join Date: Jan 2008
Posts: 42
Reputation: Thew is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Light Poster

Re: Delphi SetLength dilemma

  #5  
Jul 8th, 2008
I tried to call FreeMem(Names) after I set it with SetLength(Names,0), no exception. I think that after I set length of array to zero, system will automaticaly frees the memory used for this array.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

  #6  
Jul 8th, 2008
No exception because after SetLength(0) the variable is already nil, and FreeMem has a built-in safeguard against freeing nil pointers.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 6:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC