On 29 Dec 2005 at 14:39, Chris Hicks wrote:
On 29th December the Chief wrote:
But of course, if you called "freemem" above (and even if you didn't), then it is indeed not a good idea to make the assignment just made. At best, you'd get a memory leak. At worst, you'd get a GPF.
What would you recommend that I do? I attached my modified pcallc.p and Makefile.
[...]
getmem (s, 260); { ahem - we need to allocate memory } w1(i,s); writeln("Returned from C again I = ",i); writeln("Return from C cstring = ",s); freemem (s, 260); { free this here, else memory leak on next line }
At this point, everything is fine.
s := "This is a CString"; { not really a good idea, IMHO }
This may or may not be ok.
Personally, I would either do: s := strnew ('This is a CString'); { strnew is from the Strings unit } (which allocates sufficient memory for and copies the string to "s") followed by a "strdispose (s);" when I am finished with "s",
or
not call "freemem (s)" where it was called, but keep the 260 byte memory allocation (if that is sufficient - which it is in this case), and then call: strcopy (s, 'This is a CString'); and after finishing with "s", call the "freemem (s)" at that point.
Really, this is probably not very different from how you would treat a "char *s" in C.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/