From: Ivan Boldyrev
Subject: FFI with CMU CL problem: array of strings
Date: 
Message-ID: <5lpnixndi.ln2@elaleph.borges.uiggm.nsc.ru>
I am trying to call this (sample) function from CMU CL:

int str(int n,char **z) {
    int i;
    int sum=0;
    FILE *f=fopen("text", "rw");
    for (i=0; i<n; ++i) {
	fprintf(f, "%s\n", z[i]);
    }
    fclose(f);
    return sum;
}

My Lisp code is:

(def-alien-routine "str" int
  (n int :in)
  (z (* c-string)))

(defun strings (strarr)
  ;; strarr is array of strings
  (declare (type (array string) strarr))
  (let ((size (array-dimension strarr 0)))
    (with-alien ((astrs (* c-string) (make-alien c-string size)))
      (dotimes (i size)
	;; Alloc memory
	(setf (deref astrs i) (make-alien char (1+ (length (aref strarr i)))))
	;; Fill with data
	(setf (deref astrs i) (aref strarr i)))
      (prog1
	  (str size astrs)
	(dotimes (i size)
	  (free-alien (deref astrs i)))
	(free-alien astrs)))))

But when I call 

(strings (make-array 1 :initial-element "aaaa"))

I get

Error in function UNIX::SIGSEGV-HANDLER:  Segmentation Violation at #x400A4792.

:(

Does anyone sees my mistake?

P.S. I am going to create Lisp bindings to PVM (Parallel Virtual
Machine, library for parallel computations) for CMU LC and I study CMU
CL FFI.

P.P.S. Oh, I fogot to google on PVM+Lisp before. :)

-- 
Ivan Boldyrev                 remove .microsoft.com from my address
PGP fp: 3640 E637 EE3D AA51 A59F 3306 A5BD D198 5609 8673  ID 56098673

                        Today is the first day of the rest of your life.

From: Fred Gilham
Subject: Re: FFI with CMU CL problem: array of strings
Date: 
Message-ID: <u7n0kl74u3.fsf@snapdragon.csl.sri.com>
Ivan Boldyrev <········@uiggm.nsc.ru.microsoft.com>, after coming up
for air, wrote:

> I am trying to call this (sample) function from CMU CL:
> 
> int str(int n,char **z) {
>     int i;
>     int sum=0;
>     FILE *f=fopen("text", "rw");
>     for (i=0; i<n; ++i) {
> 	fprintf(f, "%s\n", z[i]);
>     }
>     fclose(f);
>     return sum;
> }
>
> [stuff omitted]
>
> I get
> 
> Error in function UNIX::SIGSEGV-HANDLER:  Segmentation Violation at
> #x400A4792.
> 
> :(
> 
> Does anyone sees my mistake?

Well, even though it's lisp talking to your C code, you still have to
check your return values! :-)

#include <stdio.h>

int str(int n,char **z)
{
  int i;
  int sum = 0;
  FILE *f=fopen("text", "rw");

  if(f == NULL) {
    perror("str");
    exit(1);
  }

  for (i=0; i<n; ++i) {
    fprintf(f, "%s\n", z[i]);
  }

  fclose(f);
  return sum;
}

int
main (int argc, char *argv[])
{

  char *a[4];

  a[0] = "AAAA";
  a[1] = "BBBB";
  a[2] = "CCCC";
  a[3] = "DDDD";

  str(4, a);
}




snapdragon:~/ffi-test > make test-str
cc     test-str.c   -o test-str
snapdragon:~/ffi-test > ./test-str 
str: No such file or directory


Changing "rw" above to "w+" fixes that.



snapdragon:~/ffi-test > make test-str
cc     test-str.c   -o test-str
snapdragon:~/ffi-test > ./test-str 
snapdragon:~/ffi-test > cat text 
AAAA
BBBB
CCCC
DDDD

When I try to run your C code from lisp, I get another error, but I'll
leave that to you for now.

-- 
Fred Gilham                                   ······@csl.sri.com
Lisp has jokingly been called "the most intelligent way to misuse a
computer". I think that description is a great compliment because it
transmits the full flavor of liberation: it has assisted a number of
our most gifted fellow humans in thinking previously impossible
thoughts.   E. Dijkstra
From: Ivan Boldyrev
Subject: Re: FFI with CMU CL problem: array of strings
Date: 
Message-ID: <tn3rix593.ln2@elaleph.borges.uiggm.nsc.ru>
On 8300 day of my life Fred Gilham wrote:
> Ivan Boldyrev <········@uiggm.nsc.ru.microsoft.com>, after coming up
> for air, wrote:
> 
> > I get
> > 
> > Error in function UNIX::SIGSEGV-HANDLER:  Segmentation Violation at
> > #x400A4792.
> > 
> > :(
> > 
> > Does anyone sees my mistake?
> 
> Well, even though it's lisp talking to your C code, you still have to
> check your return values! :-)
> 
> 
> Changing "rw" above to "w+" fixes that.

Thank you very much! I had use "rw" in damned Borland C++ for DOS, and
wrote it here by mistake. I will always read man pages :)
 
> When I try to run your C code from lisp, I get another error, but I'll
> leave that to you for now.

I solved it, thank you. I also allocated tons of unused raw memory :)

Now it seems to work.

-- 
Ivan Boldyrev                 remove .microsoft.com from my address
PGP fp: 3640 E637 EE3D AA51 A59F 3306 A5BD D198 5609 8673  ID 56098673

			       There are 256 symbols in English alphabet.
From: Ivan Boldyrev
Subject: Re: FFI with CMU CL problem: array of strings
Date: 
Message-ID: <2irpix1cn.ln2@elaleph.borges.uiggm.nsc.ru>
Problem is solved, thanks to everyone who replyed to group :)

-- 
Ivan Boldyrev                 remove .microsoft.com from my address
PGP fp: 3640 E637 EE3D AA51 A59F 3306 A5BD D198 5609 8673  ID 56098673

Violets are red, Roses are blue. //
I'm schizophrenic, And so am I.