I am now trying to pass a string as well as an integer between C and Pascal. I am getting a segmentation fault. I would be grateful if tell me what I have done wrong. I attach the files pcallc.p, ccall.c ccall.h and Makefile and show the output at the end.
Many thanks, Chris
pcallc.p: [ program pascalcallsC(input,output);
procedure world; external name 'world'; procedure w1(var i : integer; var s : cstring); external name 'c_w1';
var i : integer; s : cstring; begin writeln("Hello world from Pascal"); world; writeln("Returned from C to Pascal\n"); i := 96; w1(i,s); writeln("Returned from C again I = ",i); writeln("Return from C cstring = ",s); {segmentation fault on this line} s := "This is a CString"; writeln("Trying out pascal ",s); end. ]
ccall.c: [ #include <stdio.h> #include <string.h> #include "ccall.h"
void world (void) { printf("\nHello World from C\n"); }
void c_w1 (int *i, char *s) { printf("Now in function c_w1\n"); strcpy(s,"Mary had a little lamb\n"); printf("Hello again from C i = %i\n%sXXX\n",(*i),s); (*i) = 56; printf("Leaving C\n"); } ]
ccall.h: [ extern void world(void); extern void w1(int *i, char *s); ]
Makefile: [ pcallc : pcallc.p ccall.o gpc -o pcallc pcallc.p ccall.o --cstrings-as-strings ccall.o : ccall.c gcc -c ccall.c clean: rm *.o pcallc ccall.o ]
output: [nmansys@mmme1 pcallsc2]$ pcallc Hello world from Pascal
Hello World from C Returned from C to Pascal
Now in function c_w1 Hello again from C i = 96 Mary had a little lamb XXX Leaving C Returned from C again I = 56 Segmentation fault
Dr Christian Hicks Senior Lecturer, School of Mechanical & Systems Engineering, Stephenson Building, University of Newcastle upon Tyne, NE1 7RU. Phone: +44 191 222 6238 Mobile 0795 8317804 Fax: + 44 191 222 8600 Homepage: http://www.staff.ncl.ac.uk/chris.hicks
Dr Christian Hicks wrote:
I am now trying to pass a string as well as an integer between C and Pascal. I am getting a segmentation fault.
As I wrote:
: However, for `const char *', do not use : `protected var Foo: CString' (because `CString' is already a pointer : so it would pass a pointer to a pointer), but simply `CString'.
Frank
On 22 Nov 2003 at 2:18, Frank Heckenbach wrote:
Dr Christian Hicks wrote:
I am now trying to pass a string as well as an integer between C and Pascal. I am getting a segmentation fault.
As I wrote:
: However, for `const char *', do not use : `protected var Foo: CString' (because `CString' is already a pointer : so it would pass a pointer to a pointer), but simply `CString'.
That is not the only problem. Who owns the memory for the CString "s"? and who is allocating memory for it? Things are being copied to this variable without first having allocated any memory to it. This will work:
program pascalcallsC(input,output);
procedure world; external name 'world'; procedure w1(var i : integer; s : cstring); external name 'c_w1';
var i : integer; s : cstring; 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.
BTW: this is why is personally prefer "PChar" to "CString", because "PChar" seems to make it more obvious (at least to someone who is not very familiar with how strings work in C) that we are dealing with a pointer.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.bigfoot.com/~african_chief/