Alex Rivera | Logout

Are the members in the statement with AND operator always checked in the given order?

Asked 2012-03-15T10:52:01.443
11

I would like to know if the following code may ever fail with the access violation or if it's safe. Is the first member of the statement with AND operator always checked as first or may be (by some compiler optimization or something) checked the second one as first ?

var
  Item: TSomething;

procedure DoSomething;
begin
  if Assigned(Item) and (Item.SomeProperty) then
    DoSomethingElse;
end;

Is the code above definitely safe ?

Thanks!

Edit
Report

1 Answer

3

Its safe to Call the condition you mentioned Have a look

I have a stringlist and I checked for the two conditions

    Strinlst : TStringlist; 

      Tester.pas.169: if ( (Assigned(Strinlst)) and(Strinlst.count<>6)) then
      0052A3CC 8BB3E4030000     mov esi,[ebx+$000003e4]
      0052A3D2 85F6             test esi,esi              //check if Strinlst is assigned
      0052A3D4 741F             jz $0052a3f5              //jump out if not true 
      0052A3D6 8BC6             mov eax,esi
      0052A3D8 8B10             mov edx,[eax]
      0052A3DA FF5214           call dword ptr [edx+$14]
      0052A3DD 83F806           cmp eax,$06               //compare the count
      0052A3E0 7413             jz $0052a3f5              //jump to the result

Same goes for the condition in other order

      Tester.pas.169: if ( (Strinlst.count<>6) and (Assigned(Strinlst)) ) then
      0052A3CB 8B83E4030000     mov eax,[ebx+$000003e4]
      0052A3D1 8B10             mov edx,[eax]
      0052A3D3 FF5214           call dword ptr [edx+$14]   //get the count
      0052A3D6 83F806           cmp eax,$06                //compare the count    
      0052A3D9 741B             jz $0052a3f6               //jump if not true  
      0052A3DB 83BBE403000000   cmp dword ptr [ebx+$000003e4],$00 //compare if Strinlst is assigned
      0052A3E2 7412             jz $0052a3f6              //jump if false

so certainly the order of conditions is followed from Left to Right

answered 2012-03-15T11:25:05.497

Your Answer