[fpc-pascal] Library.StringFunction() : PChar => NO
Fred van Stappen
fiens at hotmail.com
Fri Mar 14 22:01:19 CET 2014
> Date: Fri, 14 Mar 2014 08:20:46 -0700
> From: leledumbo_cool at yahoo.co.id
> To: fpc-pascal at lists.freepascal.org
> Subject: Re: [fpc-pascal] Library.StringFunction() : PChar => NO
>
> > 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.
@ leledumbo, like always => brilliant => Tetras * Megas thanks. ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140314/1c81999b/attachment.html>
More information about the fpc-pascal
mailing list