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
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...
>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
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
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
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
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).