Storable empty data declaration

前端 未结 2 1686
野趣味
野趣味 2021-02-14 21:28

I\'m attempting to create a Haskell wrapper for a C library. The underlying structs are too complicated to express as explicit types, and I don\'t actually use them other than f

相关标签:
2条回答
  • 2021-02-14 21:49

    You can't allocate something if you don't know how big it is. Is the function just going to ignore its argument? Then pass in a null pointer. Otherwise, you need to actually allocate enough space for the struct - don't cut corners by allocating a zero-byte or pointer-sized buffer, as then the called function will write past the end of your buffer, corrupting memory.

    Either finish the data declaration, or write a Storable instance with proper size and alignment values; there's no way around providing size/alignment data in some form.

    0 讨论(0)
  • 2021-02-14 22:05

    Here's another approach that might work for you. I assume that you have access to all of the C header files that define the objects you need to allocate. If that's true, you could write a thin layer of C code for allocating and freeing the C objects. Your Haskell code can then call these C functions without the Haskell code ever needing to know what's behind the pointers. Haskell can also automatically call the free code when Haskell's garbage collector knows that the objects are no longer needed.

    0 讨论(0)
提交回复
热议问题