[Haskell-cafe] Begginer question about alignment in Storable

Michael D. Adams mdmkolbe at gmail.com
Sat Nov 15 16:47:02 EST 2008


If you are using hsc2hs (if you are using Cabal this is easy; just
rename the file to *.hsc and Cabal will take care of the rest), then
there is a macro for making this easier and so you don't have to think
about it.

First, place the following at the top of your source file:

#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t
(y__); }, y__)

Then #{alignment c_type} will always be expanded by hsc2hs to the
correct alignment for the C type named c_type.

On Sat, Nov 15, 2008 at 3:29 PM, Maurí­cio <briqueabraque at yahoo.com> wrote:
> Hi,
>
> I need to make the following structure into
> an instance of Storable:
>
> struct {
>  double w[2];
> }
>
> Can I assume that its function 'alignment' should
> return the same as "alignment (1::CDouble)"? Or
> should it be "2 * alignment (1::CDouble)"?
>
> (I've read the wikipedia article about memory alignment,
> and what I understand is that there's a better alignment
> for hardware efficiency but compilers and programmers can
> do almost anything about it. What can I do when writing
> a library wrapper if I don't know what kind of compiler
> pragmas were used in all platforms that library was
> compiled?)
>
> Thanks,
> Maurício
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list