[Cffi-devel] Possible mem-aref bug or maybe my error on accessing member of a pointer to a struct

Willem Rein Oudshoorn woudshoo at xs4all.nl
Sat Apr 26 11:32:07 UTC 2014


Joeish W <joeish80829 at yahoo.com> writes:

> I have this C wrapper 
>
>
> DMatch* cv_create_DMatch3(int _queryIdx, int _trainIdx, float _distance) {
>     return new DMatch(_queryIdx, _trainIdx, _distance);
> }
>
>
> it is a wrapper for these struct members
>
> http://docs.opencv.org/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html?highlight=dmatch#DMatch
>
>
> here is my lisp wrapper for it:
>
> (defctype dmatch :pointer)
>
> (defcfun ("cv_create_DMatch3" dmatch3) (:pointer dmatch) 
>   "DMatch constructor"
>   (-query-idx :int)
>   (-train-idx :int)
>   (-distance :float))
>
> In C I can do this:
>
>   DMatch* a = cv_create_DMatch3(1, 2, 3.0);
>   cout << a->distance ;
>
> and the output is 3
>
> but in lisp when I do this:
>
> (defparameter a (dmatch3 1 2 3f0))
>
> LCV> (mem-aref a :int 0) 
> 1
> LCV> (mem-aref a :int 1) 
> 2
> LCV> (mem-aref a :float 2) 
> #<SINGLE-FLOAT quiet NaN> <--- this is output
>
>
> Any help on figuring out the reason would be much appreciated


This is not really suprising.  You are treating the resulting pointer
`a` as an untyped pointer.  
When you do `(mem-aref a :float 2)` you are basically saying:

1. Assume a is a pointer to an array of floats
2. Give me the third element (element at index 2).

Unfortunately `a` is not a pointer to an array of floats, but a pointer
to a struct DMatch.

You should lookup `defcstruct` in the CFFI documentation:

http://common-lisp.net/project/cffi/manual/html_node/defcstruct.html#defcstruct

Wim Oudshoorn.





More information about the cffi-devel mailing list