> Date: Fri, 14 Mar 2014 08:20:46 -0700
> From: leledumbo_c...@yahoo.co.id
> To: fpc-pascal@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],@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. ;-)
                                          
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to