opaque pointers

509 views
Skip to first unread message

Will Spearman

unread,
Sep 13, 2013, 3:59:44 PM9/13/13
to pytho...@googlegroups.com
I need to call a c function using cffi that takes a void * pointer defined like this:

typedef void custom_handle_t;

I am trying to create one, but ffi.new() cannot infer the size which make sense:

handle = ffi.new('cmapl_record_handle_t *')

TypeError: cannot instantiate ctype 'void' of unknown size

This clearly works in C source. I am working around it like this:

_ = ffi.new('long int *')
handle = ffi.cast('void *', _)

What is the expected way to do this?

Armin Rigo

unread,
Sep 14, 2013, 3:58:28 AM9/14/13
to pytho...@googlegroups.com
Hi Will,

On Fri, Sep 13, 2013 at 9:59 PM, Will Spearman <willsp...@gmail.com> wrote:
> I need to call a c function using cffi that takes a void * pointer defined
> like this:
>
> typedef void custom_handle_t;
>
> I am trying to create one, but ffi.new() cannot infer the size which make
> sense:
>
> handle = ffi.new('cmapl_record_handle_t *')

I'm a bit unclear about what you're trying to do. Using ffi.new('foo
*') allocates a 'foo' in memory and returns a pointer to it. That
doesn't make sense here: you cannot allocate a 'void' in memory and
return a pointer to it. What is the C code that you're trying to
convert?


A bientôt,

Armin.

Will Spearman

unread,
Sep 14, 2013, 8:22:33 AM9/14/13
to pytho...@googlegroups.com
First, thanks for your development effort and support. Both are much appreciated!

I am using an "external" set of API headers and a C library. The API defines "typedef void * foo" while the internal headers used to create the C library contain the full struct definition. The opaque pointer is passed in to the API functions to be populated and is then passed around in other API calls, but the API user does not need to know any of the struct details. Including the internal headers creates a dependency mess for me, so that is a last resort.

I guess what I really need to do is create a void pointer without any malloc.



--
-- python-cffi: To unsubscribe from this group, send email to python-cffi...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/python-cffi?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "python-cffi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-cffi/jvWPJjHBYSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-cffi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Armin Rigo

unread,
Sep 14, 2013, 8:38:15 AM9/14/13
to pytho...@googlegroups.com
Hi Will,

Can you include a few lines of example code? I still don't get it,
sorry. Notably, how is the user of the external set of headers
supposed to make the "void *"? You can't create a "void *" from
nothing. You need to know at least the size of what it's supposed to
point to.

Maybe it's something like that?

void *handle;
init_stuff(&handle);
more_stuff(handle);

Note the "&handle". The code above translates to using not a "void *"
but a "void **", i.e. a pointer to a "void *" which is the handle. In
the Python code below, "p" would be the equivalent of a C variable
declaration "void **p = &handle;".

p = ffi.new("void **")
init_stuff(p)
more_stuff(p[0])


A bientôt,

Armin.

Will Spearman

unread,
Sep 14, 2013, 10:26:20 AM9/14/13
to pytho...@googlegroups.com
My C-fu was weak. Now I have it thanks to your help!




A bientôt,

Armin.

Message has been deleted

Armin Rigo

unread,
Sep 17, 2017, 2:48:34 AM9/17/17
to pytho...@googlegroups.com
Hi Floris,

On 10 September 2017 at 22:37, Floris Bruynooghe <fl...@devork.be> wrote:
>> p = ffi.new("void **")
>> init_stuff(p)
>> more_stuff(p[0])
>
> Would it be worth adding a section on using opaque pointers like this to
> documentation?

Yes, that's a good point. I've added a similar example to the docs
now, in the working-with-pointers-structures-and-arrays section. Thanks!


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages