[Solved] Dynarray of class cannot SetLength or Length lazarus / 62-General ---------------------------------------------------------------------- NaolShow · 2021-11-10 09:36 Hi everyone, it's me again, I need some help with the dynarrays. I already used them with simple data like Integers, but now I am trying to make an Array of a class called BaseItem. The problem is that it works perfectly when telling directly the size of the array: type ItemsArray = array[0..100] of BaseItem; then: Items : ItemsArray; The Length(Items) works But it does not work when I use a dynarray: type ItemsArray = array of BaseItem; then: Items : ItemsArray; If I try to call the Length(Items) it gives me the error: External: SIGSEGV at the line... writeln(Length(Items)) And if I try with the SetLength(Items, 5): External: SIGSEGV at the address: 100008042 Is there any solution to this ? Or do I need to use a list ? Thanks! ---------------------------------------------------------------------- NaolShow · 2021-11-10 09:48 I've tried in another project, and with juste the BaseI]"]>Blockednit and a dynarray type in the main class it works perfectly.. So the problem is not from the array. So here's my full code of the container unit: Is it because the array is defined in a class ? (That's the only difference with the test project) 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 unit ContainerUnit; {$mode objfpc}{$m+}{$R+} interface uses Classes, SysUtils, BaseI]"]>Blockednit; type ItemsArray = array[0..5] of BaseItem; type Container = class    public       Items : ItemsArray;       constructor Create();       function Add(item : BaseItem) : Integer; end; implementation constructor Container.Create();begin  //Items[0] := BaseItem.Create('tt');end; function Container.Add(item : BaseItem) : Integer;begin   // It throws the errors when calling these methods  writeln(Length(Items));  //SetLength(Items, 5); end; end. ---------------------------------------------------------------------- bytebites · 2021-11-10 11:12 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 ItemsArray = array[0..5] of BaseItem; This array has fixed length, you can't change it with setlength- function. ---------------------------------------------------------------------- howardpc · 2021-11-10 11:25 Or to use a dynamic array, something like this: 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 program project1; {$mode objfpc}{$H+}{$ModeSwitch advancedrecords} type   TBaseItem = record    name: String;    value: Integer;    procedure Init(const aName: String; aValue: Integer);  end;   TBaseItemArray = array of TBaseItem;   TContainer = class(TObject)  private    function GetCount: Integer;  public    Items: TBaseItemArray;    constructor Create;    function Add(aItem: TBaseItem): Integer;    property Count: Integer read GetCount;  end; { TBaseItem } procedure TBaseItem.Init(const aName: String; aValue: Integer);begin  name := aName;  value := aValue;end; constructor TContainer.Create;begin  Items := Nil;end; function TContainer.Add(aItem: TBaseItem): Integer;begin  SetLength(Items, Length(Items)+1);  Result := High(Items);  Items[Result] := aItem;end; function TContainer.GetCount: Integer;begin  Result := Length(Items);end; var  cont: TContainer;  bi: TBaseItem; begin  cont := TContainer.Create;   bi.Init('one', 1);  cont.Add(bi);  bi.Init('two', 2);  cont.Add(bi);  WriteLn('cont has ',cont.Count,' items');   cont.Free;end. ---------------------------------------------------------------------- Zvoni · 2021-11-10 11:38 You should read this: https://www.freepascal.org/docs- html/ref/refsu35.html ---------------------------------------------------------------------- PascalDragon · 2021-11-10 12:22 Quote from: NaolShow on November 10, 2021, 10:48:58 amSo here's my full code of the container unit: Is it because the array is defined in a class ? (That's the only difference with the test project) Did you do essentially the following in your main program? 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 var  c: Container;  bi: TBaseItem;begin  c := Container.Create;  bi := BaseItem.Create;  c.Add(bi);  // ...end. ---------------------------------------------------------------------- NaolShow · 2021-11-10 15:24 Hi everyone thanks for your reply. I am so sorry for annoying you with that. I am just incredibly STUPID. It was early and I was tired. I didn't see that I did not INITIALIZE the Container variable. This is because I am using a lot of units, and I have a Container variable into but did not initialize it in the initialize method... Thanks again and sorry! ---------------------------------------------------------------------- PascalDragon · 2021-11-11 08:20 Quote from: NaolShow on November 10, 2021, 04:24:23 pmI didn't see that I did not INITIALIZE the Container variable. This is because I am using a lot of units, and I have a Container variable into but did not initialize it in the initialize method... That's what I thought and that's why I asked.