How to interface with C functions that pass returned value by reference?

47 views
Skip to first unread message

H Zhang

unread,
Jan 24, 2014, 2:56:39 PM1/24/14
to ats-lan...@googlegroups.com
Some C library functions pass value through setters. For example to get a string one passes a pointer to a string (char**):
PL_get_atom_chars(term_t some_atom, char** ptr_to_string)

How does one model and interface with C functions like this? I suppose we use references?

Haitao

Brandon Barker

unread,
Jan 24, 2014, 3:02:05 PM1/24/14
to ats-lan...@googlegroups.com
One suggestion was given here. I forgot ATS arrays are actually C-arrays until that post, though I need to go back and verify this for myself.

Brandon Barker

unread,
Jan 24, 2014, 3:03:42 PM1/24/14
to ats-lan...@googlegroups.com
In your example, shouldn't char** also be a pointer to an array of strings? char* is a string.


On Friday, January 24, 2014 2:56:39 PM UTC-5, H Zhang wrote:

gmhwxi

unread,
Jan 24, 2014, 3:11:24 PM1/24/14
to ats-lan...@googlegroups.com
C-types are so vague :(

My guess is:

fun PL_get_atom_chars (term_t, &string? >> string): ...


On Friday, January 24, 2014 2:56:39 PM UTC-5, H Zhang wrote:

H Zhang

unread,
Jan 24, 2014, 5:16:15 PM1/24/14
to ats-lan...@googlegroups.com
I am trying to understand the notation of &string? >> string
Does it say that some type was pointer to a string and on return becomes string?

That is actually not what I wanted, so maybe here it is my English that was vague :-( I believe what the interface function is saying is call me with a pointer to string and I will set it to point to the return string. In C we would do:
char *some_string;
PL_get_atom_chars(some_atom, &some_string);
do_something_with(some_string);

gmhwxi

unread,
Jan 24, 2014, 5:22:42 PM1/24/14
to ats-lan...@googlegroups.com
The meaning of &string? >> string is this:

please pass a left-value whose content is uninitialized string (which is essentially an uninitialized pointer);
after the call returns, the content of the left-value is a string.

So it is precisely what you want according to the description you gave..

H Zhang

unread,
Jan 24, 2014, 6:00:44 PM1/24/14
to ats-lan...@googlegroups.com
I see. But how does one actually call a function like this in ATS? How do I have a var pre-allocated (but uninitialized)? I can't take the reference to an automatic variable like in C, can I?

H Zhang

unread,
Jan 24, 2014, 6:14:43 PM1/24/14
to ats-lan...@googlegroups.com
I experimented and see that is actually works to declare a
var x: string
and then call the function with it and ATS knows to take the reference of x to pass into the function. That is really cool although still a little magic to me. With time I will want to learn more about how this is actually achieved.

Brandon Barker

unread,
Jan 24, 2014, 6:21:06 PM1/24/14
to H Zhang, ats-lan...@googlegroups.com
It may be the case that the type of an unitialized and unallocated variable is the same: if it is unitialized, there is not really a valid view of it, I think

But I think there are views (proofs) for asserting whether or not a particular value is allocated (e.g. the mgsort1 and mgsort2 example on page 162:



Brandon Barker
brandon...@gmail.com


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/45dfd6d9-5aa5-4107-9908-3d92827ca4fa%40googlegroups.com.

H Zhang

unread,
Jan 24, 2014, 6:24:09 PM1/24/14
to ats-lan...@googlegroups.com
Oh. And I still get the wrong type warning from gcc (about expecting char ** but got void **). It seems the tmp variable is declared to be of type atstype_ptrk instead of atstype_string.

gmhwxi

unread,
Jan 24, 2014, 8:56:59 PM1/24/14
to ats-lan...@googlegroups.com
Then you may need to write a thin wrapper to suppress such warnings:

#define atscntrb_PL_get_atom_char (...) PL_get_atom_char (...)
Reply all
Reply to author
Forward
0 new messages