[Lazarus] "buffer" and string

Xiangrong Fang xrfang at gmail.com
Sun Mar 3 02:27:01 CET 2013


Hi,

I encountered unpredictable behavior with the following code:

var
  fs: TFileStream;
  s: string;
begin
  ... ...
  fs.Write(s, Length(s));
  ... ...
end;

But if I do this, then problem is gone:

var
  fs: TFileStream;
  s: string;
  buf: PChar;
begin
  ... ...
  buf := GetMem(Length(s) + 1);
  StrPCopy(buf, s);
  fs.Write(buf^, Length(s));
  ... ...
end;

My question is, why the above code with string works sometime, but not
alwasy fail? What is the internal structure of string and variable array
(i.e. array of something)?  What is the difference between @string and
@string[1] or @array and @array[0]?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130303/a749fca3/attachment-0002.html>


More information about the Lazarus mailing list