ffi.addressof doesn't work on pointers

Issue #94 wontfix
Alex Gaynor
created an issue

Example code:

(ae9fdccb25e9070a)Alexanders-MacBook-Pro:ae9fdccb25e9070a alex_gaynor$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cffi
>>> ffi = cffi.FFI()
>>> ffi.cdef("""
... struct S {
...  ...;
... };
... """)
>>> lib = ffi.verify("""
... struct S {
...  long x;
... };
... """)
clang: warning: argument unused during compilation: '-mno-fused-madd'
>>> ffi.new("struct S *")
<cdata 'struct S *' owning 8 bytes>
>>> ffi.addressof(ffi.new("struct S *"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alex_gaynor/.virtualenvs/ae9fdccb25e9070a/lib/python2.7/site-packages/cffi/api.py", line 331, in addressof
    ctype, offset = self._backend.typeoffsetof(ctype, field)
TypeError: expected a struct or union ctype

Comments (1)

  1. Armin Rigo

    ffi.addressof() is documented to only work on struct or union types. The reason it cannot work on other types (like pointers) is that this ability would prevent many optimizations; for example, for now, pointer cdata's are immutable, and so can be freely exploded by the PyPy JIT.

    If you need to have a pointer object to which you can take a pointer, you need to allocate it correspondingly: ffi.new("struct S *[1]") gives an array of 1 such pointer. (Updated the docs with this example)

  2. Log in to comment