Typed Pointer Assignment via Untyped Pointer Parameter not possible ?!?!?!



Hello,

For my double linked list I want to pass any type of pointer to it, since
all pointers will basically point to the same kind of memory structure.

For example:

PsomeNode
TsomeNode = packed record
Next
Prev
SomeData
end;

PotherNode
TotherNode = packed record
Next
Prev
OtherData
end;

Notice how both first two fields are the same.

So the list only needs to know about

Pnode
Tnode = packed record
Next
Prev
Data : record end; // empty record
end;

But I don't want to have to write code like this all the time !:

AddBegin( Pnode(SomeNode) );

Notice the nasty typecast.

I would just be happy if AddBegin would just accept any pointer.

However that seems not possible.

See example below:

// *** Begin of Demonstration ***

program Project1;

{$APPTYPE CONSOLE}

{

Tested typed pointer assignment to untyped pointer via parameter.

Version 0.01 created by Skybuck Flying.

I want to write a general function which will accept all pointer types.

All pointer types will point to the same structure heading,

I know what I am doing and I want to get rid of the external typecasts.

Unfortuantely it does not seems possible with Delphi 2007.

Possible work around could be to use untyped data... but this is a bit too
dangerous
for my taste because then there won't be any length checking anymore.

}

uses
SysUtils;

var
P1 : pointer;
P2 : pointer;
P3 : pointer;

procedure Test( var A : pointer );
begin
P2 := A;
end;

// ok better.
procedure DangerousWorkAroundNoLengthChecking( var A );
var
P : pointer absolute A; // no typecast required this way and no extra
variables.
begin
P3 := P;
end;

var
IntP : Pinteger;
MyInt : integer;

Anything : array[0..10] of byte;

begin
try
MyInt := 10;
IntP := @MyInt;

P1 := IntP; // ok
// Test( IntP ); // compile failure.
Test( Pointer(IntP) ); // nasty typecast needed
DangerousWorkAroundNoLengthChecking( IntP );

writeln( longword(P1) );
writeln( longword(P2) );
writeln( longword(P3) );

// no pointer length checking, dangerous !
DangerousWorkAroundNoLengthChecking( Anything );

except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
readln;
end.

// *** End of Demonstration ***

Bye,
Skybuck.


.



Relevant Pages

  • Re: Re: Reading UDTs out of a byte array (CopyMemory vs Get on a disk file)
    ... next right pointer ... pointer into hard format table ... ' crap as integer - offset 34 ... dbsv_offset_value of 1st redefined var ...
    (microsoft.public.vb.winapi)
  • Re: bitcopy in delphi.
    ... Move is also a little bit delphi specific... ... then inc would increase the pointer with 2 instead of just ... So for me overlap is not really an issue. ...
    (alt.comp.lang.borland-delphi)
  • Re: callback function in a component
    ... function CreateStub(ObjectPtr, MethodPtr: pointer): Pointer; ... Result:= Stub; ... // Windows API call which excepts Procedure not Method of Object ...
    (borland.public.delphi.language.objectpascal)
  • The problem simplified.
    ... {Private declarations} ... p: pointer; ... AnsiString(P):= 'This is the string'; ...
    (alt.comp.lang.borland-delphi)
  • Re: Freeing a TList object and its Items
    ... The example you show here comes from the help file (TList.Add ... So the assignment works, and the Dispose ... (It must also be a var.) ... or a pointer called aPointer: ...
    (borland.public.delphi.language.objectpascal)