Recent

Author Topic: Assigned record checking  (Read 1513 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Assigned record checking
« on: October 13, 2020, 12:54:00 pm »
Why there's checking if record (TRect) is assigned (it is unit types.pp, there's more cases like this)?
Code: Pascal  [Select][+][-]
  1. function InflateRect(var Rect: TRect; dx: Integer; dy: Integer): Boolean;
  2. begin
  3.   if Assigned(@Rect) then
  4.   begin
  5.     with Rect do
  6.     begin
  7.       dec(Left, dx);
  8.       dec(Top, dy);
  9.       inc(Right, dx);
  10.       inc(Bottom, dy);
  11.     end;
  12.     Result := True;
  13.   end
  14.   else
  15.     Result := False;
  16. end;
I know it is passed by reference, but still it is a record. In Delphi it is procedure, not function.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Assigned record checking
« Reply #1 on: October 13, 2020, 02:28:49 pm »
that is incase it gets past from the pointer source and it's empty.

but you are correct, that is kind of over kill...

I don't recall seeing that code, where did you find that cause that is inefficient ?

EDIT:
 oh, I see, its in types...

 bad code indeed...

Edit2:
 actually come to think about it, its actually most likely faster code but really, the assignment test should not be there and it could be inlined too.

« Last Edit: October 13, 2020, 02:33:42 pm by jamie »
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Assigned record checking
« Reply #2 on: October 13, 2020, 02:54:27 pm »
I also didn't see the assignment test elsewhere.

If someone wants to play with pointers then he/she must be responsible - example:
Code: Pascal  [Select][+][-]
  1. var p: PWord;
  2. begin
  3.   p:=nil;
  4.   Form1.KeyDown(p^, []);
  5. end;
Here the first parameter is also passed by reference and there's no checking, no compiler complaints, it simply crashes.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Assigned record checking
« Reply #3 on: October 13, 2020, 05:13:55 pm »
Most probably someone used boiler-plate code without fully realizing the difference between a passed record and, say, an object.

Happens all the time (at least to me :-X): you keep so much state in your head that some details go missing and you find yourself doing something by rote instead of thinking it more carefully. As in, say, inserting unneeded try..finally blocks and such like.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Assigned record checking
« Reply #4 on: October 14, 2020, 12:45:05 pm »
I know it is passed by reference, but still it is a record. In Delphi it is procedure, not function.

I'd say that the code was simply copied from Types.OffsetRect and adjusted. I take it that no one checked whether Delphi implemented InflateRect the same.

By the way: Delphi also returns False if you pass a Nil parameter to OffsetRect instead of raising an exception:

Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils, Types;
  3.  
  4. begin
  5.   Writeln(BoolToStr(OffsetRect(PRect(Nil)^, 0, 0), True));
  6. end.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Assigned record checking
« Reply #5 on: October 14, 2020, 01:33:13 pm »
its a windows function in Delphi via the api 

it returns false if it fails but you need to use the getlastError to know why it failed. I guess it sets the lasterror code.

 I guess there could be a couple of reasons whey it would fail, nil rect or rect size is already overlapped


 
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Assigned record checking
« Reply #6 on: October 15, 2020, 10:53:06 am »
its a windows function in Delphi via the api

Since it's a function declared in the Types unit it might forward to the Windows API function on Windows however on non-Windows it will be implemented directly. (Would be interesting to test whether they thought of the Rect parameter being Nil when they implemented that cross platform function :P )

 

TinyPortal © 2005-2018