Re: obtaining a pointer to a pointer for nlffi

brian <briand-/[email protected]>
Newsgroups gmane.comp.lang.sml.smlnj
Message-ID <[email protected]>
Thanks for the help.

The right way is to do exactly as Vesa says, you simply allocate in  
stages, i.e. allocate n (char *0) and then allocate (len(s)) for each  
pointer.

I actually found it in your ml-lib code :-)

Brian

On Dec 31, 2006, at 4:31 AM, Vesa Karvonen wrote:

> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.