Newsgroup: it.comp.lang.delphi
Titolo: Come utilizzare gli array dinamici in Delphi 3?

Ho sentito che nel Delphi 3 non esistono gli array dinamici ma bisogna usare
l'oggetto TList.
Il TList però (non ne sono sicuro) non permette di fare liste di valori ma
solamente di puntatori, arrivando al dunque non sono proprio riuscito a
utilizzarlo per crearmi un array dinamico.
Per esempio se io ho un FOR con indice che va da 0 a N e all'interno di
questo FOR troviamo una variabile dato di tipo integer che assume un valore
diverso per ogni ciclo del FOR e quindi voglio salvare ad ogni ciclo il
valore di dato in un array dinamico, come devo fare?
Grazie per qualsiasi suggerimento!!!

valore di dato in un array dinamico, come devo fare?
devi fare un' array dinamico.
Oddio....potresti utilizzare anche la classe TStringList (che è una lista di
stringhe dinamica) ma poi devi convertire con IntToStr e StrToInt.
Si può fare ed è semplicissimo, solamente che non è elegante !! eheh
Comunque visto che non c' è un' area repository, posto qui un messaggio
reperito nel vecchio newsgroup (area repository ) di MRCoke. L' esempio (che
non ho mai provato) permette di avere gli array dinamici senza Delphi 4 o la
classe TList. (credo che sia made in Borland )
Leggete:
============================================================================
=========
2949:Dynamically Allocating Arrays
KEYWORDS: DYNAMIC SIZE ARRAY RUN-TIME AREA: General
{
Dynamic Arrays
Is it possible to create a dynamically-sized array in Delphi?
Yes.  First, you need to create an array type using the largest
size you might possibly need.  When creating a type, no memory
is actually allocated.  If you created a variable of that type,
then the compiler will attempt to allocate the necessary memory
for you.  Instead, create a variable which is a pointer to that
type.  This causes the compiler to only allocate the four bytes
needed for the pointer.
Before you can use the array, you need to allocate memory for
it.  By using AllocMem, you will be able to control exactly how
many bytes are allocated.  To determine the number of bytes
you'll need to allocate, simply multiply the array size you
want by the size of the individual array element.  Keep in mind
that the largest block that can be allocated at one time in a
16-bit environment is 64KB.  The largest block that can be
allocated at one time in a 32-bit environment is 4GB.  To
determine the maximum number of elements you can have in your
particular array (in a 16-bit environment), divide 65,520 by
the size of the individual element.
Example:  65520 div SizeOf(LongInt)
Example of declaring an array type and pointer:
type
  ElementType = LongInt;
const
  MaxArraySize = (65520 div SizeOf(ElementType));
    (* under a 16-bit environment *)
type
  MyArrayType = array[1..MaxArraySize] of ElementType;
var
  P: ^MyArrayType;
const
  ArraySizeIWant: Integer = 1500;
Then when you wish to allocate memory for the array, you could
use the following procedure:
procedure AllocateArray;
begin
  if ArraySizeIWant <= MaxArraySize then
    P := AllocMem(ArraySizeIWant * SizeOf(LongInt));
end;
Remember to make sure that the value of ArraySizeIWant is less
than or equal to MaxArraySize.
Here is a procedure that will loop through the array and set a
value for each member:
procedure AssignValues;
var
  I: Integer;
begin
  for I := 1 to ArraySizeIWant do
    P^[I] := I;
end;
Keep in mind that you must do your own range checking.  If you
have allocated an array with five members and you try to assign
a value to the sixth member of the array, you will not receive
an error message.  However, you will get memory corruption.
Remember that you must always free up any memory that you
allocate.  Here is an example of how to dispose of this array:
procedure DeallocateArray;
begin
  P := AllocMem(ArraySizeIWant * SizeOf(LongInt));
end;
Below is an example of a dynamic array:
}
unit Unit1;
interface
uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls;
type
  ElementType = Integer;
const
  MaxArraySize = (65520 div SizeOf(ElementType));
    { in a 16-bit environment }
type
  { Create the array type.  Make sure that you set the range to
    be the largest number you would possibly need. }
  TDynamicArray = array[1..MaxArraySize] of ElementType;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  { Create a variable of type pointer to your array type. }
  P: ^TDynamicArray;
const
  { This is a typed constant.  They are actually static
    variables hat are initialized at runtime to the value taken
    from the source code.  This means that you can use a typed
    constant just like you would use any other variable.  Plus
    you get the added bonus of being able to automatically
    initialize it's value. }
  DynamicArraySizeNeeded: Integer = 10;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
  { Allocate memory for your array.  Be very careful that you
    allocate the amount that you need.  If you try to write
    beyond the amount that you've allocated, the compiler will
    let you do it.  You'll just get data corruption. }
  DynamicArraySizeNeeded := 500;
  P := AllocMem(DynamicArraySizeNeeded * SizeOf(Integer));
  { How to assign a value to the fifth member of the array. }
  P^[5] := 68;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  { Displaying the data. }
  Button1.Caption := IntToStr(P^[5]);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  { Free the memory you allocated for the array. }
  FreeMem(P, DynamicArraySizeNeeded * SizeOf(Integer));
end;
end.
============================================================================
=======
Ok, scusate del messaggione, ma spero di aver aiutato Marco !!
Ciaooooo !!!
Antonio Tarolla
BSSoftware Tips Mailing - List Support

2949:Dynamically Allocating Arrays
Direi che è un documento abbastanza datato (tra l'altro considera ancora i
limiti dei 16 bit)...
Questo qua sotto proprio non ti va?
type TIntegerList=class(Tlist)
  private
   function GetInt(index:integer):Integer;
   procedure SetInt(index, valore:integer);
  public
   destructor Destroy; override;
   Procedure AddInteger(valore:integer);
   property Interi[index:integer]:integer read GetInt write SetInt;
  end;
destructor TIntegerList.Destroy;
var i:integer;
begin
 for i:=0 to Count-1 do Dispose(Items[i]);
 inherited Destroy;
end;
function TIntegerList.GetInt(index:integer):Integer;
var valore:^Integer;
begin
 valore:=Items[index];
 result:=valore^;
end;
procedure TIntegerList.SetInt(index, valore:integer);
var Nuovo:^Integer;
begin
 Nuovo:=items[index];
 Nuovo^:=valore;
end;
Procedure TIntegerList.AddInteger(valore:integer);
var Nuovo:^Integer;
begin
 new(Nuovo);
 Nuovo^:=valore;
 Add(Nuovo);
end;
Non è più semplice? ;-)
Ciao :-)
--
Andrea Magni
E-mail: 
ama...@energy.it