On Tue, 3 Apr 2001 14:27:22 +0930, "Terry Russell"
Quote
<trochi...@bigpond.com> wrote:
>> procedure TForm1.Button2Click(Sender: TObject);
>> begin
>> TestERROR( PMyRect(nil)^ ); // error ! :)
>> end;
>the error occurs before it gets to the body of TestERROR
>'copy integer at address pointed to by nil' BANG
>when Tmyrect is large it passes the pointer you gave it instead
>'copy nil '
>Should the code behaviour change depending on an object's size?
I hope not ! :)
Pascal should be rock steady.
Let c and c++ be the optimized versions and have strange behaviour :)
like they already have :)
To be honest.
This is the first time I have seen pascal behave strangely.
Quote
>It would be nice if it did not , but:
> The only time it will fail is when you use nil .
> Any behaviour as a result of using nil pointers is undefined, apart from the test for nil.
> Using const or other pass-by-value expects a valid object and the compiler is free to arrange
> things as it sees fit.
> The procedural call starts at the '(' in the calling line, not on entry into the code body.
> You passed a bad value.
>The compiler is doing exactly what you told it to do. Not what you intended ;-)
>If you want to test for possible nil pointers you should use a var,
>ie 'I will accept anything'
>and test pointers.
>or
> try
> TestERROR( PMyRect(nil)^ ); // error ! :)
> except
> on EAccessViolation do
> ShowMessage('Bad params');
> end;
>or
> if assigned( nil ) then
This code would never execute, right ?
Because it is always false ?
So it's useless :) Correct me if I am wrong :)
Thinking about that...
Isn't it strange the compiler doesn't give a warning ?
Like:
Warning this code always evaluates to false.
Or
Warning this code will never be reached.
Quote
> TestERROR( PMyRect(nil)^ );
I learned from this exception code of yours :)
( I didn't know that accessviolations could be catched :) )
I never catch exceptions in delphi.
I talked to a young teacher once about c++ and he said that I should
only catch exceptions when realy necessary...
I like that idea... it keeps my code short and clean and it saves me
time.
So having said that... a programmer should analyse his code every week
or something to analyse if his code has risky parts like the above
code and use exception handling.
Type-casting nil to a pointer type and then dereferencing it is not a
healthy thing to do :)
The funny part is... I would never have done it this way.
I downloaded the DelphiX components from the following site:
http://www.yks.ne.jp/~hori/index-e.html
DelphiX are Delphi Direct X components for making graphics.
I looked at the code and the japan dude :) used that code.
PRect(nil)^
And I thought how the hell can that work ?
Then I tested it with a smaller record and BANG. :)
That japan dude should use exception handling code just for that tiny
piece of code just to be cool :) and make me smile :)