Forum > General

Assigning result of a function of type array

(1/3) > >>

simsee:
I have an embarrassing doubt, because I'm not a beginner.

Given that the assignment of a static/dynamic array to another determines only an assignment of the pointer to it, not of the elements, to assign which you need the copy function, when you assign the result of a function whose result type is a static/dynamic array, you have to use copy?

For example:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1;type  TArr=array of byte;   function Func:TArr;  begin    SetLength(Result,1);    Result[0]:=1;  end; var  Arr : TArr; begin  Arr:=Func; //or Arr:=Copy(Func); ???end.  

jamie:
The function returns the array, which is a pointer to a managed type, it does not copy it.

Unless there is something else you are thinking of?

Thaddy:
If you intend to use the returned array in its own context you need a copy indeed.
Say you have an array  a and an array b then follows:
a:= b makes a pointer copy and refcount increases. if an element in b changes it also changes in a.
a:= copy(b,0,high(b)) makes a deep copy and now when b changes, a will not change


MarkMLl:
I had a related problem with (from memory) a const array parameter which turned out not to be.

If in doubt, make a copy.

MarkMLl

simone:
For deep copy of a whole array you can also use copy without index and count parameters, although this is not explicitly documented here https://www.freepascal.org/docs-html/rtl/system/copy.html regarding the omission of index. So, you can write:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---A:=copy(B);
Anyway I think the op's question is about assigning a constant array (in that case result of a function).

Navigation

[0] Message Index

[#] Next page

Go to full version