Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

about dynamic array -- PS

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

spir ☣

Hello,

It seems the actual issue with static array is that the size is part the type. So that one cannot even use a pointer to refer to an array which size is supposed to change (since the pointer's type itself would be constrained by the size). Is this correct? If yes, how is it possible to build a "flexible" array type (in which on can add/remove elements) on top of static array?

Also, a side-question: is resizing a flexible array more efficient than reallocating and copying a static one (provided the above issue is solved)? (I intend to do tests, but fpc's site seems to be down? I need info about timing funcs.)

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Henry Vermaak
to spir ☣

2010/5/6 spir ☣ :

Hello, If yes, how is it possible to build a "flexible" array type (in which on can add/remove elements) on top of static array?

It depends on what you want to do with the array. As Jonas said, you may want to look at linked lists if you need to add/remove elements frequently. There are many other data structures that are optimised for different use cases.

(I intend to do tests, but fpc's site seems to be down? I need info about timing funcs.)

http://www.freepascal.org/docs-html/fcl/fptimer/index.html

or write separate programs and use "time" to execute them (unix only).

Henry

Reply by email

Matthias K.
to spir ☣: It seems the actual issue with static array is that the size is part the type. So that…

2010/5/6 spir ☣ :

Hello, It seems the actual issue with static array is that the size is part the type. So that one cannot even use a pointer to refer to an array which size is supposed to change (since the pointer's type itself would be constrained by the size). Is this correct? If yes, how is it possible to build a "flexible" array type (in which on can add/remove elements) on top of static array?

It is possible but not as clean as using Dynamic Arrays (actually its nearly the same but you need to do the work, the compiler would do for you with dynamic arrays). The following Example uses a Static Array such that the Compiler knows the Field Type.

---- Example:

type
  TMyTypedArray = array[ 0 .. High( Integer ) ] of SomeType;
  PMyTypedArray = ^TMyTypedArray;
  TMyDynArray = record
    len: Integer;
    data: Pointer;
  end;
  PMyDynArray = ^TMyDynArray;

..

procedure Init( var pda: PMyDynArray; len: Integer );
begin
  pda := New( PMyDynArray );
  pda^.len := len;
  GetMem( pda^.data, SizeOf( SomeType )*len ); // allocate array buffer
end;
function getElement( pda: PMyDynArray; index: Integer ): SomeType;
begin
  Assert( ( pda  nil ) and ( index >= 0 ) and ( index < pda^.len ) );
  Result := PMyTypedArray( pda^.data )^[ index ];
end;

----

As you can see, this uses casting. Reallocation/Allocation/Deallocation can be made with ReAllocMem/GetMem/FreeMem. It is possible to build a Dynamic Array for a given Type (or a given Element size and more casting) using own implementations for SetLength, index access (, and boundary checks). Since (possibly relocated) array data is in the pointer TMyDynArray.data, pointers to TMyDynArray can be used as Array references.

The same thing is Possible using own address calculations on Pointers without the static array type. Problem is, the deeper you go (or the more low level using pointers and casts), the less readable/secure this stuff gets.

But it is possible and nearly the same thing as using Dynamic Arrays.

Also, a side-question: is resizing a flexible array more efficient than reallocating and copying a static one (provided the above issue is solved)? (I intend to do tests, but fpc's site seems to be down? I need info about timing funcs.)

Depends.. in general it is not as efficient. Reallocation sometimes can be done more efficient by the OS or a Memory Manager. Worst case needs a full copy, best case doesnt change anything (maybe because theres free memory behind the array). Afaik DynArrays use ReAllocMem too.

Matthias

PS: And never try to declare Variables as TMyTypedArray.. this will simply crash your prog while trying to allocate (0 .. High( Integer ))*SizeOf(SomeType) memory on the Stack. Its simply for Compiler Information, such that Compiler can calcuate "pda^.data + SizeOf(SomeType)*index" for "PMyTypedArray( pda^.data )^[ index ]"..

Reply by email