Delphi SetLength dilemma

Reply

Join Date: Jan 2008
Posts: 53
Reputation: Thew is an unknown quantity at this point 
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Junior Poster in Training

Delphi SetLength dilemma

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

 
0
  #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 Quick reply to this message  
Join Date: Jan 2008
Posts: 53
Reputation: Thew is an unknown quantity at this point 
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Junior Poster in Training

Re: Delphi SetLength dilemma

 
0
  #3
Jul 6th, 2008
So if I want be sure with deallocating memory, should I use something like FreeMem(Names) ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

 
0
  #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 Quick reply to this message  
Join Date: Jan 2008
Posts: 53
Reputation: Thew is an unknown quantity at this point 
Solved Threads: 0
Thew's Avatar
Thew Thew is offline Offline
Junior Poster in Training

Re: Delphi SetLength dilemma

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Delphi SetLength dilemma

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC