Page 1 of 1

Creating 2D array in CFFI and Lisp

Posted: Mon Mar 31, 2014 10:41 pm
by joeish80829
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))
 

Re: Creating 2D array in CFFI and Lisp

Posted: Wed Apr 02, 2014 8:00 am
by nuntius
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][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.

Re: Creating 2D array in CFFI and Lisp

Posted: Fri Apr 11, 2014 4:59 pm
by joeish80829
Thanks for your answer... can u show me how to write a 2d array of pointers in CFFI?