Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic allocation with array of record

88 views
Skip to first unread message

Sylvain Fortin

unread,
Mar 1, 1998, 3:00:00 AM3/1/98
to

Hi everybody,

A simple question for you. I want to create a dynamic array of record
with Delphi 2 and I can't find out how to do it. In C I just have to use
calloc and the size of the array and use it exactly as a regular array.

I need a way to do the same thing with Delphi 2 and I don't want to use
TList or something like that. I tried with GetMem but it was impossible
to access each element of the array. An example follow.


Type
test = record
x,y :Integer;
end;
...

Var
list :^test;
size :Integer;
begin
size := 10;
GetMem(list, size);
list[1]^.x := 1; // not working
list[1]^.y := 2; // not working
end;

All the help will be appreciated.
Thank You

Sylvain Fortin

Wayne Niddery

unread,
Mar 1, 1998, 3:00:00 AM3/1/98
to

Sylvain Fortin wrote in message <34F9D47E...@videotron.ca>...

>A simple question for you. I want to create a dynamic array of record
>with Delphi 2 and I can't find out how to do it. In C I just have to use
>calloc and the size of the array and use it exactly as a regular array.


Whereas in C/C++, you can define a pointer to a type (e.g. ^test) and then
have that pointer point to an *array* of that type, in Pascal, you need to
define an *array type*.


>
>Type
> test = record
> x,y :Integer;
> end;


TestArray = array [0..9] of Test;
PTestArray = ^TestArray;

var
List: PTestArray;
begin
GetMem(list, sizeof(TestArray));
list^[0].x := 1;
list^[0].y := 2;
list^[1].x := 1;
list^[1].y := 2;

* Wayne Niddery - WinWright Consulting
* Host of RADBooks at http://home.ican.net/~wniddery/RADBooks.html
* -- Amazon.com Associate -- Delphi, C++Builder, JBuilder, InterDev
* ...remove X when replying...


Wayne Niddery

unread,
Mar 1, 1998, 3:00:00 AM3/1/98
to

-----Message forwarded from private email-----
From: Sylvain Fortin
Date: Sunday, March 01, 1998 7:39 PM
Subject: Re: Dynamic allocation with array of record

>Hi Wayne,
>
> In your example the memory is allocated dynamically but the array isn't
>dynamic because it can only have a constant value (array [0..9]). I want
>to be able to create an array of 1,15,20,111, ... Everything depend on
>the size of my database.
>
>Is it possible with Delphi?
>
>Thank You very much
>
>Sylvain Fortin

Wayne Niddery

unread,
Mar 1, 1998, 3:00:00 AM3/1/98
to

>-----Message forwarded from private email-----
>From: Sylvain Fortin
>Date: Sunday, March 01, 1998 7:39 PM
>Subject: Re: Dynamic allocation with array of record
>
>Hi Wayne,
>
> In your example the memory is allocated dynamically but the array isn't
>dynamic because it can only have a constant value (array [0..9]). I want
>to be able to create an array of 1,15,20,111, ... Everything depend on
>the size of my database.
>
>Is it possible with Delphi?

Yes. At first, the method may seem a bit awkward for someone coming from
C/C++ (at least it was to me), but it's not really. Same example as before
with appropriate changes:

Type
test = record
x,y :Integer;
end;

const
// max records that can fit in 2 gig
MaxTest = MaxInt div sizeof(Test) - 1;

type
// array can hold MaxTest records
TestArray = array [0..MaxTest] of Test;
PTestArray = ^TestArray;

var
List: PTestArray;
NumTest: integer;
begin
// allocate memory just for what's needed
NumTest := Random(MaxTest) + 1;
GetMem(list, sizeof(Test) * NumTest);


list^[0].x := 1;
list^[0].y := 2;
list^[1].x := 1;
list^[1].y := 2;

* Wayne Niddery - WinWright Consulting

jo...@enteract.com

unread,
Mar 6, 1998, 3:00:00 AM3/6/98
to

In article <34F9D47E...@videotron.ca>, Sylvain Fortin
<sylv...@videotron.ca> wrote: > > Hi everybody, > > A simple question for

you. I want to create a dynamic array of record > with Delphi 2 and I can't
find out how to do it. In C I just have to use > calloc and the size of the
array and use it exactly as a regular array. > > I need a way to do the same

thing with Delphi 2 and I don't want to use > TList or something like that. I
tried with GetMem but it was impossible > to access each element of the
array. An example follow. > > Type > test = record > x,y :Integer; > end;

> ... > > Var > list :^test; > size :Integer; > begin > size := 10; >
GetMem(list, size); > list[1]^.x := 1; // not working > list[1]^.y := 2; //
not working > end; > > All the help will be appreciated. > Thank You > >
Sylvain Fortin > Hi; In response to your inquiry pertaining to dynamic
arrays, the best vehicle Ive found is to use a Tlist. Here is an example of
how to build a TList (it expands dynamically automatically) and add objects
to the list. Instead of creating a record, you create a class. Hope this
helps Joe T unit Unit1; interface uses Windows, Messages, SysUtils,
Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 =
class(TForm) Button1: TButton; Edit1: TEdit; Button2: TButton; Edit2:
TEdit; Edit3: TEdit; procedure Button1Click(Sender: TObject); procedure
FormClose(Sender: TObject; var Action: TCloseAction); private { Private
declarations } public { Public declarations } end; tIssues = class
count : integer; Desc : string; bAVail : boolean; // This means THIS
"Issue" is available for demo end; var Form1: TForm1; issueList:
Tlist; sDesc: string; scheck: tIssues;
issue: tIssues; implementation {$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject); begin issue :=
tIssues.create; sCheck := tIssues.create; issue.count := 1; issue.Desc
:= 'Joes Desc'; issue.bAVail := TRUE; issueList := Tlist.Create;
issueList.Clear; issueList.Add(issue); issue :=
tIssues.create; issue.count := 1; issue.Desc := 'Joes Desc2';
issue.bAVail := TRUE; issueList.Add(issue); sCheck :=
issueList.Items[0]; Edit1.text := sCheck.Desc; sCheck :=
issueList.Items[1]; Edit2.text := sCheck.Desc; edit3.text :=
intTostr(issuelist.count); end; end.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading

Robert Lee

unread,
Mar 6, 1998, 3:00:00 AM3/6/98
to

Wayne's example is perfectly adequate, but I thought I'd chime in with a couple
of options to his example

If you always use a variable as in index (as opposed to the constants in
Wayne's example) you can define the array as:

type
TestArray=array[0..0] of Test;

I like this form better because it is more apparent that the array has no
actual size.

Secondly, you can address the items as

list[i].x

without the ^. The compiler will still perform the dereferencing and code is
more readable in my opinion.

Finally, as a C/C++ convertee, you may be interested to know that inc and dec
work on pointer types in a familiar way:

inc(TestPtr,2); // equivalent to TestPtr:=pointer(integer(TestPtr) +
2*sizeof(test));

This makes walking through an array with a pointer much easier, from a
readability standpoint.

Bob Lee


Wayne Niddery

unread,
Mar 7, 1998, 3:00:00 AM3/7/98
to

Robert Lee wrote in message <35004CC9...@nwu.edu>...

>Wayne's example is perfectly adequate, but I thought I'd chime in with a
couple
>of options to his example
>
>If you always use a variable as in index (as opposed to the constants in
>Wayne's example) you can define the array as:
>
>type
> TestArray=array[0..0] of Test;


A caveat here is you need to turn off range-checking for this to work,
that's why I prefer the other way (a max value).

0 new messages