Re: obtaining a pointer to a pointer for nlffi
Vesa Karvonen <vesa.karvonen-bbCR+/[email protected]>
| Newsgroups | gmane.comp.lang.sml.smlnj |
|---|---|
| Message-ID | <[email protected]> |
Quoting brian <briand-/[email protected]>: > I need a char ** for the arg to a function. > > So I created a char * to actually hold the string > > stringList = C.alloc C.T.uchar (Word.fromInt (size name + 1)) > > Then I tried the obvious: > > C.Ptr.|&| stringList > > but I get an error: > > operator domain: ('Z,'Y) C.obj > operand: (C.uchar,'X) C.obj C.ptr > in expression: > C.Ptr.|&| stringList > > The error message makes perfect sense, it's trying to create a ptr to > an obj, but I already have a ptr, not an obj. Unfortunately I can't > figure out how to make it take the ptr of a ptr. Here is one way that seems to compile (with MLton): (* allocate a char** object - must be deallocated somewhere! *) val cptrptr = C.new (C.T.pointer C.T.uchar) (* allocate a char array - must be deallocated somewhere! *) val cptr = C.alloc C.T.uchar (Word.fromInt 10) (* set the char** to point to the char array *) val () = C.Set.ptr (cptrptr, cptr) I believe that in NLFFI you can only take the address of a _C object_. A C object is something that lives on the C heap. When you allocate an array using C.alloc, you only allocate a C object to store the array - no C object is allocated to store a pointer to the array. The "pointer" that is returned by C.alloc is an ML object (it lives on the ML heap). You can't take the address of an ML object, because, for one thing, they have no (stable/permanent) addresses - GC can move them around. -Vesa Karvonen ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV