Solved

Dynamical arrays

Posted on 1998-10-21
12
306 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
[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
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 92
Firemonkey android show image from resource ? 1 69
Delphi problems to abort a printjob 9 44
DBCtrlGrid, Delphi, Scroll 8 80
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…

738 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