Solved

# Dynamical arrays

Posted on 1998-10-21
310 Views
I have a record. But I want create an array of this record. Size of this array not determine. I want increaze size of my array in code.

0
Question by:vfrc
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +4

LVL 4

Expert Comment

ID: 1343649
Are you using Delphi4, if so, you can just create the array by:

mArray:Array of MyRecordType;

With no range defined, you can increase the size dynamically in code.
0

LVL 4

Expert Comment

ID: 1343650
Sorry,

mArray=Array of MyRecordType
0

LVL 8

Expert Comment

ID: 1343651
vfrc,

here is the example I once got from inter :

Good day(or night) friends,

I'd like to give an example about dynamic allocation and range checking off...

Simply start with a new form and paste the following code and link the FormCreate method to the
one below.

//CODE BEGINS
unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type

PTestRec = ^TTestRec;
TTestRec = packed record
x : integer;
y : array[0..0] of char;
end; var
Form1: TForm1;

implementation

{\$R *.DFM}

// turn range checking off so that we can access ttestrec.y[0...n]
{\$R-} procedure TForm1.FormCreate(Sender: TObject);
var
p : PTestRec; //declare a pointer
i : integer; begin
GetMem(p, sizeof(TTestRec) + 20);
try
i :=5;
StrCopy(p.y, 'hello there');
Caption := StrPas(PChar(@(p.y[i])));
finally
FreeMem(p, sizeof(TTestRec) + 20);
end; end;

end.

// CODE ENDS

WHAT IS UP

--------------

Our record decleration is as follows(just an example)

PTestRec = ^TTestRec;

TTestRec = packed record
x : integer;
y : array[0..0] of char;
end;

Now assume that, I want to place a null terminated string in y and feed to some function. Now let's analyze the code at

{\$R-}
procedure TForm1.FormCreate(Sender: TObject);
var
p : PTestRec;
i : integer; begin
// now this is dynamic allocation. By doing this
// I have a record at p which indirectly means as follows
// TTestRec = packed record
// x : integer;
// y : array[0..20] of char;
// end;
GetMem(p, sizeof(TTestRec) + 20);
try
i :=5; // we should use a variable to address the p.y[i]
// note that p.y[1] produces and error at COMPILE TIME
StrCopy(p.y, 'hello there'); //no GPFault! we succeeded
Caption := StrPas(PChar(@(p.y[i]))); //forms caption should be 'there'
finally
FreeMem(p, sizeof(TTestRec) + 20);
end; end;

To test it put a break point at formcreate and run the program. Step and debug the p and its members...

So, is it clear?
Regards, Igor

Regards, Zif.

ps. all thanks to inter.
0

LVL 27

Expert Comment

ID: 1343652
hi vfcr,

here a sample in old pascal-code(simple List), it also works in delphi-code

type
YourRecord = record
YourRecordEntries;
end;

type
YourRecordPointer = ^YourRecordList;
YourRecordList = Record
YR : YourRecord;
Next : YourRecordPointer;
end;
var
rootptr : YourRecordPointer = nil; {holds the start of the list}
tmpptr : YourRecordPointer = nil; {temporary for free use (search, create, delete)}
topptr  : YourRecordPointer = nil; {holds the top of the list}

begin
new(tmpPtr);
tmpptr^.YR := ARecord;
tmpptr^.next := nil;
{ First Entry ?}
if rootptr = nil then rooptr := tmpptr;
if  topptr = nil then
topptr := tmpptr
else
begin {No First Entry}
topptr^.next := tmpptr;
topptr := tmpptr;
end;
end;

procedure deletedynlist;
begin
tmpptr := rootptr;
while tmpptr <> nil do
begin
rootptr := tmpptr^.next;
dispose(tmpptr);
tmpptr := rootptr;
end;
end;

there can be more procedures as sortlist or deleteentry or searchrecord and so on

meikl

0

LVL 27

Expert Comment

ID: 1343653
hi vfrc,

i forget the topptr

procedure deletedynlist;
begin
Â Â tmpptr := rootptr;
Â Â while tmpptr <> nil do
Â Â begin
Â Â Â Â rootptr := tmpptr^.next;
Â Â Â Â dispose(tmpptr);
Â Â Â Â tmpptr := rootptr;
Â Â end;
Topptr := nil;
end;

meikl
0

LVL 2

Expert Comment

ID: 1343654
Another way is by the use of TList (available even for Delphi 1.0)
0

LVL 5

Expert Comment

ID: 1343655
http://www.delumpa.com/

Look in the tips section to learn how to use dynamic arrays if you don't have Delphi 4.
0

LVL 20

Expert Comment

ID: 1343656
But if you HAVE Delphi 4, please please please use it's dynamic arrays. You can dynamically change the array size by using SetLength(array,arrayLen)...

0

LVL 5

Expert Comment

ID: 1343657
Some of us are handicapped!!  Some of us only have Delphi 2!! (Are we classified as Delphi Gimps?)   :)
0

LVL 20

Expert Comment

ID: 1343658
Yes, you are...   :-)
0

LVL 1

Accepted Solution

AttarSoftware earned 50 total points
ID: 1343659
Heh, I shall be cheeky and answer this one with my short reply;

type
myRecord = RECORD
stuff  : longint ;
things : byte ;
END ;

myRecords = array [ 1 .. 1 ] of myRecord ;

myRecordsPtr = ^myRecords ;
nElements : longint ;

then you can dimension this with:

ReAllocMem( myRecordsPtr, sizeof( myRecord ) * nElements ) ;

and access it with:

for c := 1 to nElements do
ShowMessage( Inttostr( myRecordsPtr^[ c ].stuff ) );

Hope this works...

Tim.
0

LVL 1

Expert Comment

ID: 1343660
Hello?

Tim.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code Â  (CODE) The Imâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized â€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
###### Suggested Courses
Course of the Month9 days, left to enroll