Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6904955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:07:30+00:00 2026-05-27T08:07:30+00:00

In another question , David Heffernan posted a comment about his all time least

  • 0

In another question, David Heffernan posted a comment about his “all time least favourite Delphi construct”:

SetLength(FItems, Length(FItems)+1);

The construct he dislikes was heavily used in the Pebongo project, such as in this excerpt:

procedure TBSONDocument.ReadStream( F: TStream ); 
var 
  len : integer; 
  elmtype : byte; 
  elmname : string; 
begin 
  Clear; 
  f.Read( len, sizeof( len ) ); 
  f.Read( elmtype, sizeof( byte ) ); 

  while elmtype <> BSON_EOF do // Loop
  begin 
    elmname := _ReadString( f ); 
    SetLength( FItems, length( FItems ) + 1 ); // This, spotted by TOndrej
    case elmtype of 
      BSON_ARRAY: FItems[high( FItems )] := TBSONArrayItem.Create; 
      BSON_BINARY: FItems[high( FItems )] := TBSONBinaryItem.Create; 
      ...
    end; 
    f.Read( elmtype, sizeof( byte ) ); 
  end; 
end; 

What are the alternatives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:07:31+00:00Added an answer on May 27, 2026 at 8:07 am

    What I was talking about was growing dynamic arrays 1 element at a time:

    SetLength(FItems, Length(FItems)+1);
    

    In a loop, and with large arrays, this can lead to memory address fragmentation. When this happens you can find yourself unable to allocate a large contiguous block of memory even though the total available address space is large. If you are constrained by 32 bit address space this can be a very real problem. In addition, performance can also be an issue.

    There are a variety of ways to avoid the problem:

    • Pre-allocate the array. Sometimes this involves iterating twice, once to count the items and once to populate the array. This can be perfectly efficient. In fact, that is precisely what TBSONDocument.Clone in your question does.
    • Use a TList or TList<T> container. Although these use dynamic arrays as their underlying storage they implement a capacity based approach to allocation. When they are full, they allocate a large number of extra items in anticipation for future additions. This again can be perfectly efficient.
    • Yet another option, to be used as a last resort, is to move away from contiguously allocated memory. Allocate memory in chunks and use an indexed property of a class or record to map between indices and the actual chunk and location where the storage lives. This is particularly effective at avoiding address space fragmentation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

David's answer to another question shows a Delphi DLL function returning a WideString. I
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
Another question of mine about optimizing Objective C programs inspired the following: does anyone
In another question I posted yesterday, I got very good advice on how a
Another question about the application I'm making. So when the user signs up, he
Another question asked about the meaning of the code snippet a >>> 0 in
Another question asked about attributes, but I would like to extend the question to
Another question about Web proxy. Here is my code: IWebProxy Proxya = System.Net.WebRequest.GetSystemWebProxy(); Proxya.Credentials
Another question from an app programmer who's trying the first time to get together
In another question I asked about alignment this was given to me to answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.