var
str1 ,str2: string;procedure TForm1.Button1Click(Sender: TObject);
begin
str1 := 'abcd';
str2 := str1;
Memo1.Lines.Add(IntToStr(Integer(@str1[1]))) ; //注释掉这句和添加这句对结果影响很大。
Memo1.Lines.Add(IntToStr(Integer(str1))) ;
Memo1.Lines.Add(
'str1 Address: ' + IntToStr(Integer(str1)) + #13#10 +
'str1 Reference: ' + IntToStr(PInteger(Integer(str1) - 8)^) + #13#10 +
'str1 Value: ' + str1 + #13#10 +
'str2 Address: ' + IntToStr(Integer(str2)) + #13#10 +
'str2 Reference: ' + IntToStr(PInteger(Integer(str2) - 8)^) + #13#10 +
'str2 Value: ' + str2 + #13#10
); str2 := '1234';
Memo1.Lines.Add('str2 := ''1234''');
Memo1.Lines.Add(
'str1 Address: ' + IntToStr(Integer(str1)) + #13#10 +
'str1 Reference: ' + IntToStr(PInteger(Integer(str1) - 8)^) + #13#10 +
'str1 Value: ' + str1 + #13#10 +
'str2 Address: ' + IntToStr(Integer(str2)) + #13#10 +
'str2 Reference: ' + IntToStr(PInteger(Integer(str2) - 8)^) + #13#10 +
'str2 Value: ' + str2 + #13#10
);
end;
刚才在测试string引用计数的时候发现1个非常奇怪的问题,就是上面代码中添加注释的那句会影响到引用计数。不知各位对这个是怎样理解的,请教、
解决方案 »
- 如何获得自己进程的THandle?
- 如何给一个SpeedButton按扭绘一个带色的边框.
- 为什么ListView的自绘事件CusterDrawItem里用文字输出函数速度会很慢?
- 菜鸟提问:为何窗体刚打开时DBGRID表格周围要晃动四秒钟左右?
- 向有图标编程经验的大侠问一个问题~~
- 关于数据库安装出错的问题??????在线等急.......
- SOS---一个刚用DELPHI做课程设计得大三学生得紧急求救!谁能救小妹一命,感激万分!
- 谁能告诉我,这是怎么回事?
- Oracle 安装以后登录的缺省用户名和密码是什么?
- Midas程序的服务器怎样在另一台机器上运行?
- delphi2010 读取纯真数据库出现乱码?
- 2010年初大亮语句
这里的赋值只是简单的将str1指向存放abcd的地址, 这个地址-$8 是引用计数 -1; 当你注释掉那句话的时候肯定显示引用计数为-1, 且str2和str1一样, str2和str1是同样的道理为什么你不注释的时候会有问题, 那是因为@str1[1] 的操作会引发UniqueString操作, 这个过程里,delphi检查如果引用计数-1 <>0 则单独分配一块内存, 并拷贝str1指向的内存,也就是abcd, 并且设置新的地址中-$8的位置为1 也就是新的字符串引用计数为1. 故此你看到的地址就和真正原始的'abcd'不是一个地址了, 引用数也不一样了.