#1 2016-10-19 23:43:23

Bo
Member
From: Melbourne
Registered: 2016-07-04
Posts: 48
Website

Re: How function results are allocated

Hi Ab,

I am reading your blog "How function results are allocated" (http://blog.synopse.info/post/2012/04/1 … -allocated) and running the code you provided in the blog to understand the problem, but what I have found is that the result is different, it was still 'test' instead of 'toto'. I am running code in Berlin 10.1 as following:

function GetString: string;
// is compiled as procedure GetString(var result: string);
begin
  if result='' then
    result := 'test' else
  writeln('result=',result);
end;
function GetRaise: string;
// is compiled as procedure GetRaise(var result: string);
begin
  result := 'toto';
  raise Exception.Create('Problem');
end;

var s: string;
begin
  // here s=''
  s := GetString; // called as GetString(s);
  // here s='test'
  s := GetString; // called as GetString(s);
  // will write 'result=test' on the console
  try
    s := GetRaise; // called as GetRaise(s);
  finally
    // here s='toto'
    writeln('s=',s);
    Readln(s);
  end;
end.

Have something changed in Delphi since you wrote the blog or have I done something wrong?

Offline

#2 2016-10-20 09:09:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,504
Website

Re: Re: How function results are allocated

With Delphi 7 and Delphi 10.1, the last s is 'toto', as expected.

result=test
s=toto

Offline

#3 2016-10-20 10:57:02

Bo
Member
From: Melbourne
Registered: 2016-07-04
Posts: 48
Website

Re: Re: How function results are allocated

ab wrote:

With Delphi 7 and Delphi 10.1, the last s is 'toto', as expected.

result=test
s=toto

This is strange, mine running result did not get "result=test" either!!!

Same result with XE5 and 2007.

open?id=0B8uyyClosvkAclFGR240YV9VTjA

Last edited by Bo (2016-10-20 11:11:34)

Offline

#4 2016-10-22 05:17:55

Bo
Member
From: Melbourne
Registered: 2016-07-04
Posts: 48
Website

Re: Re: How function results are allocated

Hi Ab,

Now I can reproduce your result. The difference is the "s" must not be a global variable.

Offline

Board footer

Powered by FluxBB