home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0032_Dynamically Allocating Arrays.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  4.5 KB  |  158 lines

  1.  
  2. {
  3. Dynamic Arrays
  4.  
  5. Is it possible to create a dynamically-sized array in Delphi?
  6.  
  7. Yes.  First, you need to create an array type using the largest
  8. size you might possibly need.  When creating a type, no memory
  9. is actually allocated.  If you created a variable of that type,
  10. then the compiler will attempt to allocate the necessary memory
  11. for you.  Instead, create a variable which is a pointer to that
  12. type.  This causes the compiler to only allocate the four bytes
  13. needed for the pointer.
  14.  
  15. Before you can use the array, you need to allocate memory for
  16. it.  By using AllocMem, you will be able to control exactly how
  17. many bytes are allocated.  To determine the number of bytes
  18. you'll need to allocate, simply multiply the array size you
  19. want by the size of the individual array element.  Keep in mind
  20. that the largest block that can be allocated at one time in a
  21. 16-bit environment is 64KB.  The largest block that can be
  22. allocated at one time in a 32-bit environment is 4GB.  To
  23. determine the maximum number of elements you can have in your
  24. particular array (in a 16-bit environment), divide 65,520 by
  25. the size of the individual element.
  26. Example:  65520 div SizeOf(LongInt)
  27.  
  28. Example of declaring an array type and pointer:
  29. }
  30. type
  31.   ElementType = LongInt;
  32.  
  33. const
  34.   MaxArraySize = (65520 div SizeOf(ElementType));
  35.     (* under a 16-bit environment *)
  36.  
  37. type
  38.   MyArrayType = array[1..MaxArraySize] of ElementType;
  39. var
  40.   P: ^MyArrayType;
  41.  
  42. const
  43.   ArraySizeIWant: Integer = 1500;
  44.  
  45. Then when you wish to allocate memory for the array, you could
  46. use the following procedure:
  47.  
  48. procedure AllocateArray;
  49. begin
  50.   if ArraySizeIWant <= MaxArraySize then
  51.     P := AllocMem(ArraySizeIWant * SizeOf(LongInt));
  52. end;
  53.  
  54. Remember to make sure that the value of ArraySizeIWant is less
  55. than or equal to MaxArraySize.
  56.  
  57. Here is a procedure that will loop through the array and set a
  58. value for each member:
  59.  
  60. procedure AssignValues;
  61. var
  62.   I: Integer;
  63. begin
  64.   for I := 1 to ArraySizeIWant do
  65.     P^[I] := I;
  66. end;
  67.  
  68. Keep in mind that you must do your own range checking.  If you
  69. have allocated an array with five members and you try to assign
  70. a value to the sixth member of the array, you will not receive
  71. an error message.  However, you will get memory corruption.
  72.  
  73. Remember that you must always free up any memory that you
  74. allocate.  Here is an example of how to dispose of this array:
  75.  
  76. procedure DeallocateArray;
  77. begin
  78.   P := AllocMem(ArraySizeIWant * SizeOf(LongInt));
  79. end;
  80.  
  81. Below is an example of a dynamic array:
  82.  
  83. }
  84.  
  85. unit Unit1;
  86.  
  87. interface
  88.  
  89. uses
  90.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
  91.   Controls, Forms, Dialogs, StdCtrls;
  92.  
  93. type
  94.   ElementType = Integer;
  95.  
  96. const
  97.   MaxArraySize = (65520 div SizeOf(ElementType));
  98.     { in a 16-bit environment }
  99.  
  100. type
  101.   { Create the array type.  Make sure that you set the range to
  102.     be the largest number you would possibly need. }
  103.   TDynamicArray = array[1..MaxArraySize] of ElementType;
  104.   TForm1 = class(TForm)
  105.     Button1: TButton;
  106.     procedure FormCreate(Sender: TObject);
  107.     procedure Button1Click(Sender: TObject);
  108.     procedure FormDestroy(Sender: TObject);
  109.   private
  110.     { Private declarations }
  111.   public
  112.     { Public declarations }
  113.   end;
  114.  
  115. var
  116.   Form1: TForm1;
  117.   { Create a variable of type pointer to your array type. }
  118.   P: ^TDynamicArray;
  119.  
  120. const
  121.   { This is a typed constant.  They are actually static
  122.     variables hat are initialized at runtime to the value taken
  123.     from the source code.  This means that you can use a typed
  124.     constant just like you would use any other variable.  Plus
  125.     you get the added bonus of being able to automatically
  126.     initialize it's value. }
  127.   DynamicArraySizeNeeded: Integer = 10;
  128.  
  129. implementation
  130.  
  131. {$R *.DFM}
  132.  
  133. procedure TForm1.FormCreate(Sender: TObject);
  134. begin
  135.   { Allocate memory for your array.  Be very careful that you
  136.     allocate the amount that you need.  If you try to write
  137.     beyond the amount that you've allocated, the compiler will
  138.     let you do it.  You'll just get data corruption. }
  139.   DynamicArraySizeNeeded := 500;
  140.   P := AllocMem(DynamicArraySizeNeeded * SizeOf(Integer));
  141.   { How to assign a value to the fifth member of the array. }
  142.   P^[5] := 68;
  143. end;
  144.  
  145. procedure TForm1.Button1Click(Sender: TObject);
  146. begin
  147.   { Displaying the data. }
  148.   Button1.Caption := IntToStr(P^[5]);
  149. end;
  150.  
  151. procedure TForm1.FormDestroy(Sender: TObject);
  152. begin
  153.   { Free the memory you allocated for the array. }
  154.   FreeMem(P, DynamicArraySizeNeeded * SizeOf(Integer));
  155. end;
  156.  
  157. end.
  158.