I was coding today and something strange happened. Somehow at some point part of my string was changed although I wasn't changing it (so I thought...)
Long story short, the string was changed, but by different function, which I didn't pass the variable as pointer or "var s: string"
I quickly came to the conclusion, if function is "inline" then parameters variables are passed as reference/pointer, but not always!
I wrote this POC code to show it in action. The strangest thing is
the TWO functions MUST be inlined for the "magic" to happen, why is that? Is this kind of behavior documented somewhere? Some FPC dev shall explain?
Request: I don't have Delphi and I'm curious, please compile this code and post here if the string was replaced to "magic".
Quick poll: did you know about this? I didn't.
program magic;
procedure test(s: string); inline;
begin
//this procedure actually does nothing
//it just changes it's "s" variable (copy of?) to "magic"
s := 'magic';
end;
procedure main; inline;
var
some_string: string;
begin
//set some string
some_string := 'some value';
//call function that "does nothing"
test(some_string);
//expected "some value", shows "magic"
writeln(some_string);
readln;
end;
begin
main;
end.
It's not because it's string, it does the same with any type, lets check integer:
program magic;
procedure test(x: integer); inline;
begin
x := 1234;
end;
procedure main; inline;
var
int_test: integer;
begin
//set some integer
int_test := 555;
//call function that "does nothing"
test(int_test);
//expected "555", shows "1234"
writeln(int_test);
readln;
end;
begin
main;
end.
Regards