• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Dynamical arrays

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.

Sorry for bad english.  Max.

0
vfrc
Asked:
vfrc
  • 2
  • 2
  • 2
  • +4
1 Solution
 
dwwangCommented:
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
 
dwwangCommented:
Sorry,

mArray=Array of MyRecordType
0
 
ZifNabCommented:
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
formcreate(follow my comments)

{$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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kretzschmarCommented:
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}


procedure addrecord(ARecord : YourRecord);
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
 
kretzschmarCommented:
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
 
kjtengCommented:
Another way is by the use of TList (available even for Delphi 1.0)
0
 
scrapdogCommented:
http://www.delumpa.com/

Look in the tips section to learn how to use dynamic arrays if you don't have Delphi 4.
0
 
MadshiCommented:
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)...

Regards, Madshi.
0
 
scrapdogCommented:
Some of us are handicapped!!  Some of us only have Delphi 2!! (Are we classified as Delphi Gimps?)   :)
0
 
MadshiCommented:
Yes, you are...   :-)
0
 
AttarSoftwareCommented:
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
 
AttarSoftwareCommented:
Hello?

Tim.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now