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 8333979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:12:02+00:00 2026-06-09T03:12:02+00:00

I’m working in a code that has a data structure like this: type TData1

  • 0

I’m working in a code that has a data structure like this:

type
  TData1 = record
    IntField: Integer;
    StrField: string;
  end;

  TData2 = record
    DateField: TDateTime;
    StrField: string;
  end;

var
  AData1 = array of ^TData1;
  AData2 = array of ^TData2;

Sometimes I have to add an element to one of that arrays, like this:

L := Length(AData1);
SetLength(AData1, L + 1);
New(AData1[L]);

How can I write a procedure that does that job of increasing the array size and allocating memory for a new item that works for any kind of pointer? And it must be done without changing the definitions of the records (TData1, TData2) and the arrays (AData1, AData2), so it won’t break existing code.

Ps: I don’t have much background with pointers and that kind of programming, I’d certainly use objects and dynamic linked lists, but in this case it’s a legacy code and I can’t change it, at least for now.

  • 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-06-09T03:12:03+00:00Added an answer on June 9, 2026 at 3:12 am

    Update:

    This is not a full answer. Using TDynArray as mentioned by David may solve your problem.

    Using RTTI I made a another solution for you. It is a generic solution and you can easily add more functions/capabilities. It will preserve your type declarations. Example of adding/removing records is included.

    The record TDynPtArray handles any dynamic array of pointers to records. It is initialized with an Init call:

    DPA.Init(TypeInfo(TData1), AData1);
    Data1 := DPA.Add;  // Adds a record with default values and
                       // returns a pointer to the record
    DPA.Remove; // Finalizes/deallocates the last record and 
                // shrinks the dynamic array
    

    –

    uses
      Windows,System.SysUtils,System.TypInfo;
    
    Type
      TPtArray = array of Pointer;
      PPtArray = ^TPtArray;
      TDynPtArray = record
      private
        FDynArray: PPtArray;
        FTypeInfo: PTypeInfo;
        FTypeData: PTypeData;
      public
        constructor Init( T: Pointer; var dynArray);
        function Add : Pointer;
        procedure Remove;
        procedure Clear;
      end;
    
    constructor TDynPtArray.Init(T: Pointer; var dynArray);
    begin
      FTypeInfo := T;
      if  (FTypeInfo^.Kind <> tkRecord) then
        raise Exception.CreateFmt('%s is not a record',[FTypeInfo^.Name]);
      FTypeData := GetTypeData( FTypeInfo);
      FDynArray := @dynArray;
    end;
    
    function TDynPtArray.Add: Pointer;
    var
      L: integer;
    begin
      L := Length(FDynArray^);
      SetLength(FDynArray^,L+1);
      GetMem( FDynArray^[L], FTypeData^.elSize);
      ZeroMemory( FDynArray^[L], FTypeData^.elSize);
      Result := FDynArray^[L];
    end;
    
    procedure RecordClear(var Dest; TypeInfo: pointer);
    asm
    {$ifdef CPUX64}
      .NOFRAME
    {$endif}
      jmp System.@FinalizeRecord
    end;
    
    procedure TDynPtArray.Remove;
    var
     L: integer;
    begin
      L := Length(FDynArray^);
      if (L = 0) then
        exit;
      RecordClear( FDynArray^[L-1]^,FTypeInfo); // Finalize record
      FreeMem( FDynArray^[L-1], FTypeData^.elSize);
      SetLength(FDynArray^,L-1);
    end;
    
    procedure TDynPtArray.Clear;
    begin
      while (Length(FDynArray^) <> 0) do
        Self.Remove;
    end;
    

    And a little test:

    type
      PData1 = ^TData1;
      TData1 = record
        IntField: Integer;
        StrField: string;
      end;
      TData1Arr = array of PData1;
    
    var
      AData1: TData1Arr;
      Data1: PData1;
      DPA: TDynPtArray;
    begin
      DPA.Init(TypeInfo(TData1), AData1);
      Data1:= DPA.Add;
      Data1^.StrField := '111';
      WriteLn(Data1^.IntField);
      WriteLn(Data1^.StrField);
    
      DPA.Clear;
    
      ReadLn;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I know there's a lot of other questions out there that deal with this

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.