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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:44:57+00:00 2026-05-28T07:44:57+00:00

I have a C function calling into Haskell. The C function passes a StablePtr

  • 0

I have a C function calling into Haskell. The C function passes a StablePtr of a Haskell datatype and the Haskell code needs to change some of its values. What’s an efficient way to do this? For example, consider the following

foreign export ccall editChar :: StablePtr MyObject -> CInt -> CChar -> IO ()

data MyObject = Obj String

editChar :: StablePtr MyObject -> CInt -> CChar -> IO ()
editChar cMyObjectPtr index newChar = do
  -- Code goes here

How would editChar be implemented to be as efficient and Haskelly as possible in order to set the Char at index to newChar? Eventually the object being mutated will be rather large memory-wise and have many subcomponents so returning a new object as the result of editChar is out of the question.

  • 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-28T07:44:58+00:00Added an answer on May 28, 2026 at 7:44 am

    You can’t mutate the Chars inside MyObject. Indeed, you can’t even mutate contents of a StablePtr at all. All you can do is dereference the StablePtr to get your MyObject back.

    If you define

    newtype MyObject = Obj (IORef String)
    

    (or MVar instead of IORef)

    then you’ll be able to mutate it through the usual methods.

    For what it’s worth, if memory consumption is what you’re concerned about, String isn’t suitable at all; it uses 5 machine words per character. However, the overhead of “returning a new value” might not be as high as you think: thanks to sharing, prepending a Char to a String doesn’t copy the entire string, but instead just reuses the reference to the “old” one. With tree structures such as Seq, these advantages carry over to replacing elements, too.

    However, if you’re doing a lot of mutation, then you might want to consider a mutable vector.

    Of course, if the String is just an example (as your last paragraph implies), then this advice doesn’t necessarily apply. But if you have

    data Huge = Huge { giganticPart :: Gigantic, smallPart :: Int }
    

    then myHuge { smallPart = 42 } isn’t going to copy the entire Gigantic, and if Gigantic is an appropriate tree structure, you’ll be able to do your modifications to it without copying the whole thing. This is the core idea of purely functional, persistent data structures, and one of the most important advantages of Haskell.

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

Sidebar

Related Questions

So I have a TimerTask task calling a function onTimerComplete() in its run() onTimerComplete()
I have a function that is recursively calling itself, and i want to detect
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have a number of iframes calling a function in my main page. Is
I have a javascript function for checking errors which I am calling on OnClicentClick
I have the view function in django that written like a dispatcher calling other
I have got a problem with calling a global function, which takes a pointer
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have some function (pasted below little piece of function), which I used as
My program have several worker threads that calling a function in a dynamically loaded

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.