指针的问题,p:=nil和not Assigned(p)是不是一样的

quicksand201 2005-01-21 12:24:57
如题
...全文
298 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chijingde 2005-01-21
  • 打赏
  • 举报
回复
Tests for a nil (unassigned) pointer or procedural variable.

Unit

System

Category

miscellaneous routines

function Assigned(const P): Boolean;

Description

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.
syl79 2005-01-21
  • 打赏
  • 举报
回复
应该是楼主打错字了吧,如果问p=nil和not assigned(p)是不是相等的,我个人认为是相等的。
syl79 2005-01-21
  • 打赏
  • 举报
回复
p:=nil是把P置为空指针;not assinged (p)是返回一个boolean的表达式,如果P=nil则为true
劣势 2005-01-21
  • 打赏
  • 举报
回复
不可以的,我也是刚查过帮助,看下面的例子(Delphi 的Demo)
var P: Pointer;

begin
P := nil;//(P = nil 但是没有Assigned)
if Assigned (P) then Writeln ('You won''t see this');
GetMem(P, 1024); {P valid}
FreeMem(P, 1024); {P no longer valid and still not nil}//
if Assigned (P) then Writeln ('You''ll see this');
end;
劣势 2005-01-21
  • 打赏
  • 举报
回复
可以的吧,根据自己的喜好了
ly_liuyang 2005-01-21
  • 打赏
  • 举报
回复
不一样的
nil当然不能被Assgined的
但不能Assigned的,不一定是Nil的

http://lysoft.7u7.net
quicksand201 2005-01-21
  • 打赏
  • 举报
回复
看delphi的demo,代码里会出现上述指针的比较,是不是用p=nil的地方都可以用not Assigned(p)来替换,哪一种方法更好更方便?
quicksand201 2005-01-21
  • 打赏
  • 举报
回复
不小心打错了,是 p=nil 和 not Assigned(p) 的比较
劣势 2005-01-21
  • 打赏
  • 举报
回复
只要是指针,这种都可以的,可以使用在类的实例是否创建的判断上
类其实也是指针

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧