Recent

Author Topic: problem on dynamic array create with a pointer in a function  (Read 2312 times)

chris77

  • Newbie
  • Posts: 4
problem on dynamic array create with a pointer in a function
« on: September 13, 2021, 07:19:20 pm »
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

  • Sr. Member
  • ****
  • Posts: 397
Re: problem on dynamic array create with a pointer in a function
« Reply #1 on: September 13, 2021, 11:37:05 pm »
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

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: problem on dynamic array create with a pointer in a function
« Reply #2 on: September 14, 2021, 09:00:40 am »
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

  • Newbie
  • Posts: 4
Re: problem on dynamic array create with a pointer in a function
« Reply #3 on: September 14, 2021, 10:24:16 am »
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 ....
« Last Edit: September 14, 2021, 11:04:04 am by chris77 »

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: problem on dynamic array create with a pointer in a function
« Reply #4 on: September 14, 2021, 12:16:24 pm »

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][+][-]
  1.  
  2.  
  3. type
  4. tst=record
  5. tmp:integer;
  6. myarr:array of string;
  7. end;
  8.  
  9. function init(i:integer;d1:string;d2:string):pointer;
  10. const
  11.  Z: tst = (tmp:0; myarr: nil);
  12. begin
  13. Z.tmp:=i;
  14. setlength(Z.myarr,2);
  15. Z.myarr[0]:=d1;
  16. Z.myarr[1]:=d2;
  17. exit(@Z);
  18. end;
  19.  
  20. var
  21. p:pointer;
  22. u:tst;
  23.  
  24. begin
  25. p:=init(27,'Hello','Press return to end . . .');
  26. u:=tst(p^);  // cast pointer data to tst
  27. writeln(u.tmp,' , ',u.myarr[0],' , ',u.myarr[1]);
  28.  
  29. readln;
  30. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: problem on dynamic array create with a pointer in a function
« Reply #5 on: September 14, 2021, 01:21:45 pm »
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 ....

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][+][-]
  1. type
  2.   TRecord = record
  3.     s: array of String;
  4.   end;
  5.   PRecord = ^TRecord;
  6.  
  7. var
  8.   p: PRecord;
  9. begin
  10.   New(p);
  11.   SetLength(p^.s, 2);
  12.   Dispose(p);
  13. end.

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.

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

  • Newbie
  • Posts: 4
Re: problem on dynamic array create with a pointer in a function
« Reply #6 on: September 14, 2021, 01:55:45 pm »
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

  • Sr. Member
  • ****
  • Posts: 394
Re: problem on dynamic array create with a pointer in a function
« Reply #7 on: September 14, 2021, 02:35:24 pm »

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][+][-]
  1.  
  2.  
  3. type
  4. tst=record
  5. tmp:integer;
  6. myarr:array of string;
  7. end;
  8.  
  9. AOS=array of string;
  10.  
  11. function init(i:integer;a:AOS):pointer;
  12. const
  13.  Z: tst = (tmp:0; myarr: nil);
  14.  var
  15.  n:int32;
  16. begin
  17. Z.tmp:=i;
  18. setlength(Z.myarr,length(a));
  19. for n:=low(a) to high(a) do Z.myarr[n]:=a[n] ;
  20. exit(@Z);
  21. end;
  22.  
  23. var
  24. p:pointer;
  25. u,v:tst;
  26. param:AOS;
  27. i:int32;
  28.  
  29. begin
  30. param:=AOS.create('one','two','three');
  31. p:=init(3,param);
  32. writeln('address ',int64(p));
  33. u:=tst(p^);  // cast pointer data to tst
  34.  
  35.  
  36. param:=AOS.create('one','two','three','four');
  37. p:=init(4,param);
  38. writeln('address ',int64(p));
  39. v:=tst(p^);  // cast pointer data to tst
  40.  
  41. // show stuff
  42.  write(u.tmp,' ');
  43. for i:=low(u.myarr) to high(u.myarr) do write(u.myarr[i],' ');
  44. writeln;
  45.  
  46.  write(v.tmp,' ');
  47. for i:=low(v.myarr) to high(v.myarr) do write(v.myarr[i],' ');
  48. writeln;
  49.  
  50. writeln('press return to end');
  51. readln;
  52. end.


chris77

  • Newbie
  • Posts: 4
Re: problem on dynamic array create with a pointer in a function
« Reply #8 on: September 14, 2021, 02:49:57 pm »
thank you very much ...Bobdog

 

TinyPortal © 2005-2018