Hello,
What's the expected output of this code ?
function TheA(): string;
begin
Result:=Result+'A';
end;
writeln(TheA());I thought that when the result type is an automated one its value gets initialized... Maybe I'm wrong...

Mailing lists / fpc-pascal / 2013-03
16 messages · 7 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello,
What's the expected output of this code ?
function TheA(): string;
begin
Result:=Result+'A';
end;
writeln(TheA());I thought that when the result type is an automated one its value gets initialized... Maybe I'm wrong...
Yes you are wrong. It is very illogical because a local string variable is initialized to be empty but the return value is not. Delphi has the same problem.
I once made even a report about it:
http://bugs.freepascal.org/view.php?id=20907
Regards,
Juha
That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1).
I find the current release (2.6.2) initialises a string function result to EmptyStr as you would hope.
{$mode objfpc}
{$h+}
function TheA(): string;
begin
Result:=Result+'A';
end;
Var
C : String;
begin
C:='B';
C:=TheA;
C:=TheA;
Writeln(C);
end.Prints AA
Michael.
That report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1).…
Howard Page-Clark wroteThat report says the issue was assigned to Jonas and fixed in revision 20427 (ver 2.6.1). I find the current release (2.6.2) initialises a string function result to EmptyStr as you would hope.
Ok, that sounds good.
I must have misunderstood what was changed. I thought it was the warning only but apparently the fundamental issue was fixed.
Juha
Yes you are wrong. It is very illogical because a local string variable is initialized to…
Delphi would present this problem "only" in some situations (which is worse), and it didn't even show a warning when the result is a string :-/
-Flávio
{$mode objfpc} {$h+} function TheA(): string; begin Result:=Result+'A'; end; Var C :…
Michael Van Canneyt wrote> That report says the issue was assigned to Jonas and fixed in revision > 20427 (ver 2.6.1). > I find the current release (2.6.2) initialises a string function > result to EmptyStr as you would hope.
[...]
Prints AA
Hello,
I'm asking because today I has updated my old 2.7.1 SVN since 8-9 months and a library starts to fail and the "bug" was the Result string which enters the function with a value while it does not happend in the old one.
If Result as string is not initialized a warning should be issued do not ? This happends in your simple test, but I was unable to find it in the library that I'm using.
The code that makes me wonder something is wrong is this one:
-----------------------------------
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
//Result:='';
TheB(Result);
end;
Var
C : String;
begin
C:='B';
C:=TheA;
C:=TheA;
Writeln('= "',C,'"');
end.
-----------------------------------
In this situation no warning happends and result is different if I
uncomment the "Result:='';" line.I think the missing warning is a bug, the missing warning or the non initialization.
For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA() is initialised manually or not. Perhaps 2.7.1 has a regression here if it differs for you?
Howard
It is not a regression. 2.7.1 has the Delphi compatible behaviour.
Michael.
Seems that we should add a remark here:
http://wiki.lazarus.freepascal.org/User_Changes_Trunk
Regards,
Sven
For me (win32, FPC 2.6.2) the output is identical (= "A") whether or not Result in TheA()…
Howard Page-Clark wrote> The code that makes me wonder something is wrong is this one: >
[...]
Hello,
The problem is not the different behavior as it is or will be noted in documentation, the problem is the lack of warning of using a not initialized variable, so it is hard to fix code that works fine in 2.6.x and that could fail with Trunk.
Should I report a bug in Mantis about the warning ?
FPC 2.6.2 does give a warning (code is from http://bugs.freepascal.org/view.php?id=20907#c55064 ):
$ fpc -vw tt7.pp
Free Pascal Compiler version 2.6.2 [2013/02/03] for powerpc Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Darwin for PowerPC
Compiling tt7.pp
tt7.pp(8,13) Warning: Function result variable does not seem to initialized
Assembling (pipe) tt7.s
Linking tt7
26 lines compiled, 2.6 sec
1 warning(s) issued
so it is hard to fix code that works fine in 2.6.x and that could fail with Trunk.
It could also fail in 2.6.x, just less often.
Jonas
Jonas Maebe wroteFPC 2.6.2 does give a warning (code is from http://bugs.freepascal.org/view.php?id=20907#c55064 ):
[...]
It could also fail in 2.6.x, just less often.
Hello,
2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not have 2.6.2 to test):
\fpc\svn\bin\i386-win32\fpc test.pp
Free Pascal Compiler version 2.7.1 [2013/03/03] for i386 Copyright (c) 1993-2013 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling test.pp
Linking test.exe
26 lines compiled, 0.1 sec, 29680 bytes code, 1236 bytes data
\fpc\fpc\2.6.0\bin\i386-win32\fpc.exe test.pp
Free Pascal Compiler version 2.6.0 [2011/12/25] for i386 Copyright (c) 1993-2011 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling test.pp
Linking test.exe
26 lines compiled, 0.0 sec , 27264 bytes code, 1692 bytes data
----------------------------------
program test;
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
TheB(Result);
end;
Var
C : String;
begin
C:='B';
C:=TheA;
C:=TheA;
Writeln('= "',C,'"');
end.
-----------------------------------And the effect is the same as using a non initialized variable, so it must generate a warning AFAIK. So passing Result as var does not generate a warning.
José Mejuto wrote2.6.0, 2.6.2 and 2.7.1 trunk produces a warning with that code, but with this one 2.6.0, and 2.7.1 trunk does not generate it (I do not have 2.6.2 to test):
That's because you are passing an uninitialized value to a var- parameter. That only generates a hint, because a lot of code uses var- parameters also for parameters that do not yet have to be initialized (because the code predates the existence of the "out" keyword, or simply out of habit).
This is unrelated to string results specifically.
Jonas
OK, I see now. I had changed the code to perform the same with integers and yes, it also does not report a warning only a hint which is enought to me.
I'm quite sure that the library that starts this thread does not show a hint, so maybe in some situations it could fail. I'll try to reproduce it in a small code (if there is a problem, of course), meanwhile everything seems to work as expected.
Thank you for your help.
That's because you are passing an uninitialized value to a var- parameter. That only…
Hello,
I had found why I was unable to see the hint, because it is not shown due the special contruction of the function.
This code produces a hint for the "Result: String" not initialized, but not for the "Result: integer" one:
------------------------------------------
program test;
{$mode objfpc}
{$h+}
procedure TheB(var aTheA: string);
begin
aTheA:=aTheA+'A';
end;
function TheA(): string;
begin
TheB(Result);
end;
procedure TheNumberB(var aTheB: integer);
begin
aTheB:=aTheB*2;
end;
function TheNumberA: integer;
procedure HideTheHint;
begin
//As it is a procedure the "Result" var is taken
//from the function scope.
TheNumberB(Result);
end;
begin
HideTheHint;
end;
Var
C : String;
N : integer;
begin
C:='B';
C:=TheA;
C:=TheA;
Writeln('= "',C,'"');
N:=1;
N:=TheNumberA;
N:=TheNumberA;
Writeln('= "',N,'"');
end.
-------------------------------------------Yes, it is a bit obfuscated. I do not known if this is a bug in the hint.