Warning when returning a dynamic array lazarus / 61-Beginners ---------------------------------------------------------------------- Lev · 2021-11-01 13:28 Hi I'm running a function from this thread: https://forum.lazarus.freepascal.org/index.php?topic=40744.0: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  IntegerArray = array of Integer; function SetArray(const len: Cardinal): IntegerArray;var i:Integer;begin  SetLength(Result, len);  if (len < 3) then  begin    SetLength(Result, 0);  //returns an empty array    exit;  end;  Result[0]:= 0;  Result[1]:= 1;  for i := 2 to len-1 do    Result[i]:= Result[i-1] + Result[i-2];end; with fpc 3.2.2 for i386 and getting the following error: Warning: function result variable of a managed type does not seem to be initialized What could possibly be the reason and how could it be circumvented? ---------------------------------------------------------------------- Bart · 2021-11-01 14:15 There are circumsances when Result will NOT be what you expected. I always forget how exactly this works, but it has been documented in the forum several times. See PacalDragons answer in this thread. Maybe we should have it in a FAQ somewhere? Bart ---------------------------------------------------------------------- WooBean · 2021-11-01 14:43 Hi lev, your code seems to me an absolutely legitime way for producing Fibonacci sequence elements. The warning from FPC compiler is not very deep assumption that dynamic array can use some uninitialized elements but in fact it is not true. Leave your code as is or report the issue to FPC team. WooBean ---------------------------------------------------------------------- Lev · 2021-11-01 14:54 Quote from: Bart on November 01, 2021, 03:15:18 pmThere are circumsances when Result will NOT be what you expected. I always forget how exactly this works, but it has been documented in the forum several times. See PacalDragons answer in this thread. Maybe we should have it in a FAQ somewhere? Bart Ok, that explains it. But what about the workaround part of the question? Of the three variants recommended there: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SetLength(Arr,0);Arr:= Nil;Finalize(Arr); the first one still ignites a warning (and it sounds entirely unreasonable to set length to 0 and then set it to nonzero in the very next line), the second one is ok, but looks pretty cryptic and the third one looks strange as finalizing is not something you do in the very beginning of a function. Is there a way to call Default with nonzero element count (or to call 'create' with a given number of zero elements)? This would match the behaviour in other languages and would look reasonable in the beginning of a function. https://wiki.freepascal.org/Dynamic_array As for including it in a FAQ, I would expect a pair of words about it here: https://wiki.freepascal.org/Dynamic_array ---------------------------------------------------------------------- Lev · 2021-11-01 15:02 Quote from: WooBean on November 01, 2021, 03:43:58 pmHi lev, your code seems to me an absolutely legitime way for producing Fibonacci sequence elements. The warning from FPC compiler is not very deep assumption that dynamic array can use some uninitialized elements but in fact it is not true. Leave your code as is or report the issue to FPC team. WooBean I'm afraid the compiler is right: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  IntegerArray = array of Integer; function SetArray(const len: Cardinal): IntegerArray;begin  SetLength(Result, len);  writeln(Result[0]);end; var  a: IntegerArray; begin;  a := IntegerArray.create(3,2,1);  a := setarray(5);end. indeed prints '3'. ---------------------------------------------------------------------- WooBean · 2021-11-01 15:17 @lev Your second example of using dynamic array is different from the first (in a way of creating true cause for emitting a warning by FPC). At the second you use uninitialized value at line 7:    writeln(Result[0]); //after setlength(Result, len); At the first example you initialize two first elements of sequence and then use it for populating remaing elements in Result (dynamic array). WooBean ---------------------------------------------------------------------- Lev · 2021-11-01 17:37 Quote from: WooBean on November 01, 2021, 03:43:58 pmThe warning from FPC compiler is not very deep assumption that dynamic array can use some uninitialized elements but in fact it is not true I have provided an example of code which demonstrates that the assumption is correct: it can use the uninitialized elements. The compiler is right. The error message belongs to the line with 'setlength' and happens even if it is the only line out there: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  IntegerArray = array of Integer; function SetArray(const len: Cardinal): IntegerArray;begin  SetLength(Result, len);end; var  a: IntegerArray; begin;  a := IntegerArray.create(3,2,1);  a := setarray(5);end. It would be too much to expect from the compiler to track how I'm using the uninitialized array later on. The problem is not with the warning but with the general approach of initializing vectors through `setlength`. I don't see what the problem is with pascal having a normal constructor for dynamic arrays which initializes with zeros instead. Anyway I'm not in the mood of a continuous discussion on the subject. I'm relatively happy with the Arr:= Nil; solution ---------------------------------------------------------------------- PascalDragon · 2021-11-02 12:35 Quote from: WooBean on November 01, 2021, 03:43:58 pmThe warning from FPC compiler is not very deep assumption that dynamic array can use some uninitialized elements but in fact it is not true. Leave your code as is or report the issue to FPC team. No, the warning is legitimate especially as the compiler can't detect whether the whole array data will be rewritten in the remaining code. Quote from: Lev on November 01, 2021, 06:37:52 pmThe problem is not with the warning but with the general approach of initializing vectors through `setlength`. I don't see what the problem is with pascal having a normal constructor for dynamic arrays which initializes with zeros instead. SetLength only changes the length and can be used with existing arrays to enlarge them so it must not touch existing elements (except to remove them if the new length is smaller than the current one). To really reset an array SomeArray := Nil is indeed the correct way. Alternatively, as you're touching all elements in your SetArray function you can disable the warning locally (I would not suggest to disable it globally, cause the warning does have it's use): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  IntegerArray = array of Integer; function SetArray(const len: Cardinal): IntegerArray;var i:Integer;begin  {$push}  {$warn 5093 off} // you can find the number by compiling with -vq  SetLength(Result, len);  {$pop}  if (len < 3) then  begin    SetLength(Result, 0);  //returns an empty array    exit;  end;  Result[0]:= 0;  Result[1]:= 1;  for i := 2 to len-1 do    Result[i]:= Result[i-1] + Result[i-2];end;