From: Francogrex on
Hi I'm having some troubles using from within cffi a function that
returns a pointer. Please see below maybe you can give me ideas:

;;the C function that will become pointer.dll
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
#include <stdio.h>
#include <stdlib.h>

int _stdcall myarray(int sz)
{
int *end;
int pin [4]={sz,2,73,4};
end= pin;
return *end;
}

;;the cffi code
(cffi:load-foreign-library "C:/pointer.dll")
(cffi:defcfun ("myarray" myarray) :pointer (sz :int))
(MYARRAY 8)
;;This gives: #<foreign :POINTER-VOID 00000008>
(cffi:mem-aref (MYARRAY 8) :int 0)
;;This gives an error of segmentation violation
From: Pascal J. Bourguignon on
Francogrex <franco(a)grex.org> writes:

> Hi I'm having some troubles using from within cffi a function that
> returns a pointer. Please see below maybe you can give me ideas:
>
> ;;the C function that will become pointer.dll
> #ifdef BUILD_DLL
> #define EXPORT __declspec(dllexport)
> #else
> #define EXPORT __declspec(dllimport)
> #endif
> #include <stdio.h>
> #include <stdlib.h>
>
> int _stdcall myarray(int sz)
> {
> int *end;
> int pin [4]={sz,2,73,4};
> end= pin;
> return *end;
> }
>
> ;;the cffi code
> (cffi:load-foreign-library "C:/pointer.dll")
> (cffi:defcfun ("myarray" myarray) :pointer (sz :int))
> (MYARRAY 8)
> ;;This gives: #<foreign :POINTER-VOID 00000008>
> (cffi:mem-aref (MYARRAY 8) :int 0)
> ;;This gives an error of segmentation violation

This is not surprizing, this function is wrong, even in a C program.
Please use only fully debugged and correct C function with your CFFI
wrappers. It's already hard enough as it is...


--
__Pascal Bourguignon__
From: Paul Tarvydas on
In the C code, try "return end;" or "return pin;" instead of "return *end;".
pt


From: Spiros Bousbouras on
On Fri, 27 Nov 2009 10:40:44 -0500
Paul Tarvydas <tarvydas(a)visualframeworksinc.com> wrote:
> In the C code, try "return end;" or "return pin;" instead of "return *end;".
> pt

The C code in question is

int _stdcall myarray(int sz)
{
int *end;
int pin [4]={sz,2,73,4};
end= pin;
return *end;

}

I don't know what _stdcall does but I doubt that it is so powerful that
it fixes the issue of returning a pointer to automatic storage.
From: Paul Tarvydas on
Spiros Bousbouras wrote:

> On Fri, 27 Nov 2009 10:40:44 -0500
> Paul Tarvydas <tarvydas(a)visualframeworksinc.com> wrote:
>> In the C code, try "return end;" or "return pin;" instead of "return
>> *end;". pt
>
> The C code in question is
>
> int _stdcall myarray(int sz)
> {
> int *end;
> int pin [4]={sz,2,73,4};
> end= pin;
> return *end;
>
> }
>
> I don't know what _stdcall does but I doubt that it is so powerful that
> it fixes the issue of returning a pointer to automatic storage.

Yes, I missed the "sz" stuck in the array initializer. This C code is
utterly wrong.

(iirc, _stdcall changes the order of args on the stack - Windows uses Pascal
order instead of C order for system calls).

pt