Board index » delphi » Creating two-dimensional arrays in Delphi 1

Creating two-dimensional arrays in Delphi 1

Quote
In article <3286f833.8576...@news.tue.nl> P.A.d.Cle...@ele.tue.nl (paul) writes:
>I want to create 2-dim arrays in Delphi 1, for example:
>MyArray: array [1..x] of [1..y], where x and y are variables. I guess
>I have to use the Getmem function, but can somebody send an example
>how to access the array. I have also heard of a 'VarArrayCreate'
>procedure, but i cannot find it in Delphi 1.

What you should do here is to define a class type with an array-style read and
write method... or simply a procedure and a function.  Within these, implement
whatever access-method seems appropriate for obtaining the data at location
[x,y].  Depending on your requirements (e.g. is the array "sparse?"), you may
find that a non-array data structure such as a tree or a hash-table works
better than an actual square array, and unless your CPU requirements are truly
extraordinary it simply won't matter how you do it.

/mr/

 

Re:Creating two-dimensional arrays in Delphi 1


Hello,

I want to create 2-dim arrays in Delphi 1, for example:

MyArray: array [1..x] of [1..y], where x and y are variables. I guess
I have to use the Getmem function, but can somebody send an example
how to access the array. I have also heard of a 'VarArrayCreate'
procedure, but i cannot find it in Delphi 1.

Thanks in advance,
paul

Re:Creating two-dimensional arrays in Delphi 1


The Delphi Help subsystem comes up with the following:

The following example declares an initialized multidimensional array Maze.

  type
    TCube = array[0..1, 0..1, 0..1] of Integer;
  const
    Maze: TCube = (((0, 1), (2, 3)), ((4, 5), (6, 7)));

These are the values for the array Maze:

  Maze[0, 0, 0] = 0
  Maze[0, 0, 1] = 1
  Maze[0, 1, 0] = 2
  Maze[0, 1, 1] = 3
  Maze[1, 0, 0] = 4
  Maze[1, 0, 1] = 5
  Maze[1, 1, 0] = 6
  Maze[1, 1, 1] = 7

paul <P.A.d.Cle...@ele.tue.nl> wrote in article
<3286f833.8576...@news.tue.nl>...

Quote
> Hello,

> I want to create 2-dim arrays in Delphi 1, for example:

> MyArray: array [1..x] of [1..y], where x and y are variables. I guess
> I have to use the Getmem function, but can somebody send an example
> how to access the array. I have also heard of a 'VarArrayCreate'
> procedure, but i cannot find it in Delphi 1.

> Thanks in advance,
> paul

Re:Creating two-dimensional arrays in Delphi 1


Quote
P.A.d.Cle...@ele.tue.nl (paul) wrote:
>Hello,
>I want to create 2-dim arrays in Delphi 1, for example:
>MyArray: array [1..x] of [1..y], where x and y are variables. I guess
>I have to use the Getmem function, but can somebody send an example
>how to access the array. I have also heard of a 'VarArrayCreate'
>procedure, but i cannot find it in Delphi 1.

        Creating this type of array is done easily :

TYPE

        MyArray  :  array[0..100, 3..2000] of integer  (or any other type)

You cannot initialise an array with variables in its arguments because the compiler
doesn't know how many bytes are needed, so it has to be predefined (ex.  array [1..3,1..4]
) or constant-based (ex array[1..FirstVal, 2..LastVal]).

        Forget GetMem if you write simple programs...GetMem is used to allocate memory for
pointer-based variables.
exemple :

        TYPE

                PtrArray : array[1..200] of byte;

                PtrMyArray : ^PtrArray;

                TMyArray : array[1..200] of PtrMyArray;

                {This initialise an array of pointers.}

var

MyVar : TMyArray;

begin...
...
for loop := 1 to 200 do begin
 Getmem (MyVar[loop], 200);     {Get memory...}
end;
...
...
for loop := 1 to 200 do begin
 FreeMem(MyVar[loop],SizeOf ( MyVar[loop] )     {Flush memory...}
end;

Re:Creating two-dimensional arrays in Delphi 1


Quote
P.A.d.Cle...@ele.tue.nl (paul) wrote:
>Hello,
>I want to create 2-dim arrays in Delphi 1, for example:
>MyArray: array [1..x] of [1..y], where x and y are variables. I guess
>I have to use the Getmem function, but can somebody send an example
>how to access the array. I have also heard of a 'VarArrayCreate'
>procedure, but i cannot find it in Delphi 1.

        Creating this type of array is done easily :

TYPE

        MyArray  :  array[0..100, 3..2000] of integer  (or any other type)

You cannot initialise an array with variables in its arguments because the compiler
doesn't know how many bytes are needed, so it has to be predefined (ex.  array [1..3,1..4]
) or constant-based (ex array[1..FirstVal, 2..LastVal]).

        Forget GetMem if you write simple programs...GetMem is used to allocate memory for
pointer-based variables.
exemple :

        TYPE

                PtrArray : array[1..200] of byte;

                PtrMyArray : ^PtrArray;

                TMyArray : array[1..200] of PtrMyArray;

                {This initialise an array of pointers.}

var

MyVar : TMyArray;

begin...
...
for loop := 1 to 200 do begin
 Getmem (MyVar[loop], 200);     {Get memory...}
end;
...
...
for loop := 1 to 200 do begin
 FreeMem(MyVar[loop],SizeOf ( MyVar[loop] )     {Flush memory...}
end;

Other Threads