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

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

Then you may need to write a thin wrapper to suppress such warnings:

#define atscntrb_PL_get_atom_char (…) PL_get_atom_char (…)On Friday, January 24, 2014 6:24:09 PM UTC-5, H Zhang wrote:

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.

On Friday, January 24, 2014 3:14:43 PM UTC-8, H Zhang wrote:

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.

On Friday, January 24, 2014 3:00:44 PM UTC-8, H Zhang wrote:

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?

On Friday, January 24, 2014 2:22:42 PM UTC-8, gmhwxi wrote:

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…

On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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

One suggestion was given herehttps://groups.google.com/d/msg/ats-lang-users/tBInziRBi1c/UwjuLyKFMswJ. I
forgot ATS arrays are actually C-arrays until that post, though I need to
go back and verify this for myself.On Friday, January 24, 2014 2:56:39 PM UTC-5, H Zhang wrote:

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

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:

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

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.On Friday, January 24, 2014 3:14:43 PM UTC-8, H Zhang wrote:

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.

On Friday, January 24, 2014 3:00:44 PM UTC-8, H Zhang wrote:

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?

On Friday, January 24, 2014 2:22:42 PM UTC-8, gmhwxi wrote:

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…

On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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

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:
http://www.ats-lang.org/DOCUMENT/INTPROGINATS/PDF/main.pdf ).

Brandon Barker
brandon…@gmail.comOn Fri, Jan 24, 2014 at 6:14 PM, H Zhang zht...@gmail.com wrote:

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.

On Friday, January 24, 2014 3:00:44 PM UTC-8, H Zhang wrote:

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?

On Friday, January 24, 2014 2:22:42 PM UTC-8, gmhwxi wrote:

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…

On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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


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
.

C-types are so vague :frowning:

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:

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

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?On Friday, January 24, 2014 2:22:42 PM UTC-8, gmhwxi wrote:

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…

On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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

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 :frowning: 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);On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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

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…On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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

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.On Friday, January 24, 2014 3:00:44 PM UTC-8, H Zhang wrote:

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?

On Friday, January 24, 2014 2:22:42 PM UTC-8, gmhwxi wrote:

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…

On Friday, January 24, 2014 5:16:15 PM UTC-5, H Zhang wrote:

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 :frowning: 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);

On Friday, January 24, 2014 12:11:24 PM UTC-8, gmhwxi wrote:

C-types are so vague :frowning:

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:

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