Theodoros Bebekis wrote:
> An recent coding error revealed that it is possible to pass a bounded array
> to a dynamic array. Here is how.
> 
> 
> type
>    TIntArray = array of integer;
>    PIntArray = ^TIntArray;
> 
> 
> (* helper, it displays the ref counter of a dynamic array *)
> procedure DisplayRefCount(A: TIntArray);
> var
>    P : Pointer;
> begin
>    P := A;
> 
>    P := Pointer(Integer(P) - 4);
>    ShowMessage(IntToStr(PInteger(P)^));

That displays the array's length, not its reference count. Be careful 
about passing empty arrays!

Also note that since you're passing the array non-const, the function 
increments the reference count before proceeding with your code.

> (* I have the type address ($TYPEDADDRESS) compiler directive as  {$T-}, the 
> default
>     setting, wich makes the @ operator to return an un-typed pointer.
>     The fact that it is valid to pass an un-typed pointer to a typed pointer,
>     is the source of the error

Yes, well, there's your problem. Always turn on the "typed @ operator" 
compiler option. I've never understood why that's not the default. It 
just makes so much more sense.

I think I asked about it once. Apparently, it's left turned off because 
otherwise some perfectly valid code won't compile, requiring a 
type-cast. Code that needs to equate, say, PInteger with PIntegerArray. 
Also code that takes the address of a resourcestring to get a 
PResStringRec. (The compiler thinks it has the type PString, which is 
wrong.)

-- 
Rob

Reply via email to