problem on dynamic array create with a pointer in a function lazarus / 62-General ---------------------------------------------------------------------- chris77 · 2021-09-13 17:19 Hello , (sorry for my bad english) I have try to create a type that y contains a dynamic array and a pointer to this type, then i have a function that create the data, i use getmem of my pointer a fill the dynamic array with initializearray(@ttx,typeinfo(tttx),length(tx)); ttx:=tx;ttx:=copy(tx,0,length(tx); then i control my data after i quit the function and the data disappear. the table ttx that i have save in another dynamic table . ttx =nil ..... Are there any compiler function type {$} to not destruct my data ...? ---------------------------------------------------------------------- HeavyUser · 2021-09-13 21:37 sorry you need to provide a small piece of code that reproduces the problem, a function in a console application something small and readable in order to get help on this. What you describe sounds like an override of the dynamic array internal memory. I have no idea what initiallizeArray is or does so I can not offer any farther opinions. ---------------------------------------------------------------------- PascalDragon · 2021-09-14 07:00 As HeavyUser wrote, please show us what you're trying to do. In most cases a pointer to a dynamic array is the wrong solution however, because a dynamic array already is a pointer (in contrast to a static array). ---------------------------------------------------------------------- chris77 · 2021-09-14 08:24 i use a pointer with getmem(sizeof(trecord)) where trecord contains array of string ...i make that because the pointer can stock multiple type ...that i need... i know that are a wrong idea but i m sure that can be possible the error was probably on getmem .... ---------------------------------------------------------------------- BobDog · 2021-09-14 10:16 Without using getmem, please remember to use a static member to return a pointer to it from a function. Otherwise the information is lost outside the function. static = const in this respect. Simple example 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   typetst=recordtmp:integer;myarr:array of string;end; function init(i:integer;d1:string;d2:string):pointer;const Z: tst = (tmp:0; myarr: nil);beginZ.tmp:=i;setlength(Z.myarr,2);Z.myarr[0]:=d1;Z.myarr[ 1]:=d2;exit(@Z);end; varp:pointer;u:tst; beginp:=init(27,'Hello','Pres s return to end . . .');u:=tst(p^);  // cast pointer data to tstwriteln(u.tmp,' , ',u.myarr[0],' , ',u.myarr[1]); readln;end. ---------------------------------------------------------------------- PascalDragon · 2021-09-14 11:21 Quote from: chris77 on September 14, 2021, 10:24:16 ami use a pointer with getmem(sizeof(trecord)) where trecord contains array of string ...i make that because the pointer can stock multiple type ...that i need... i know that are a wrong idea but i m sure that can be possible the error was probably on getmem .... 1. You should use New instead of GetMem if your record contains a managed type (the equivalent function to FreeMem is then Dispose) 2. Using SetLength is enough then 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  TRecord = record    s: array of String;  end;  PRecord = ^TRecord; var  p: PRecord;begin  New(p);  SetLength(p^.s, 2);  Dispose(p);end. Quote from: BobDog on September 14, 2021, 12:16:24 pmWithout using getmem, please remember to use a static member to return a pointer to it from a function. Otherwise the information is lost outside the function. static = const in this respect. This will however mean that you'll always use the same memory when init is called multiple times. Depending on the usecase this might or might not be what is desired. ---------------------------------------------------------------------- chris77 · 2021-09-14 11:55 same result with de "new" operation ... and the same with first proposition ... but i think that the solution with const variable after that i save the pointer in a array of pointer ... there i have other variable but tx was alwasy nil... arf .... i will to make another structure ... ---------------------------------------------------------------------- BobDog · 2021-09-14 12:35 The address is static (const), but the data held is passed separately, seems to be anyway. But I daresay a little careful planning is needed not to pollute anything. 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   typetst=recordtmp:integer;myarr:array of string;end; AOS=array of string; function init(i:integer;a:AOS):pointer;const Z: tst = (tmp:0; myarr: nil); var n:int32;beginZ.tmp:=i;setlength(Z.myarr,length(a));for n:=low(a) to high(a) do Z.myarr[n]:=a[n] ;exit(@Z);end; varp:pointer;u ,v:tst;param:AOS;i:int32; beginparam:=AOS.create('one','two','three'); p:=init(3,param);writeln('address ',int64(p));u:=tst(p^);  // cast pointer data to tst  param:=AOS.create('one','two','three','four');p:= init(4,param);writeln('address ',int64(p));v:=tst(p^);  // cast pointer data to tst // show stuff write(u.tmp,' ');for i:=low(u.myarr) to high(u.myarr) do write(u.myarr[i],' ');writeln;  write(v.tmp,' ');for i:=low(v.myarr) to high(v.myarr) do write(v.myarr[i],' ');writeln; writeln('press return to end');readln;end. ---------------------------------------------------------------------- chris77 · 2021-09-14 12:49 thank you very much ...Bobdog