RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Assigned Function

Tests for a nil (unassigned) pointer or procedural variable.

Pascal
function Assigned(const P): Boolean;
C++
Boolean Assigned(const  P);

Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable. 

Assigned returns false if P is nil, true otherwise.

Note: Assigned can't detect a dangling pointer--that is, one that isn't nil but no longer points to valid data. For example, in the code example for Assigned, Assigned won't detect the fact that P isn't valid.
 

Delphi Examples: 

 

{
This example demonstrates the System Assigned function succeeds.
}
procedure TForm1.Button1Click(Sender: TObject);
var P: Pointer;
begin
  P := nil;
  if Assigned (P) then
    MessageDlg('You won''t see this', mtInformation, [mbOK], 0);
  GetMem(P, 1024);  {P valid}
  FreeMem(P, 1024); {P no longer valid and still not nil}
  if Assigned (P) then
    MessageDlg('You''ll see this', mtInformation, [mbOK], 0);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!