[Lazarus] Help with basic pointer operations

2011-08-22 Thread Tommi Prami

Hello,

I kind of always try to stay away from the pointers, but now I maybe 
need to use them...


I think this is not correct yet (at least it does not to 100% what I like) :

var
  LSimpleStr : string;
  LTmpWStr  : array [0..250] of WideChar;
...
  LSimpleStr := 'Some text';
  mwStrToWide(LSimpleStr, LTmpWStr ,  250);
...


procedure mwStrToWide(const AString: string; PWideString: PWideChar;
  const ABufferLen: Integer);
begin
  FillChar(PWideString^, ABufferLen, 0);

  if AString <>  '' then
  begin
StringToWideChar(AString, PWideString, ABufferLen);
  end;
end;


With debugger it seems that the Buffer is not cleared correctly like I 
would like to (Much easier to debug when you see more easily the content 
of the Array).


And the most worrying part is that the code seems to work other vice, 
but I have the feeling that it is not 100% correct.


-TP-

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Re: [Lazarus] Help with basic pointer operations

2011-08-22 Thread michael . vancanneyt



On Mon, 22 Aug 2011, Tommi Prami wrote:


Hello,

I kind of always try to stay away from the pointers, but now I maybe need to 
use them...


I think this is not correct yet (at least it does not to 100% what I like) :

var
 LSimpleStr : string;
 LTmpWStr  : array [0..250] of WideChar;
...
 LSimpleStr := 'Some text';
 mwStrToWide(LSimpleStr, LTmpWStr ,  250);
...


procedure mwStrToWide(const AString: string; PWideString: PWideChar;
 const ABufferLen: Integer);
begin
 FillChar(PWideString^, ABufferLen, 0);


At least this is wrong, since the buffer is 2*250 wide. 
FillChar assumes that 1 char = 1 byte.


Michael.

--
___
Lazarus mailing list
Lazarus@lists.lazarus.freepascal.org
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus