I am beginning to make some progress (thanks for your help so far), but would be very grateful for some more help.
My attempts at producing the XView - Pascal code are in Makefile, p.p, x_message.c and x_message.h.
When I try to compile I get: $ make gpc -c p.p p.p: In main program: p.p:11: error: argument to `WriteLn' is of wrong type p.p:11: error: (Use `--cstrings-as-strings' to treat `CString' as a string.) make: *** [p.o] Error 1
I based this code on various examples, including some that demonstrated passing strings between C and Pascal that I produced with the Chief's help back in 2003 (http://www.staff.ncl.ac.uk/chris.hicks/programming.htm, pcallsc2 <http://www.staff.ncl.ac.uk/chris.hicks/programming.htm, pcallsc2> near the bottom).
I don't understand what is happening here. My Writeln is similar to the 2003 example that worked, likewise I am not sure why the --cstrings-as-strings is not working. I am also wondering whether the Pascal in the 2003 example was quite right. Does the second getmem release the memory? program pascalcallsC(input,output);
procedure world; external name 'c_world'; procedure w1(var i : integer; s : pchar); external name 'c_w1';
var i : integer; s : pchar; begin writeln("Hello world from Pascal"); world; writeln("Returned from C to Pascal\n"); i := 96; 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); getmem (s, 260); { free this here, else memory leak on next line } s := "This is a CString"; { not really a good idea, IMHO } writeln("Trying out pascal ",s); end. Many thanks, Chris.
On 29 Dec 2005 at 13:51, Chris Hicks wrote:
I am beginning to make some progress (thanks for your help so far), but would be very grateful for some more help.
My attempts at producing the XView - Pascal code are in Makefile, p.p, x_message.c and x_message.h.
When I try to compile I get: $ make gpc -c p.p p.p: In main program: p.p:11: error: argument to `WriteLn' is of wrong type p.p:11: error: (Use `--cstrings-as-strings' to treat `CString' as a string.) make: *** [p.o] Error 1
Put "{$X+}" in the source code, or pass '--extended-syntax' at the command line.
Note: many things have changed, and many checks are now stricter. For example, to be able to use a built-in "Result" in functions, then need to pass '--implicit-result' at the command line.
I suggest you read this: http://www.gnu-pascal.de/gpc/h-news.html
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/
On 29 Dec 2005 at 13:51, Chris Hicks wrote:
[...]
I am also wondering whether the Pascal in the 2003 example was quite right. Does the second getmem release the memory? program pascalcallsC(input,output);
procedure world; external name 'c_world'; procedure w1(var i : integer; s : pchar); external name 'c_w1';
var i : integer; s : pchar; begin writeln("Hello world from Pascal"); world; writeln("Returned from C to Pascal\n"); i := 96; 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); getmem (s, 260); { free this here, else memory leak on next line }
This is a memory leak. If you are trying to free the memory here, then what you should call is "freemem", not "getmem".
s := "This is a CString"; { not really a good idea, IMHO }
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.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/
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.
Many thanks, Chris.
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/