On Sun, Sep 05, 1999 at 07:35:25AM +0200, Frank Heckenbach wrote:
Just to clarify some points about CStrings which are really confusing...
GPC does in fact support assignments of Pascal strings to CStrings and handles them correctly (i.e., sets the CString pointer to the beginning of the text in the string, not to the capacity field, and adds a #0 terminator).
Well, good that means my memory isn't going.:) The reason why I described the problem with the message subject that I did was because things *did* work correctly if placed in the main program block. Now with the descriptions from Peter and you I realize that I was just lucky before when I had CString working in various function or modules. Perhaps OS/2 just didn't happen to reuse the memory referenced by the pointers as quickly.
Correct solutions are e.g. GetMem/FreeMem (or New/Dispose)
Anyone want to give me a snippet of code?
I assume something like:
Var foo : CString; [...] begin New(foo, 3); { or } GetMem(foo, 3);
To get 3 bytes of string space right? Do I have to worry about enough space for the null, or is that transparent?
When calling a routine (of the RTS or of some C library) that expects a CString parameter, don't worry, just pass it a Pascal string :-) -- unless the function will modify the CString, then things get more difficult...
Okay this is primarilly what I was doing. I've been working on some modules that supply Pascal equivalents of the C socket calls and I was writing my own functions for some routines that were just as easy to rewrite in Pascal (like inet_ntoa() ) and for some reason was being anal and trying to return CStrings like their C equivalents. It makes as much sense to return Pascal strings, to provide a more natural interface on the Pascal side.
Thanks to everyone for their clarifications this week, -Kevin