Dereference c struct member not same as in CFFI/Lisp

Discussion of Common Lisp

Dereference c struct member not same as in CFFI/Lisp

Postby joeish80829 » Sat Nov 02, 2013 10:57 pm

here is the c code with explanation didn't add all code to keep it short but i know it is still long...if this seems daunting because all the code I added....pls know 1. I worked on this a long time and added the minimal code i could think to explain the issue 2. if it does seem daunting...if you would be so kind as to look through this and tell me how i could edit my ? to make it easier to answer...with that being said... though this is a very simple post .... Im hoping at the end of the explanation you might be able to help me re-wrap my CV-SEQ struct so i get the same resultt as i do in c

Code: Select all
CvSeq* contours;  //hold the pointer to a contour in the memory block

// adds data to CvSeq* contours with &contours call
cvFindContours(imgGrayScale, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

// This is how its supposed to work ....no matter how many times you dereference contours to get the h_next struct member
// the value of contours->h_next  is always the same

cout << "c1 = " << endl << " " << contours->h_next << endl << endl;  //output same as next line - see output below
cout << "c2 = " << endl << " " << contours->h_next << endl << endl; //output same as above line see output below

// If you assign contours->h_next to a new variable "c"

CvSeq* c = contours->h_next;

// then cout contours->h_next the output of the below 2 cout's is  still the same as the output of the above 2 - see output below

cout << "c3 = " << endl << " " << contours->h_next << endl << endl;
cout << "c4 = " << endl << " " << contours->h_next << endl << endl;

// but  if you assign contours->h_next back to contours the output of the next 2 couts is different than above 4
// see output below

 contours = contours->h_next;
cout << "c5 = " << endl << " " << contours->h_next << endl << endl;
cout << "c6 = " << endl << " " << contours->h_next << endl << endl;


here is the cout output

Code: Select all
c1 =
 0x182f730

c2 =
 0x182f730

c3 =
 0x182f730

c4 =
 0x182f730

c5 =
 0x182f560

c6 =
 0x182f560



The struct CvSeq is defined as this

Code: Select all
#define CV_TREE_NODE_FIELDS(node_type)                               \
    int       flags;             /* Miscellaneous flags.     */      \
    int       header_size;       /* Size of sequence header. */      \
    struct    node_type* h_prev; /* Previous sequence.       */      \
    struct    node_type* h_next; /* Next sequence.           */      \
    struct    node_type* v_prev; /* 2nd previous sequence.   */      \
    struct    node_type* v_next  /* 2nd next sequence.       */
 
/*
   Read/Write sequence.
   Elements can be dynamically inserted to or deleted from the sequence.
*/
#define CV_SEQUENCE_FIELDS()                                              \
    CV_TREE_NODE_FIELDS(CvSeq);                                           \
    int       total;          /* Total number of elements.            */  \
    int       elem_size;      /* Size of sequence element in bytes.   */  \
    schar*    block_max;      /* Maximal bound of the last block.     */  \
    schar*    ptr;            /* Current write pointer.               */  \
    int       delta_elems;    /* Grow seq this many at a time.        */  \
    CvMemStorage* storage;    /* Where the seq is stored.             */  \
    CvSeqBlock* free_blocks;  /* Free blocks list.                    */  \
    CvSeqBlock* first;        /* Pointer to the first sequence block. */
 
typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
}
CvSeq;

Im not getting the same thing as in lisp with my struct like this - SWIG wrapped

Code: Select all
(cffi:defcstruct cv-seq
  (flags :int)
  (header-size :int)
  (h-prev :pointer)
  (h-next :pointer)
  (v-prev :pointer)
  (v-next :pointer)
  (total :int)
  (elem-size :int)
  (block-max :pointer)
  (ptr :pointer)
  (delta-elems :int)
  (storage :pointer)
  (free-blocks :pointer)
  (first :pointer))



with the below code my output is the same for both princ...not newline of course =)

Code: Select all
(with-foreign-object (contours '(:pointer (:struct cv-seq)))
      (find-contours img-grayscale  storage contours
             (size-of cv-contour) +retr-list+
             +chain-approx-simple+ (point 0 0))

      (with-foreign-slots ((h-next) contours (:struct cv-seq))
    (setf contours  h-next)
    (princ contours)
    (princ #\newline)
    (setf contours  h-next)
    (princ contours)))


any help on fixing my struct, my coding or editing this post =) is much appreciated

Cheers!
joeish80829
 
Posts: 152
Joined: Tue Sep 03, 2013 5:32 am

Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 4 guests