The Racket FFI's documentation has types for _ptr
, _cpointer
, and _pointer
.1
However, the documentation (as of writing this question) does not seem to compare the three different types. Obviously the first two are functions that produce ctype?
s, where as the last one is a ctype?
itself. But when would I use one type over the other?
1It also has as other types such as _box
, _list
, _gcpointer
, and _cpointer/null
. These are all variants of those three functions.
_ptr
is a macro that is used to create types that are suitable for function types in which you need to pass data via a pointer passed as an argument (a very common idiom in C).
_pointer
is a generic pointer ctype that can be used pretty much wherever a pointer is expected or returned. On the Racket side, it becomes an opaque value that you can't manipulate very easily (you can use ptr-ref
if you need it). Note the docs have some caveats about interactions with GC when using this.
_cpointer
constructs safer variants of _pointer
that use tags to ensure that you don't mix up pointers of different types. It's generally more convenient to use define-cpointer-type
instead of manually constructing these. In other words, these help you build abstractions represented by Racket's C pointers. You can do it manually with cpointer-push-tag!
and _pointer
but that's less convenient.
There's also a blog post I wrote that goes into more detail about some of these pointer issues: http://prl.ccs.neu.edu/blog/2016/06/27/tutorial-using-racket-s-ffi/