[Lazarus] "buffer" and string

Xiangrong Fang xrfang at gmail.com
Sun Mar 3 07:17:11 CET 2013


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 <leledumbo_cool at yahoo.co.id>

> > 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 archive at Nabble.com.
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20130303/147dd6e5/attachment-0003.html>


More information about the Lazarus mailing list