[fpc-pascal] Library.StringFunction() : PChar => NO

leledumbo leledumbo_cool at yahoo.co.id
Fri Mar 14 16:20:46 CET 2014


> It will be the memory manager of the library who deals with the PChar
result.
So difficult for the main application to manage the memory used by PChar.

It's not a problem, as long as you provide an API to dispose the memory used
by the returned PChar. The responsibility of calling this API is delegated
to the application.

> MyStrTest(inStr:Pchar; outStr:pChar; size:integer):integer; cdecl;  
> var 
>   MyRealResult :string;
> begin
>  MyRealResult :=   inStr + ' added by function ';
>   Result := length(myRealResult);
> if size >= result then move(OutStr[0], MyRealResult[1], result);
> end;

Wrong usage of Move
(http://www.freepascal.org/docs-html/rtl/system/move.html). First parameter
is source, second is destination. So you have to swap them in above
function.

> function  testLib(InputText : string) : string ;
> var
>  MyStrOut : string;
>   MyLen : integer;
> begin
>    MyLen := MyStrTest( @InputText[1], at MyStrOut[1], 0);
>    SetLEngth(MyStrOut, mylen+1); 
>    FillChar(MyStrOut[1], MyLen+1,0);
>    MyStrTest(@InputText[1], @MyStrOut[1], 0);
>  result := MyStrOut ;
> end;

Wrong usage of the MyStrTest. Here you give Size parameter of value 0, while
in MyStrTest... see that last if statement? Guess yourself. Moreover, after
the first call, you set the length and then filling the space with 0. The
2nd MyStrTest call doesn't differ from the first.

>From the code, I can guess what that good guru means. First call he just
wants to get the length, therefore giving size 0. He then allocates enough
storage and re-call the function, but this time he seems to have a typo. The
2nd MyStrTest call should have size parameter set to MyLen.



--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Library-StringFunction-PChar-NO-tp5718594p5718595.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list