[Lazarus] "buffer" and string

leledumbo leledumbo_cool at yahoo.co.id
Sun Mar 3 05:19:29 CET 2013


> 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.




More information about the Lazarus mailing list