Difference between revisions of "Dynamic array"

From Free Pascal wiki
m
m
Line 12: Line 12:
 
   ...
 
   ...
 
   <b>end</b>
 
   <b>end</b>
You can put as many <b>SetLength</b> statements as you want in your program in order to expand, or truncate an array but you must put at least one statement before you can use the variable for the first time.  
+
You can put as many <b>SetLength</b> statements as you want in your program in order to expand or truncate an array but you must put at least one statement before you can use the variable for the first time.  
  
The individual elements can be accessed as usual:
+
The individual elements can be accessed as follows:
 
   ...
 
   ...
 
   SetLength(MyVariable,19);
 
   SetLength(MyVariable,19);
Line 24: Line 24:
 
   writeln('Myvariable has ',length(MyVariable),' elements');  {should be 19}
 
   writeln('Myvariable has ',length(MyVariable),' elements');  {should be 19}
 
   ...
 
   ...
   writeln('Its range is ',low(MyVariable),' to ',low(MyVariable)); {should be 0 to 18}
+
   writeln('Its range is ',low(MyVariable),' to ',high(MyVariable)); {should be 0 to 18}
 
   ...
 
   ...
  
 
The index of a dynamic array is ZERO based, ie. it must be whittin the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.
 
The index of a dynamic array is ZERO based, ie. it must be whittin the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.
  
As the program or the procedure exits, the variable is removed and the computer memory is available again. This behaviour suggest that array is put on the stack, but some memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.   
+
As the program or the procedure exits, the dynamic array variable is removed and the computer memory is made available again. This behaviour suggest that the stack memory is used, but in fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.   
 
[[category:Pascal]]
 
[[category:Pascal]]

Revision as of 04:08, 16 January 2010

The dynamic array type is a very nice feature of the freepascal syntax. It is very similar to the array type, but allows more flexibility for the programmer since the number of elements does not need to be known until the program execution.

The declaration part is just as simple as for the array type:

 var
 ...
 MyVariable : array of type;
 ...

The number of elements can be set or modified whenever needed during the execution of the program by using the statement:

 begin
 ...
  SetLength (MyVariable, ItsNewLength);
 ...
 end

You can put as many SetLength statements as you want in your program in order to expand or truncate an array but you must put at least one statement before you can use the variable for the first time.

The individual elements can be accessed as follows:

 ...
 SetLength(MyVariable,19);
 ...
 MyVariable[18] := 123;
 ...
 MyOtherVariable := MyVariable[0];
 ...
 writeln('Myvariable has ',length(MyVariable),' elements');  {should be 19}
 ...
 writeln('Its range is ',low(MyVariable),' to ',high(MyVariable)); {should be 0 to 18}
 ...

The index of a dynamic array is ZERO based, ie. it must be whittin the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.

As the program or the procedure exits, the dynamic array variable is removed and the computer memory is made available again. This behaviour suggest that the stack memory is used, but in fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.