Recent

Author Topic: fpc_ansistr_to_chararray declaration within astrings.inc  (Read 2078 times)

lagprogramming

  • Full Member
  • ***
  • Posts: 159
fpc_ansistr_to_chararray declaration within astrings.inc
« on: December 09, 2015, 09:28:21 pm »
Code: Pascal  [Select]
  1. procedure  fpc_ansistr_to_chararray(out res: array of AnsiChar; const src: RawByteString); compilerproc;
  2. var
  3.   len: SizeInt;
  4. begin
  5.   len := length(src);
  6.   if len > length(res) then
  7.     len := length(res);
  8. {$push}{$r-}
  9.   { make sure we don't try to access element 1 of the ansistring if it's nil }
  10.   if len > 0 then
  11.     move(src[1],res[0],len);
  12.   fillchar(res[len],length(res)-len,0);
  13. {$pop}
  14. end;
  15.  

   Because "src" and "res" lengths are compared, I feel like "out" should not be there. Am I wrong?

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: fpc_ansistr_to_chararray declaration within astrings.inc
« Reply #1 on: December 09, 2015, 09:56:21 pm »
Because the length of the array that is being used, not its contents, I believe your feeling is not accurate.

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: fpc_ansistr_to_chararray declaration within astrings.inc
« Reply #2 on: December 10, 2015, 07:15:00 pm »
Quote
http://www.freepascal.org/docs-html/3.0.0/ref/refsu67.html
The purpose of an out parameter is to pass values back to the calling routine: the variable is passed by reference. The initial value of the parameter on function entry is discarded, and should not be used.
If a variable must be used to pass a value to a function and retrieve data from the function, then a variable parameter must be used. If only a value must be retrieved, a out parameter can be used.
Needless to say, default values are not supported for out parameters.
The difference of out parameters and parameters by reference is very small (however, see below for managed types): the former gives the compiler more information about what happens to the arguments when passed to the procedure: it knows that the variable does not have to be initialized prior to the call.
Remark: For managed types (reference counted types), using Out parameters incurs some overhead: the compiler must be sure that the value is correctly initialized (i.e. has a reference count of zero (0)). This initialization is normally done by the caller.

Quote
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Parameters_%28Delphi%29#Out_Parameters
 Out Parameters
An out parameter, like a variable parameter, is passed by reference. With an out parameter, however, the initial value of the referenced variable is discarded by the routine it is passed to. The out parameter is for output only; that is, it tells the function or procedure where to store output, but does not provide any input.
...
Out parameters are frequently used with distributed-object models like COM. In addition, you should use out parameters when you pass an uninitialized variable to a function or procedure.


   For now the code works fine. Does it work by accident? I look at "out" as a compiler optimization hint(compared to "var").