Solved

Dynamical arrays

Posted on 1998-10-21
12
295 Views
Last Modified: 2010-04-06
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
Comment
Question by:vfrc
  • 2
  • 2
  • 2
  • +4
12 Comments
 
LVL 4

Expert Comment

by:dwwang
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

by:dwwang
ID: 1343650
Sorry,

mArray=Array of MyRecordType
0
 
LVL 8

Expert Comment

by:ZifNab
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
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
 
LVL 27

Expert Comment

by:kretzschmar
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}


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
 
LVL 27

Expert Comment

by:kretzschmar
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

by:kjteng
ID: 1343654
Another way is by the use of TList (available even for Delphi 1.0)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:scrapdog
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

by:Madshi
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)...

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:scrapdog
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

by:Madshi
ID: 1343658
Yes, you are...   :-)
0
 
LVL 1

Accepted Solution

by:
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

by:AttarSoftware
ID: 1343660
Hello?

Tim.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now