Can I assume that string and bytes are 32-bit aligned?

I want to emit bytecode from OCaml and interpret it from C. Let’s say that each instruction is 32 bits. To my understanding: An OCaml object consists of a header and then a vector of cells. Each cell is word size to fit an OCaml value. The string or bytes buffer is stored using this memory, so it should have word alignment (reasonably at least 32 bits).

I think you might want to use BigArray here. The buffer in a string or bytes box will typically be word aligned, but I think that’s strictly an implementation detail and unsafe to assume. Whereas BigArray guarantees the alignment of its element kind is respected.

2 Likes