Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-03

"buffer" and string

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Xiangrong Fang

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.freepascal.org/pipermail/lazarus/attachments/20130303/a749fca3/attachment.html>

Reply by email

leledumbo
to Xiangrong Fang
My question is, why the above code with string works sometime, but not

alwasy fail?

TStream.Write expects a buffer, typically the first element of an item (NOT a pointer to it) and the length (number of elements after first one).

fs.Write(s, Length(s));

Here you try to give the WHOLE string, which is variable depending on the string type. It could be plain array for ShortString, but also pointer to a dynamically allocated structure for other strings. For ShortStrings, however, the above code will have the string length (if I'm not mistaken) before the first character in the written file.

To do it correctly without converting to PChar is to give the first element:

fs.Write(s[1], Length(s));
What is the internal structure of string and variable array (i.e. array of something)?

Explained in docs:
- static array:
http://www.freepascal.org/docs-html/prog/progsu161.html#x204-2170008.2.9
- dynamic array:
http://www.freepascal.org/docs-html/prog/progsu162.html#x205-2180008.2.10
- strings:
http://www.freepascal.org/docs-html/prog/progsu159.html#x202-2120008.2.7

What is the difference between @string and @string[1] or @array and @array[0]?

@string means pointer to to the string STRUCTURE, for ShortString, this is the same as @string[0] (the length)
@string[1] means pointer to the first character in the string @array means pointer to the array STRUCTURE, for static array, this is the same as @array[<lowest index>]
@array[0] means pointer to the element at index 0

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-buffer-and-string-tp4029601p4029603.html Sent from the Free Pascal - Lazarus mailing list .

Reply by email

Xiangrong Fang
to leledumbo

If a buffer expect the first "element", and count is the number of elements to write, then what is the unit size of that element? is it always "byte"?

2013/3/3 leledumbo

Show quoted text 56 lines
> My question is, why the above code with string works sometime, but not alwasy fail? TStream.Write expects a buffer, typically the first element of an item (NOT a pointer to it) and the length (number of elements after first one). > fs.Write(s, Length(s)); Here you try to give the WHOLE string, which is variable depending on the string type. It could be plain array for ShortString, but also pointer to a dynamically allocated structure for other strings. For ShortStrings, however, the above code will have the string length (if I'm not mistaken) before the first character in the written file. To do it correctly without converting to PChar is to give the first element: fs.Write(s[1], Length(s)); > What is the internal structure of string and variable array (i.e. array of > something)? Explained in docs: - static array: http://www.freepascal.org/docs-html/prog/progsu161.html#x204-2170008.2.9 - dynamic array: http://www.freepascal.org/docs-html/prog/progsu162.html#x205-2180008.2.10 - strings: http://www.freepascal.org/docs-html/prog/progsu159.html#x202-2120008.2.7 > What is the difference between @string and @string[1] or @array and > @array[0]? @string means pointer to to the string STRUCTURE, for ShortString, this is the same as @string[0] (the length) @string[1] means pointer to the first character in the string @array means pointer to the array STRUCTURE, for static array, this is the same as @array[<lowest index>] @array[0] means pointer to the element at index 0 -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-buffer-and-string-tp4029601p4029603.html Sent from the Free Pascal - Lazarus mailing list . -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130303/147dd6e5/attachment.html>

Reply by email

Flávio Etrusco
to Xiangrong Fang
Xiangrong Fang wrote
If a buffer expect the first "element", and count is the number of elements to write, then what is the unit size of that element? is it always "byte"?

Yes - count is the number of bytes, not the number of elements.

Reply by email

leledumbo
to Xiangrong Fang: If a buffer expect the first "element", and count is the number of elements to write,…
If a buffer expect the first "element", and count is the number of elements

to write, then what is the unit size of that element? is it always "byte"?

From
http://www.freepascal.org/docs-html/rtl/classes/thandlestream.write.html: "Write overrides the Write method of TStream. It uses the Handle property to write the Count BYTES from Buffer."

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-buffer-and-string-tp4029601p4029644.html Sent from the Free Pascal - Lazarus mailing list .

Reply by email