dynamic array setLength - aeh - why? lazarus / 21-General ---------------------------------------------------------------------- Nicole · 2022-11-10 17:50 The last hour I spent at a strange error. Perhaps there is wrong something completely different, but I cannot see it. There is a method which has a line: 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 ..myMethod(var myArray);begin..  SetLength(myarray, value);..end; Often it works, sometimes not. I do not know, what is the reason. May it be, that it does not work, if the value is > 1000 ? The call is from a method like this: Do you see anything wrong 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 for i:=0 to BaseArray.Count - 1  do myMethod (BaseArray[i].myArray) ---------------------------------------------------------------------- Bart · 2022-11-10 18:09 What is "does not work"? Do you check wether value is what you expected? Do you check Length(myarray) after the call to SetLength? (Not in the debugger, but old fashioned writeln's) What optimization level do you use, does the issue happen with -O- (no optimizations)? (There are some bugs with -O2 on Win64 IIRC) Can you make an example project demonstrating the issue (keep on stripping out code that does not seem to influence the buggy behaviour), more likely than not this will point you to the error (maybe in an entirely different place). Bart ---------------------------------------------------------------------- Nicole · 2022-11-10 18:15 As said, the problem seems to occur, if the value is larger than 1000. ---------------------------------------------------------------------- Bogen85 · 2022-11-10 18:17 Quote from: Nicole on November 10, 2022, 07:15:17 pmAs said, the problem seems to occur, if the value is larger than 1000. OK Quote from: Bart on November 10, 2022, 07:09:40 pmCan you make an example project demonstrating the issue (keep on stripping out code that does not seem to influence the buggy behaviour), more likely than not this will point you to the error (maybe in an entirely different place). @Nicole, can you provide this? Otherwise it is just guess work for us. ---------------------------------------------------------------------- Bogen85 · 2022-11-10 18:18 Quote from: Bogen85 on November 10, 2022, 07:17:45 pm@Nicole, can you provide this? Otherwise it is just guess work for us. Or at least try it for yourself. ---------------------------------------------------------------------- Josh · 2022-11-10 18:19 hi what is the size of base array, what is the array of?, bytes, words,strings, record sctructures, objects etc, could be mem issues, esp if compiling 32bit.. more info needed especially on the data structure ---------------------------------------------------------------------- jamie · 2022-11-10 23:06 I could be wrong but, I believe you need to give the procedure the complete base of the array, since any inner arrays are part of the base and thus need to be managed.  So, try passing the base array with an index.  If you want such control, you can build old school dynamic arrays that will work that way. ---------------------------------------------------------------------- Nicole · 2022-11-12 08:44 Thank you for the replies. I am aware, that is it guessing. This is exactly my problem. If I could bring the whole thing to a simple project, I could solve the problem probably by myself. @Jamie: Many thanks for the idea about the array, I will try that. In the meanwhile, I am sure, that "1000" is the limit for my problems. Indeed, 999 is a limit in one of my variables, but at a place, where it should not matter. I wonder, if I should just go the ugly way and move my pointer a little bit further in cases, where more than 1000 datasets are ahead... Probably nobody will be aware of, that of so many data some are not there any more.  Only the first 200 will be watched closely. ---------------------------------------------------------------------- KodeZwerg · 2022-11-12 09:12 Quote from: Nicole on November 10, 2022, 06:50:20 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure myMethod(out myArray: TmyArray);var  Len: Integer;begin..  Len := Length(myArray);SetLength(myarray, Len + value);..end;My 50cents. ---------------------------------------------------------------------- dseligo · 2022-11-12 12:15 Quote from: Nicole on November 10, 2022, 06:50:20 pmThe last hour I spent at a strange error. Perhaps there is wrong something completely different, but I cannot see it. There is a method which has a line: What is the error - is it a message, exception, something else? Perhaps you wrote what the error is somewhere, but I cannot see it. ---------------------------------------------------------------------- Thaddy · 2022-11-12 12:31 Note the max size is high (SizeInt), see declatarion for setlength . 2147483647 on 32 bit, 9223372036854775807 on 64 bit. Depending on memory and stack that is a heck of a lot more than 1000. I ran a test and can confirm that e.g. for a dynamic array of integer all elements are correct. Here's my test: for 16 bit :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 {$mode objfpc}{$IOCHECKS OFF for speed}{$ASSERTIONS ON for verification}uses sysutils; // just for typed exceptionstype   TMyArray = array of integer;var  a:TMyArray;  i:integer;begin  setlength(a,32767);  for i := 0 to high(a) do    a[i] := i-10;  a[10000] := 0;  // force assertion failed. You can comment this out  for i := 0 to High(a) do   try    assert(a[i] = i-10,'Wrong Value');  writeln(i,a[i]:8);  except    on E:EAssertionFailed      do begin      writeln(E.Message,' at index:',i:6,' have: ',a[i],' expected: ',(i-10));        break;      end;    on E:Exception do begin      writeln(E.message);      break;    end;  end;end. ---------------------------------------------------------------------- jamie · 2022-11-12 14:42 what does the declaration of this array look like? is a Array of array of What?  If so this will be problematic like i said because the inner array is managed by the base array. if you did something like this maybe. 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 TMySubArray = Array of WhatEver;Var MyBaseArray :Array of TmySubArray;   Given the example above, that should separate the operation so that inner arrays are self-managed.  You must remember to always ensure the base array has the number of indexes that is required to house the sub array.  also, you need to clean up the sub arrays when freeing stuff. ---------------------------------------------------------------------- ASerge · 2022-11-12 18:58 Quote from: KodeZwerg on November 12, 2022, 10:12:24 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure myMethod(out myArray: TmyArray);var  Len: Integer;begin  Len := Length(myArray);   SetLength(myarray, Len + value);end;If TmyArray is a dynamic array, then Len is always zero, because the out parameter. ---------------------------------------------------------------------- Thaddy · 2022-11-12 19:56 Yes, you can't use out there, must be var ---------------------------------------------------------------------- Nicole · 2022-11-14 10:17 Thank you for your replies. The error is an access violation. In the meanwhile, I found that the line SetLength is not the line with the problem. The trouble is below in the loop. In this loop there may be an out of bounds effect. I am working on it, as the thing is nested and very complex. So the most probable reason is under the line (the example is to explain, not to reproduce): SetLength(myArray, 1000); myArray[1005]...... *peng* To summarize: The error-message comes in the SetLenth line, although the error happens below. ---------------------------------------------------------------------- Thaddy · 2022-11-14 14:12 the error message should be a runtime rangecheck error (201) with {$R+} and probably boom without...