Yeah, thats the way it is really.
PChar is, really, just a pointer to some 'text' in memory. If that text is created and managed by a ANSIString, then you need to let the ANSIstring code manage it or bad things happen.
If you can manage the text pointed to by your pChar yourself, C style, allocate some memory keep within its bounds, you would not need to use the ANSIString functions but the down side is you cannot use the ANSIString functions !
FPC does have some tools to manage pure c type string ....
Davo
Yeah, my code example was a little terse...
Thanks. I'll check out AnsiString, but the VST framework is strictly PChars handed to me. There's no way to change it even if I could. Some of my string stuff is in very time-critical routines, so in C++, I've written my own byte-twiddling routines to work directly on strings to avoid unnecessary copies. I'll probably do the same here if the AnsiString class doesn't do what I need or doesn't do it fast enough.
AnsiString is not a class, it's a type. A reference counted type to be precise. So as long as you only have one reference to the string changing it's characters does not result in a new string copy, otherwise a copy-on-write mechanism is used. And as others already wrote, in most cases you'll be able to simply pass an
AnsiString to the C code by using
PChar(YourString). And you can also retrieve an
AnsiString from a
PChar by using
StrPas(YourPChar). The main advantage of using
AnsiString is that the count is part of the string (using
Length(YourString)) and one does not need to walk the whole string in search for a
#0 (what
StrLen does).
If you want to use
PChar nevertheless you need to handle the allocations yourself. Using
GetMem(Len + 1) to allocate enough of it, using the
PChar routines mentioned by
dbannon to work with it and
FreeMem(YourPChar) to free it again. Essentially just as you'd do in C.