Safe pointer arithmetic and typecasts :D
From: Skybuck Flying (nospam_at_hotmail.com)
Date: 03/31/04
- Next message: Skybuck Flying: "Handling Out Of Memory"
- Previous message: Daniel Hobert: "Re: Page Control getting Clipped."
- Next in thread: Bruce Roberts: "Re: Safe pointer arithmetic and typecasts :D"
- Reply: Bruce Roberts: "Re: Safe pointer arithmetic and typecasts :D"
- Reply: Rob Kennedy: "Re: Safe pointer arithmetic and typecasts :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 09:39:33 +0200
Hi,
I always wondered how to do safe pointer aritmetic in delphi without
overflow errors or warnings...
I think I now know how to do it... :)
With Pchar is probably safest. ( Borland examples used Pchar links at the
bottom of this message ;) )
Someone once suggested cardinal but that is also not safe :D
I also wonder about how much memory can be allocated in Windows ?
GetMem's prototype is GetMem(var P: Pointer; Size: Integer);
Integer is limited to maxint(2147483647).
Does that mean only 2 GB can be allocated with getmem ? or is it still
possible to allocate more with a typecast maybe ?
GetMem( p, integer(4294967295) ); // :)
Anyway here is the code demonstration overflow problems or warnings when
using integer typecasts or cardinal typecasts... it also demonstrates using
Pchar for safe typecasting/pointer aritmetic ;)
Also I was worried that Pchar's could lead to debugging problems if Delphi
tried to show the string that it was pointing too when holding
the cursor over it (I think it's called tooltip (inspection of variables
during debugging) )
procedure TForm1.Button1Click(Sender: TObject);
var
p1 : pointer;
p2 : pointer;
size : integer;
s : Pchar;
begin
// pointer on the integer edge :D
integer(p1) := maxint; // 2147483647
// pointer addition arithmetic
size := 100;
// p2 := pointer( integer(p1) + size ); // error: integer overflow ! if
runtime errors enabled.
// p2 := pointer( cardinal(p1) + size ); // warning: combining signed and
unsigned types - widened both operands ?!
// p2 := pointer( cardinal(p1) + cardinal(size) ); // no errors :D and it's
safe :D I hope lol.
p2 := pointer( pchar(p1) + size ); // safe no erros or warnings.
ShowMessage( IntToStr( cardinal(p2) ) ); // 2147483747
s := p2; // test debug tooltip, it wont be displayed so no crashes ;)
ShowMessage( IntToStr( cardinal(s) ) );
// pointer substracting arithmetic
size := -100;
// p2 := pointer( cardinal(p1) + cardinal(size) ); // error: integer
overflow !!!!???? if runtime errors enabled.
// p2 := pointer( cardinal(p1) + (size) ); // warning: combining signed and
unsigned types - widened both operands ?!
p2 := pointer( Pchar(p1) + size ); // safe no errors or warnings.
ShowMessage( IntToStr( cardinal(p2) ) ); // 2147483547
// pointer wrap around 4 GB, a bit extreme but still safe :D lol.
// maybe this should not be allowed lol. I don't know :D
// I hate exceptions anyway... :) eeeevvilll :D
integer(p1) := 0;
size := -100;
p2 := pointer( Pchar(p1) + size ); // safe no errors or warnings.
ShowMessage( IntToStr( cardinal(p2) ) ); // 4294967196 (max longword:
4294967295) ok.
end;
I wonder if anybody has any comments on this :D
If not I'll assume it's super safe and the right way to do it :D
Borland demonstrates using Pchar, though the examples do not touch on these
overflow problems etc... but they do show the usage of pchar :D
http://community.borland.com/article/0,1410,15926,00.html
http://community.borland.com/article/0,1410,16542,00.html
Skybuck.
- Next message: Skybuck Flying: "Handling Out Of Memory"
- Previous message: Daniel Hobert: "Re: Page Control getting Clipped."
- Next in thread: Bruce Roberts: "Re: Safe pointer arithmetic and typecasts :D"
- Reply: Bruce Roberts: "Re: Safe pointer arithmetic and typecasts :D"
- Reply: Rob Kennedy: "Re: Safe pointer arithmetic and typecasts :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]