Difference between revisions of "Dynamic array"

From Free Pascal wiki
Jump to navigationJump to search
Line 29: Line 29:
 
The index of a dynamic array is ZERO based, ie. it must be within 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 within the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.
  
Actually, dynamic arrays are pointers with automatic dereferencing which are initialized to <b>nil</b>. This means, that <b>MyVariable</b> is interpreted as a pointer variable when handed over to low level routines like <b>fillchar</b>, <b>sizeof</b>, etc. but it is automatically expanded to <b>MyVariable^</b> when indexing its elements (as in <b>MyVariable[2]</b>) or when handed over to routines that expect array types.
+
Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to <b>nil</b> automatically. This means, that <b>MyVariable</b> is interpreted as a pointer variable when handed over to low level routines like <b>fillchar</b>, <b>sizeof</b>, etc. but it is automatically expanded to <b>MyVariable^</b> when indexing its elements (as in <b>MyVariable[2]</b>) or when handing it over to routines that expect array types.
  
 
From memory management view dynamic array variables are simple pointers. <b>SetLength</b> allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the computer memory is made available again. In fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.   
 
From memory management view dynamic array variables are simple pointers. <b>SetLength</b> allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the computer memory is made available again. 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]]
  
Assigning <b>nil</b> to a dynamic variable automatically frees the memory where the pointer pointed to. It's identical to <b>SetLength(MyVariable,0)</b>. This can have a side effect, if the pointer value is not valid for some reason (i.e., if it was read from disk where it was stored from previous program runs). To init such an invalid pointer you have to use <b>fillchar(MyVariable,sizeof(MyVariable),#0)</b>.
+
Assigning <b>nil</b> to a dynamic array variable automatically frees the memory where the pointer pointed to. It's identical to <b>SetLength(MyVariable,0)</b>. This can have a side effect, if the pointer value is not valid for some reason (i.e., if it was read from disk where it was stored from previous program runs). To init such an invalid pointer you have to use <b>fillchar(MyVariable,sizeof(MyVariable),#0)</b>.

Revision as of 12:51, 31 July 2011

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 inserting 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 within the range from 0 to (Length-1). It is NOT possible to change this to a ONE based system.

Actually, dynamic arrays are pointers with automatic dereferencing. They are initialized to nil automatically. This means, that MyVariable is interpreted as a pointer variable when handed over to low level routines like fillchar, sizeof, etc. but it is automatically expanded to MyVariable^ when indexing its elements (as in MyVariable[2]) or when handing it over to routines that expect array types.

From memory management view dynamic array variables are simple pointers. SetLength allocates and frees memory on the heap as needed. When used in functions or procedures only the pointer is added to the stack. When the procedure exits, the dynamic array variable is removed and the computer memory is made available again. In fact, the memory management procedures are inserted in the executable program and the result is totally transparent to the programmer.

Assigning nil to a dynamic array variable automatically frees the memory where the pointer pointed to. It's identical to SetLength(MyVariable,0). This can have a side effect, if the pointer value is not valid for some reason (i.e., if it was read from disk where it was stored from previous program runs). To init such an invalid pointer you have to use fillchar(MyVariable,sizeof(MyVariable),#0).