Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6098493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:09:53+00:00 2026-05-23T13:09:53+00:00

i need to know how can i resolve Unions and Type** (e.g. int**) with

  • 0

i need to know how can i resolve Unions and Type** (e.g. int**) with the FFI?
I know that i need a Storable instance for structs, could i use it for unions too?

a union like so:

typedef union {
     int i;
     char c;
} my_union;

This would typically be represented in Haskell as:

data MyUnion = I CInt | C CChar

My question is how would you marshall (define an Storable instance for)
myUnion into my_union? It’s my understanding that an instance my_union
would take up sizeof(int) bytes in memory, i.e. the size of it’s largest
member. So to store this we would write something along the lines of:

instance Storable myUnion where
     size _ = #{size my_union} -- <-- hsc2hs shortcut
     alignment _ = alignment undefined::CInt -- <-- What should this really be?
     peek ptr = do -- <-- How are you supposed to know which element to extract?
     poke ptr (I i) =  poke ptr i -- <-- Or should this be #{poke my_union, i} ptr i ?
     poke ptr (C c) = poke ptr c

Also, how can you represent a int** with the FFI?
when i got a function like int foo(int i1, int* i2);
the signature would be: foo -> CInt -> Ptr CInt -> CInt

but what if there is: int foo(int i1, int** i2);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:09:54+00:00Added an answer on May 23, 2026 at 1:09 pm

    C unions are not tagged unions, see wikipedia on this. In haskell MyUnion would take up more memory than a single raw (unboxed) 64-bit int. In GHC it would be a special pointer to either a thunk or a value: the thunk is when a lazy MyUnion has not been evaluated yet, the value is for when it has been evaluated and the pointed-to memory size can vary (unlike unions in C). The “special” pointer would use the usually-zero low bits of the 64-bit pointer to indicate whether it is known to be a C or I value, to combine the tagging with the pointer.

    A less-lazy declaration in Haskell can be made with

    data MyUnion1 = I !Int | C !Char
    data MyUnion2 = I {-# UNPACK #-} !Int | C {-# UNPACK #-} !Char
    

    Where the “!” indicates the value is never stored as an unevaluated thunk. The UNPACK compiler pragma comment asks GHC to store the raw unboxed value alongside the tag instead of storing a pointer to the Int or Char. So MyUnion2 may take up less memory and will be strict instead of lazy.

    Also, I should emphasize “char” from C is a single signed byte while “Char” in Haskell is a full unicode code point (value 0 to 1114111). To store a C “char in Haskell you would use a CChar.

    You have unions in use in C and need to serialize and desearialize them? Do you already have a binary format in use by C ? If you need to invent a binary format then you do need to design a tag to make Haskell happy. Your C example has no way to tell whether the value was “constructed” with an int or a char, while MyUnion in Haskell can tell whether the value was constructed by an I or a C.

    The C type you wrote is also quite dangerous, as if I write to the single byte “char” and read the multi-byte “int” the rest of the bytes in the “int” are likely undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to know how can i connect telephone using php languages
I need to know how can I get every instruction's duration so I can
I'm working in a C++ unmanaged project. I need to know how can I
I need to know if I can create a file in a specific folder,
I need to know how I can detect the current application pool I am
I need to know how I can replace the last s from a string
I need to know how i can make a string like hello to Hello
I need to know how I can insert data inside objects inside an array
I have an unsorted array of objects. I need to know how I can
I need to know if any JSON implementations can handle sparse arrays to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.