[fpc-pascal]Incorrect RT 216 error?
Jonas Maebe
jonas at zeus.rug.ac.be
Fri Feb 15 22:09:38 CET 2002
On Thu, 14 Feb 2002 James_Wilson at i2.com wrote:
> string variable. But I get the RT 216 on line 82 (according to -gl) when I
> try something like this:
>
> string [string_index] := block [block_index];
Your problem is that
a) you are using ansistrings
b) you are trying to use them the same way as shortstrings
a and b don't mix very well as you have found out. Ansistrings are
dynamically allocated on the heap and dynamically grow and shrink (though
automatically inserted calls to helper routines by the compiler) when they
are modified.
You first set the string to '', which, when the string is an ansistring,
simply releases all memory allocated for it and then stores nil in it (an
ansistring is a pointer to a record containing the current length, max
length, number of references and the actual string stored as an array of
char terminated by a #0). Next, you start putting data in it, which
obviously causes a crash since the string is nil.
Now, how should you do it then? One way is:
> If I use concat instead it works, but it's *much* slower:
>
> string := concat (string, block [block_index]);
In this case, the concat helper makes sure enough memory gets allocated
for the anistring before anything is inserted. Of course, doing this for
every character makes it very slow as you have found out.
Another way is to use the setlength() procedure, with which you can create
an empty ansistring with enough room for a specified amount of characters.
Please read the manual for more information about ansistrings.
Jonas
More information about the fpc-pascal
mailing list