yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
363 stars 95 forks source link

Support for flexible array members #353

Open yallop opened 8 years ago

yallop commented 8 years ago

A common pattern in C libraries is to make the final member of a struct an array whose length is determined at runtime, like this example from the sanlock library:

struct sanlk_resource {
    char lockspace_name[SANLK_NAME_LEN]; /* terminating \0 not required */
    char name[SANLK_NAME_LEN]; /* terminating \0 not required */

       /* ... */

    uint32_t num_disks;
    /* followed by num_disks sanlk_disk structs */
    struct sanlk_disk disks[0];
};

Ctypes doesn't currently offer much/any support for this pattern, but it's sufficiently common that we ought to support it.

yallop commented 8 years ago

/cc @simonjbeaumont

simonjbeaumont commented 8 years ago

@yallop thanks for confirming the workaround too :)

simonjbeaumont commented 8 years ago

I took the time to blog about the current workaround when these are required. http://simonjbeaumont.com/posts/ocaml-ctypes-flexible-array-member/

yallop commented 8 years ago

Thanks! These blog posts are extremely useful.