多个字符串变量真的可以引用相同的数据吗?

发布于 2024-10-22 06:38:50 字数 275 浏览 0 评论 0原文

根据网上的信息我发现以下两个变量 指向内存中的同一位置。

任何人都可以提出一个代码示例来证明它实际上是正确的(例如,通过更改第一个变量中的一个字母,并看到此更改从第二个变量中可见)?

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b: String;
begin
  a := 'Test';
  b := a;

  showmessage (a);
  showmessage (b);
end;

According to the information in the internet I found out that two following variables
point to the same place in memory.

Could anyone propose a code example to demonstrate that in fact it is true (e.g. by changing one of the letter in the 1st variable and see that this change is visible from the second variable)?

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b: String;
begin
  a := 'Test';
  b := a;

  showmessage (a);
  showmessage (b);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你穿错了嫁妆 2024-10-29 06:38:50
procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  ShowMessage(BoolToStr(pointer(a) = pointer(b), true));
end;

结果是 True,所以是的,ab 指向相同的数据。

但请注意,它

procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  b := 'Test2';
  ShowMessage(BoolToStr(pointer(a) = pointer(b), true));
end;

应该显示 False

另外,请注意,

procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  ShowMessage(BoolToStr(@a = @b, true));
end;

还显示 False,因为 ab不同字符串(指针)变量,因此内存中某个地方(@a)是a数据的地址,其他地方(@b)是b 的数据。第一个示例表明,内存中的这两个位置包含相同的地址,即 ab 包含相同的数据。

procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  ShowMessage(BoolToStr(pointer(a) = pointer(b), true));
end;

The result is True, so yes, a and b point to the same data.

Notice, however, that

procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  b := 'Test2';
  ShowMessage(BoolToStr(pointer(a) = pointer(b), true));
end;

displays False, as it should be.

In addition, notice that

procedure TForm4.FormCreate(Sender: TObject);
var
  a, b: string;
begin
  a := 'Test';
  b := a;
  ShowMessage(BoolToStr(@a = @b, true));
end;

also displays False, because a and b are different string (pointer) variables, so at some place in memory (@a) is the address of the data of a, and somewhere else (@b) is the address of the data of b. The first example shows that these two places in memory contain the same address, that is, that a and b contain the same data.

风筝有风,海豚有海 2024-10-29 06:38:50

通常,Delphi 对字符串使用“写时复制”语义,因此您需要一种黑客技术来实现这一目的,例如:

procedure TForm13.Button1Click(Sender: TObject);
const
  Test: string = '12345';

var
  S1, S2: string;
  P: PChar;

begin
  SetString(S1, PChar(Test), 5);
// we need to copy '12345' string from readonly memory to heap
  S2:= S1;
// Now both S1 and S2 points to the same memory

  P:= Pointer(S1);
  P^:= 'A';
  ShowMessage(S2);  // 'A2345'
end;

Normally Delphi use 'copy-on-write' semantics for strings, so you need a kind of hacking to do the trick, for example:

procedure TForm13.Button1Click(Sender: TObject);
const
  Test: string = '12345';

var
  S1, S2: string;
  P: PChar;

begin
  SetString(S1, PChar(Test), 5);
// we need to copy '12345' string from readonly memory to heap
  S2:= S1;
// Now both S1 and S2 points to the same memory

  P:= Pointer(S1);
  P^:= 'A';
  ShowMessage(S2);  // 'A2345'
end;
最初的梦 2024-10-29 06:38:50
var
  a, b: string;
begin
  a := 'Test';
  a := a + '!'; // added after Rob's comment below, 
                // makes sure a points to an allocation on the heap
  b := a;
  PChar(b)[3] := 'T';
  ShowMessage(a); //--> TesT!
end;
var
  a, b: string;
begin
  a := 'Test';
  a := a + '!'; // added after Rob's comment below, 
                // makes sure a points to an allocation on the heap
  b := a;
  PChar(b)[3] := 'T';
  ShowMessage(a); //--> TesT!
end;
波浪屿的海角声 2024-10-29 06:38:50

你的问题对我来说不是很清楚。
如果你这样做:

begin 
  a := 'Test';
  b := a;
  a := a+'HH';
  showmessage (a);
  showmessage (b); 
end;

我想你会看到它......

Your question is not very clear for me.
If you do :

begin 
  a := 'Test';
  b := a;
  a := a+'HH';
  showmessage (a);
  showmessage (b); 
end;

I think you will see it...

ぃ双果 2024-10-29 06:38:50

OK,这段代码可能会更清楚

procedure TForm1.FormCreate(Sender: TObject);

var
  a, b , s : string;
  p : pointer;
begin
  a := 'Test';
  b := a;
// we see the 2 diff. var pointing on the same adress
  s := Format('%p -> %p / %p -> %p', [@pointer(a),pointer(a),@pointer(b),pointer(b)] );
  ShowMessage( 'first : '+s);
// we see the 2 diff. var pointing on different adresses
  a := 'Test2';
  s := Format('%p -> %p / %p -> %p', [@pointer(a),pointer(a),@pointer(b),pointer(b)] );
  ShowMessage( 'second : '+s);
end;

OK may be more clear with this code

procedure TForm1.FormCreate(Sender: TObject);

var
  a, b , s : string;
  p : pointer;
begin
  a := 'Test';
  b := a;
// we see the 2 diff. var pointing on the same adress
  s := Format('%p -> %p / %p -> %p', [@pointer(a),pointer(a),@pointer(b),pointer(b)] );
  ShowMessage( 'first : '+s);
// we see the 2 diff. var pointing on different adresses
  a := 'Test2';
  s := Format('%p -> %p / %p -> %p', [@pointer(a),pointer(a),@pointer(b),pointer(b)] );
  ShowMessage( 'second : '+s);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文