to use a linked list isn't an option? Take a look at TList and at TObjectList in Delphi help, and ask for futher information : )
Main Topics
Browse All TopicsOK have the following data types:
TFeatureType = (ftPoint, ftLine, ftArea, ftCompound, ftUnknown);
TFeatureEntry = record
OriginalPermanentID: string;
OutPermanentID: string;
FeatureType: TFeatureType;
FeatureTable: string;
end;
TFeatureEntryArray = array of TFeatureEntry;
TFeatureMapEntry = record
FeatureID: String;
Features: TFeatureEntryArray;
end;
TFeatureMapArray = array of TFeatureMapEntry;
Have a problem that when I assign large amounts of data dynamically to a variable of type TFeatureMapArray, the program eats up my memory pretty quick. I have distilled down the problem to the following code snippet (Map is of type TFeatureMapArray).
for i := 0 to 100000 do
begin
SetLength(Map, i + 1);
Map[i].FeatureID := '12345678';
SetLength(Map[i].Features,
Map[i].Features[0].Origina
Map[i].Features[0].OutPerm
Map[i].Features[0].Feature
Map[i].Features[0].Feature
end;
Anyone have any ideas why? What can I do to stop this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
agreed.. if you absolutely want to use a dynamic array, increase it by a large amount as needed and then crincate at the end. However I also agree that a TList would be more efficient
do something like this for dynamic arrays
var count,i:Integer;
begin
count := 0;
SetLength(Map,5000);
for i:=1 to whatever do
begin
if i > High(Map) then
SetLength(Map,Length(Map)+
map[i].stuff := 'test';
inc(count);
end;
SetLength(Map,count);
end;
Now calling high() is still not the most efficiet thing to do in a loop, but it will work better than resizing the array every loop.
If you have more code that depends on dynamic sizing, you can increase the size of the arrays in smaller chunck (e.g., 25% each time you must grow it), although array copy operations will hit you each time you must grow it it won't be that bad and it can avoid the overhead of 100K worth of pointers in a linked list. (I think this is what rbohax is saying). And as suggested, you can truncate the dynamic array to the correct size when you are finished adding elements.
You must track the end of array yourself since high will reflect the allocated dynamic array size instead of the amount being used (capacity vs. count as implemented in some VCL classes).
Whether you are better off to a dynamic array grown by large hunk or using as a tlist depends on how you use the list/array.
You might also what to encapsulate this in a class, allowing you to change implementation by changing class instead of whereever you use the code.
I'm going out on a limb here but . . . could you mabe use a TClientDataset with the fields defined apporpriately instead of the dynamic array? You could programatically define a new TCDS for each of what are now your Map[i]'s and have an array of TCDS's instead of the array of TFeatureMapEntry's.
Just a thought . . . ;-)
Business Accounts
Answer for Membership
by: davelanePosted on 2003-09-24 at 09:28:39ID: 9422079
Just noticed that when I take the SetLength outside of the loop (see below) it executes lightning fast and doesn't eat up the memory? Problem is I can't do that, because I don't know how many entries I have.
SetLength(Map, 100001);
for i := 0 to 100000 do
.....