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

  • SEARCH
  • Home
  • 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 8040705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:59:07+00:00 2026-06-05T03:59:07+00:00

I have quite a few C structs structured like typedef struct { unsigned int

  • 0

I have quite a few C structs structured like

typedef struct {
    unsigned int a;
    unsigned int b;
} StructA;

And a lot of functions like

void doSomethingWith(StructA*,StructB*,StructC*);

Is there an easy way to call these functions with Haskell FFI? Like, is there something that behaves like the & operator in C? (I imagine there isn’t, but if there was I’d like to know). Do I have to make the Haskell side data‘s instance of storeable (I don’t have any constructor functions for these structs).

Also: If I have to pass a struct instead of a struct pointer (not a hypothetical question, I have a few functions like that – it’s not my code so I can’t do anything about it), can I just pass the components of the struct instead? Like if I want to call

void function(StructA);

can I do this with

foreign import ccall "function" :: CUInt -> CUInt -> IO()

?

  • 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-06-05T03:59:08+00:00Added an answer on June 5, 2026 at 3:59 am

    To pass a reference to Haskell data to C, where the memory is allocated in the Haskell heap, and C will operate on the data directly, you need to:

    • ensure it has the correct shape in memory (via a Storable instance that maps A to the an identical byte structure as StructA).
    • allocate and fill pinned memory on the Haskell heap, via mallocForeignPtr

    There are several consequences to consider in this approach:

    • GHC will de-allocate the value once you drop all references to the ForeignPtr — so you will need to be sure that the C side won’t touch it again
    • You’re letting C mess with stuff on the Haskell heap, so make sure its correct

    Other options:

    • pass opaque references to C via a StablePtr
    • allocate and memory on the C side, and use a finalizer to free it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have controller which extends Controller_Template , and also i have quite few functions
I have a situation where I have quite a few generated functions, and would
I have quite a few RewriteRules in my .htaccess that looks like this RewriteRule
I have quite a few simple functions that I had previously kept in my
I have quite a few places in my iPhone application with a line like
I have quite a few instances where I start with a basic subform, then
I have quite a few situations where I have database structures similar to: TABLE
I have quite a few dictionaries where the key is a composite of several
I have quite a few libraries and models that get loaded into a controller,
I have quite a few problems, probably misunderstandings, about the new keyword with properties

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.