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 7939063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:59:06+00:00 2026-06-03T22:59:06+00:00

The question says it all. More specifically, I am writing bindings to a C

  • 0

The question says it all. More specifically, I am writing bindings to a C library, and I’m wondering what c functions I can use unsafePerformIO with. I assume using unsafePerformIO with anything involving pointers is a big no-no.

It would be great to see other cases where it is acceptable to use unsafePerformIO too.

  • 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-03T22:59:08+00:00Added an answer on June 3, 2026 at 10:59 pm

    In the specific case of the FFI, unsafePerformIO is meant to be used for calling things that are mathematical functions, i.e. the output depends solely on the input parameters, and every time the function is called with the same inputs, it will return the same output. Also, the function shouldn’t have side effects, such as modifying data on disk, or mutating memory.

    Most functions from <math.h> could be called with unsafePerformIO, for example.

    You’re correct that unsafePerformIO and pointers don’t usually mix. For example, suppose you have

    p_sin(double *p) { return sin(*p); }
    

    Even though you’re just reading a value from a pointer, it’s not safe to use unsafePerformIO. If you wrap p_sin, multiple calls can use the pointer argument, but get different results. It’s necessary to keep the function in IO to ensure that it’s sequenced properly in relation to pointer updates.

    This example should make clear one reason why this is unsafe:

    # file export.c
    
    #include <math.h>
    double p_sin(double *p) { return sin(*p); }
    
    # file main.hs
    {-# LANGUAGE ForeignFunctionInterface #-}
    
    import Foreign.Ptr
    import Foreign.Marshal.Alloc
    import Foreign.Storable
    
    foreign import ccall "p_sin"
      p_sin :: Ptr Double -> Double
    
    foreign import ccall "p_sin"
      safeSin :: Ptr Double -> IO Double
    
    main :: IO ()
    main = do
      p <- malloc
      let sin1  = p_sin p
          sin2  = safeSin p
      poke p 0
      putStrLn $ "unsafe: " ++ show sin1
      sin2 >>= \x -> putStrLn $ "safe: " ++ show x
    
      poke p 1
      putStrLn $ "unsafe: " ++ show sin1
      sin2 >>= \x -> putStrLn $ "safe: " ++ show x
    

    When compiled, this program outputs

    $ ./main 
    unsafe: 0.0
    safe: 0.0
    unsafe: 0.0
    safe: 0.8414709848078965
    

    Even though the value referenced by the pointer has changed between the two references to “sin1”, the expression isn’t re-evaluated, leading to stale data being used. Since safeSin (and hence sin2) is in IO, the program is forced to re-evaluate the expression, so the updated pointer data is used instead.

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

Sidebar

Related Questions

The question says it all, I am trying to use an external library in
The question says it all. I was taking a look at Can a recursive
The question more or less says it all. Given the following record structure: type
Question says it all ... is there a way to generate more than 50
The question says it all. Can I compile C# to native code and boot
The title basically says it all. I'm aware this can't be done using traditional
Question says it all. I'm just having trouble figuring this out. What i'm trying
Question says it all. I tried browsing through the latest tag but could not
Question says it all. Some quick code examples of usage would be nice.. thanks!
Question says it all. Does anyone know if the following... size_t div(size_t value) {

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.