开发者

Static variable of procedure in Delphi

开发者 https://www.devze.com 2022-12-18 19:14 出处:网络
What is difference in memory management of variables a and b? Are they both similar static variables but visibility of b is local?

What is difference in memory management of variables a and b?

Are they both similar static variables but visibility of b is local?

Is it ok to declare static variable in procedure or function?

const 
  a: s开发者_如何学Gotring = 'aaa';

procedure SubMethod;
const 
  b: string = 'bbb';
begin
  a := a + 'a';
  b := b + 'b';
end;


Yes, they are the same. As you can see from the disassembly, 'a' and 'b' live in sequential memory locations:

Unit26.pas.32: a := a + 'a';
004552C8 B814874500       mov eax,$00458714
004552CD BAF0524500       mov edx,$004552f0
004552D2 E809F8FAFF       call @LStrCat
Unit26.pas.33: b := b + 'b';
004552D7 B818874500       mov eax,$00458718
004552DC BAFC524500       mov edx,$004552fc
004552E1 E8FAF7FAFF       call @LStrCat

In my case, @a = $00458714, @b = $00458718.

Note, however, that you have to enable Assignable typed constants setting to compile such code.

If you don't have this setting enabled, you have to move 'b' out of the procedure. The following code will not compile.

var
  a: string = 'aaa';

procedure SubMethod;
var
  b: string = 'bbb';  // <-- compilation stops here
begin
  a := a + 'a';
  b := b + 'b';
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消