Is it possible to re-size arrays at runtime?

Is it possible to re-size arrays at runtime?

Is it possible to re-size arrays at runtime?

I came across an exampleof how to do one. The author is BenLicht, who has come up with a brilliant way to doa runtime sized array.

The trick is using a pointer to an arraywith a size of 1, then allocating memory for the pointer by multiplyingthe number of items you want in the array by the size of the array type.

Here’s a sample unit I’ve adapted from his example:

{This unit demonstrates how to implement a dynArray}unit U;interfaceuses  SysUtils, WinTypes, WinProcs, Classes, Controls, Forms, Dialogs, StdCtrls;
type  TResizeArr = array[0..0] of string;  PResizeArr = ^TResizeArr;  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;  procedure DefineDynArray(var h : THandle;          {Handle to mem pointer}                           NumElements : LongInt;    {Number of items in array}                           var PArr : PResizeArr);   {Pointer to array struct}  procedure TestDynArray;implementation{$R *.DFM}{============================================================================ Procedure that defines the dynarray. Note that the THandle and Pointer to the array are passed by reference. This enables them to be defined outside the scope of this procedure. ============================================================================}procedure DefineDynArray(var h : THandle;          {Handle to mem pointer}                         NumElements : LongInt;    {Number of items in array}                         var PArr : PResizeArr);   {Pointer to array struct}begin  {Allocate Windows Global Heap memory}  h    := GlobalAlloc(GMEM_FIXED, NumElements * sizeof(TResizeArr));  PArr := GlobalLock(h);end;{============================================================================ Procedure that uses the DefineDynArray proc. This is pretty useless, but provides a good example of how you can access the elements of the 'array' once the array is defined. ============================================================================}procedure TestDynArray;var  MyArray : PResizeArr;  I       : Integer;  str     : String;  h       : THandle;begin  str := '';  DefineDynArray(h, 10, MyArray); {Define the 'array'}  for I := 0 to 9 do    MyArray^[I] := IntToStr(I);
  for I := 0 to 9 do    str := str + MyArray^[I] + ',';  ShowMessage(str);  GlobalUnlock(h); {Must make a call to unlock the memory, then}  GlobalFree(h);   {free the memory and invalidate the handle}end;procedure TForm1.Button1Click(Sender: TObject);begin     TestDynArray;end;end.

This is a perfect example of one of those programming things that takehours to figure out, but turn out to be amazingly simple.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as