Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Typed Pointer A...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 3667 of 3852
Post > Topic >>

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

by "Skybuck Flying" <spam@[EMAIL PROTECTED] > Mar 29, 2008 at 08:26 AM

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 := @[EMAIL PROTECTED]
 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.
 




 4 Posts in Topic:
Typed Pointer Assignment via Untyped Pointer Parameter not possi
"Skybuck Flying"  2008-03-29 08:26:07 
Re: Typed Pointer Assignment via Untyped Pointer Parameter not p
"Skybuck Flying"  2008-03-29 08:36:05 
Re: Typed Pointer Assignment via Untyped Pointer Parameter not p
"Skybuck Flying"  2008-03-29 08:37:14 
Re: Typed Pointer Assignment via Untyped Pointer Parameter not p
"Skybuck Flying"  2008-03-29 08:41:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 0:03:00 CDT 2008.