Recent

Author Topic: Copy for dynamic array  (Read 9640 times)

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Copy for dynamic array
« on: June 26, 2012, 10:35:52 pm »
If I use this:
Code: [Select]
var
a,b: array of extended;
begin
 SetLength(a,2);
 a[0]:=5;
 a[1]:=6;
 b:=Copy(a,0,Length(a));
 SetLength(a,0);
end;
Can I continue to use b-array safely ?
Variable b has a reserved memory area ?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12596
  • FPC developer.
Re: Copy for dynamic array
« Reply #1 on: June 26, 2012, 11:07:43 pm »
Afaik yes. string types are copy on write, dynamic arrays are always deep-copy.

Since you only use proper, typed operations, a writeln'ing length(b) at the end will even make this visible

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: Copy for dynamic array
« Reply #2 on: June 26, 2012, 11:37:48 pm »
I wonder because if I use:
Code: [Select]
var
  a: array of extended;
  c: Pextended;
begin
  SetLength(a,2);
  a[0]:=5;
  a[1]:=6;
  c:=@a[1];
  SetLength(a,0);
  debugln(floattostr(c^));
in this case is also safe ?
assign the address reserves memory space?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Copy for dynamic array
« Reply #3 on: June 26, 2012, 11:49:41 pm »
I wonder because if I use:
Code: [Select]
var
  a: array of extended;
  c: Pextended;
begin
  SetLength(a,2);
  a[0]:=5;
  a[1]:=6;
  c:=@a[1];
  SetLength(a,0);
  debugln(floattostr(c^));
in this case is also safe ?
assign the address reserves memory space?

sure it is as safe as dividing by zero. What you experience is the fact that setting the length to 0 does not free the memory immediately and can be used again if its required although it seems safe enough and harmless immediately after the setlength call in heavelly used environment that will return unpredictable results and if the memory used is big enough to release back to the system you will end with dificult to find access violations.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: Copy for dynamic array
« Reply #4 on: June 27, 2012, 08:54:39 am »
OK, I read a little about reference counting http://www.freepascal.org/docs-html/ref/refsu15.html and I understand that for my first example array is not yet disposed until ref. count will be 0 - SetLength(a,0) and SetLength(b,0).
The second example will not work properly.  :)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Copy for dynamic array
« Reply #5 on: June 27, 2012, 12:06:15 pm »
Instead of doing all the copying, if you are purposefully getting rid of array a, can't you do b:=a; ? It would be pointer assignment, so doing setlength(a, 0); afterwards, would also wipe clean b. But something like a:=nil; ... this just goes to guessing, but i'm assuming its sort of how it works.

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: Copy for dynamic array
« Reply #6 on: June 27, 2012, 04:45:38 pm »
So sorry, I made a mistake in my first example
instead
Code: [Select]
b:=Copy(a,0,Length(a));should be
Code: [Select]
b:=a; :(

 

TinyPortal © 2005-2018