In Delphi typically used const, which notifies the compiler that it can optimize the code and pass only the reference, otherwise it pass by value. In FPC to accurately ensure this used constref.if the array is very large, is this getting pushed on the stack each time it gets called?
function SearchTable(CodePageArr: array of word; id: cardinal): word;
In Delphi typically used const, which notifies the compiler that it can optimize the code and pass only the reference, otherwise it pass by value.Just for precision's sake. The array will always be passed by reference. The presence of "const" tells the compiler that it doesn't need to make a copy of the array (since the values won't/cannot be changed/assigned to.) If "const" is not specified then a copy of the array is made and all array operations are done on the copy.
Dynamic arrays are reference counted. Calling setLength(myDynamicArrayVariable, 0) virtually does myDynamicArrayVariable := nil and decreases the reference count. Only when the reference count hits zero, the memory block is released.Rewrite InitArray as a function that return an array.
In Delphi typically used const, which notifies the compiler that it can optimize the code and pass only the reference, otherwise it pass by value.Just for precision's sake. The array will always be passed by reference. The presence of "const" tells the compiler that it doesn't need to make a copy of the array (since the values won't/cannot be changed/assigned to.) If "const" is not specified then a copy of the array is made and all array operations are done on the copy.
Rewrite InitArray as a function that return an array.That's very inefficient. The compiler will allocate stack space to hold the result of the function then copy the result into the receiving variable.
Only in the case of a const open arrayNo. Even if the array is passed by value, the compiler will pass the array by reference and the reference is used to make a copy of the array. What "const" does is simply tell the compiler that making a copy of the array is not necessary since the array elements are not modified.
Rewrite InitArray as a function that return an array.That's very inefficient. The compiler will allocate stack space to hold the result of the function then copy the result into the receiving variable.
ETA:
@ThaddyOnly in the case of a const open arrayNo. Even if the array is passed by value, the compiler will pass the array by reference and the reference is used to make a copy of the array. What "const" does is simply tell the compiler that making a copy of the array is not necessary since the array elements are not modified.
IOW, the array is always passed by reference. You can see that in the generated assembly.
I suspect again an intentional misrepresentation of my answer.No. There was no intentional misrepresentation of your answer.
...whereas a typed dynamic array declared as const can be modified!.Can you give an example of a "typed dynamic array declared as const" whose elements can be modified ?. I doubt the compiler is going to allow modifying the elements of an array that has been declared as const parameter to some function/procedure.
Just for precision's sake. The array will always be passed by reference. The presence of "const" tells the compiler that it doesn't need to make a copy of the array (since the values won't/cannot be changed/assigned to.) If "const" is not specified then a copy of the array is made and all array operations are done on the copy.Yes. Rather, it is implementation details, and in the documentation of FPC I didn't see it. Although Delphi has Passing Parameters (http://docwiki.embarcadero.com/RADStudio/Rio/en/Program_Control_(Delphi)#Passing_Parameters)
Can you give an example of a "typed dynamic array declared as const" whose elements can be modified ?. I doubt the compiler is going to allow modifying the elements of an array that has been declared as const parameter to some function/procedure.I actually just did! You are really useless today, aren't you? :) O:-) https://forum.lazarus.freepascal.org/index.php/topic,46444.msg330997.html#msg330997
I actually just did! You are really useless today, aren't you? :) O:-)That thing you call an "example" doesn't even compile therefore it is like you, eternally useless.
It requires FPC 3.2 or newer, because of the initialization of a and b.I actually just did! You are really useless today, aren't you? :) O:-)That thing you call an "example" doesn't even compile therefore it is like you, eternally useless.
It requires FPC 3.2 or newer, because of the initialization of a and b.Thank you for that information.