building and analysing C structs with Storable

Stefan O'Rear stefanor at
Sun Oct 14 15:23:00 EDT 2007

On Sun, Oct 14, 2007 at 09:19:24PM +0200, Henning Thielemann wrote:
> On Sat, 13 Oct 2007, Adam Langley wrote:
>> On 10/13/07, Henning Thielemann <lemming at> wrote:
>>> Are there analogons to ByteString's Put and Get Monads to construct and
>>> disect binary data in a C struct for exchange with a C program?
>> The memory layout of structs depends on the ABI of system. You can
>> take a guess and write code in the Get and Put monads. Your guess will
>> probably work pretty well within a single architecture (e.g. x86 or
>> x86-64).
>> However, the portable way to do this is to get the information from
>> the C compiler, which is the approach that c2hs[1] and friends use. I
>> would recommend this unless there's some good reason to think
>> otherwise. (see [2] for the exact hook)
>> [1]
>> [2]
> I do not quite see the need for an extra tool. I thought it must be 
> possible to ship a Haskell compiler with modules that depend on the 
> system's C compiler, just like the modules that are implemented differently 
> for Windows and Unix. Such modules could provide the functionality to 
> create and inspect C structs for exchange with system libraries.

You say "the system's C compiler" as if there was only one.  It's quite
common for UNIXoid systems to have several C compilers installed
simultaneously; and if you use the module corresponding to the wrong
compiler, you get silent data loss.  I wouldn't risk it.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url :

More information about the Libraries mailing list