Consider this code:
program Project11;
{$APPTYPE CONSOLE}
uses
SysUtils;
function Test: string;
var
r, s: string;
begin
r := 'Hello';
Writeln(Format('%.8x', [NativeInt(Pointer(r))]));
s := r;
Writeln(Format('%.8x', [NativeInt(Pointer(s))]));
Result := r;
Writeln(Format('%.8x', [NativeInt(Pointer(Result))]));
end;
var
S1: string;
begin
try
S1:= Test;
Writeln(Format('%.8x', [NativeInt(Pointer(S1))]));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
When running it on Delphi XE I get
0040F358
0040F358
020A28CC
020A28CC
The question: why the compiler makes a deep copy when assigning a local string variable to the function's result?