Creating 2D array in CFFI and Lisp

Discussion of Common Lisp

Creating 2D array in CFFI and Lisp

I'm trying to convert this declaration of an array of points to CFFI but I'm getting unhandled memory fault errors Directly below this C declaration is a tree of foreign-alloc functions I used to create this I'm hoping someone can check my work and tell me if it is my declaration that's the problem

Code: Select all
`Point pt[2][3];    pt[0][0].x = rng.uniform(x_1, x_2);    pt[0][0].y = rng.uniform(y_1, y_2);    pt[0][1].x = rng.uniform(x_1, x_2);    pt[0][1].y = rng.uniform(y_1, y_2);    pt[0][2].x = rng.uniform(x_1, x_2);    pt[0][2].y = rng.uniform(y_1, y_2);    pt[1][0].x = rng.uniform(x_1, x_2);    pt[1][0].y = rng.uniform(y_1, y_2);    pt[1][1].x = rng.uniform(x_1, x_2);    pt[1][1].y = rng.uniform(y_1, y_2);    pt[1][2].x = rng.uniform(x_1, x_2);    pt[1][2].y = rng.uniform(y_1, y_2);    const Point* ppt[2] = {pt[0], pt[1]};`

Code: Select all
`(defparameter a (foreign-alloc :pointer :count 2 :initial-contents(list                           (foreign-alloc :pointer :count 3 :initial-contents(list                                                   (foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2)))(foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2)))(foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2)))))(foreign-alloc :pointer :count 3 :initial-contents(list                                                   (foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2)))(foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2)))(foreign-alloc :pointer :initial-element                                                   (point (uniform rng x-1 x-2) (uniform rng y-1 y-2))))))))`

Here is how I have point defined it works as intended. The uniform function works well in the point so I didn't post but will if necessary

Code: Select all
`;; Point* cv_create_Point(int x, int y) (defcfun ("cv_create_Point2" point2) (:pointer point)  "Point constructor"  (x :int)  (y :int)) `
joeish80829

Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: Creating 2D array in CFFI and Lisp

You may have a misunderstanding about how arrays work in C/C++.

Consider the following example.

Code: Select all
`int32_t x[2][3][4];int32_t *y=&x[0][0][0];`

When the compiler sees "int32_t x[2][3][4];", it allocates a single block of memory for 2*3*4*sizeof(int32_t)=96 bytes.

The integer at x[0][0][1] is at address "y+1". Since "y" is an int32_t pointer, the absolute address is 1*sizeof(int32_t) = 4 bytes after y.
The integer at x[a][b][c] is at address "y+((a*3)+b)*4+c".

In Java, a multidimensional array is handled as an array of pointers to arrays to arrays ... to data.
That looks like what you were trying to implement with all the foreign-allocs in CFFI.

nuntius

Posts: 532
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Creating 2D array in CFFI and Lisp

Thanks for your answer... can u show me how to write a 2d array of pointers in CFFI?
joeish80829

Posts: 153
Joined: Tue Sep 03, 2013 5:32 am