[lazarus] Lazarus ansistring AV?

Marc Weustink marc at dommelstein.net
Fri Jul 25 18:40:05 EDT 2003


At 00:41 26-7-2003 +0200, Marco van de Voort wrote:

>The other way around (pchartyped:=ansistringtyped) isn't  allowed as 
>conversion, only as cast.
>
>pchartyped:=pchar(ansistringtyped);
>
>This construct is also somewhat dangerous.
>
>The pchar becomes a reference to the content of the ansistring. The
>ansistring will still disappear when it goes out of scope.
>(and pchar will remain pointing to released memory)
>Any modification to the pchar will affect the ansistring.

Ah, thats another thing. Ansistrings share the same data (they are in fact 
pointers) if they are assigned, until they are changed. Casting to PChar 
may have strange side effects

Example

Good:

   a, b : String

   A := 'Some Text';   // A points to 'Some Text'
   B := A;             // B points to the same text
   B := 'Other Text';  // A new instance ot B is created
                       // and it points to 'Other Text'
                       // A has still 'Some Text'


Bad:

   a, b : String

   A := 'Text';       // A points to 'Text'
   B := A;            // B points to the same text
   PChar(B)^ := 'N';  // Now B has the text 'Next'
                      // but since the data was shared with A
                      // A has also the text 'Next'

Marc 






More information about the Lazarus mailing list