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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:21:58+00:00 2026-05-13T23:21:58+00:00

I wish to manipulate data on a very low level. Therefore I have a

  • 0

I wish to manipulate data on a very low level.

Therefore I have a function that receives a virtual memory address as an integer and “does stuff” with this memory address. I interfaced this function from C, so it has the type (CUInt -> a).
The memory I want to link is a Word8 in a file. Sadly, I have no idea how to access the pointer value to that Word8.

To be clear, I do not need the value of the Word8, i need the value to the virtual memory address, which is the value of the pointer to it.

  • 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-13T23:21:59+00:00Added an answer on May 13, 2026 at 11:21 pm

    For the sake of a simple example, say you want to add an offset to the pointer.

    Front matter:

    module Main where
    import Control.Monad (forM_)
    import Data.Char (chr)
    import Data.Word (Word8)
    import Foreign.ForeignPtr (ForeignPtr, withForeignPtr)
    import Foreign.Ptr (Ptr, plusPtr)
    import Foreign.Storable (peek)
    import System.IO.MMap (Mode(ReadOnly), mmapFileForeignPtr)
    

    Yes, you wrote that you don’t want the value of the Word8, but I’ve retrieved it with peek to demonstrate that the pointer is valid. You might be tempted to return the Ptr from inside withForeignPtr, but the documentation warns against that:

    Note that it is not safe to return the pointer from the action and use it after the action completes. All uses of the pointer should be inside the withForeignPtr bracket. The reason for this unsafeness is the same as for unsafeForeignPtrToPtr below: the finalizer may run earlier than expected, because the compiler can only track usage of the ForeignPtr object, not a Ptr object made from it.

    The code is straightforward:

    doStuff :: ForeignPtr Word8 -> Int -> IO ()
    doStuff fp i =
      withForeignPtr fp $ \p -> do
        let addr = p `plusPtr` i
        val <- peek addr :: IO Word8
        print (addr, val, chr $ fromIntegral val)
    

    To approximate “a Word8 in a File” from your question, the main program memory-maps a file and uses that buffer to do stuff with memory addresses.

    main :: IO ()
    main = do
      (p,offset,size) <- mmapFileForeignPtr path mode range
      forM_ [0 .. size-1] $ \i -> do
        doStuff p (offset + i)
      where
        path  = "/tmp/input.dat"
        mode  = ReadOnly
        range = Nothing
     -- range = Just (4,3)
    

    Output:

    (0x00007f1b40edd000,71,'G')
    (0x00007f1b40edd001,117,'u')
    (0x00007f1b40edd002,116,'t')
    (0x00007f1b40edd003,101,'e')
    (0x00007f1b40edd004,110,'n')
    (0x00007f1b40edd005,32,' ')
    (0x00007f1b40edd006,77,'M')
    (0x00007f1b40edd007,111,'o')
    (0x00007f1b40edd008,114,'r')
    (0x00007f1b40edd009,103,'g')
    (0x00007f1b40edd00a,101,'e')
    (0x00007f1b40edd00b,110,'n')
    (0x00007f1b40edd00c,33,'!')
    (0x00007f1b40edd00d,10,'\n')
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to test a function that will generate lorem ipsum text, but it
I have a .rtf file that contains nothing but an integer, say 15. I
I wish Subversion had a better way of moving tags. The only way that
I wish to implement my software on a shareware basis, so that the user
I have a SQL Server 2005 database used by several applications. Some users wish
HI, I have a device that exposes a telnet interface which you can log
I have a web-app that will reside on a production server where I want
i wish to send some data as array from my iphone app to server
I have a static array: myArray: array [0..15] of byte; I wish to pass
So I wish to work with #hash links.. As I cannot manipulate the history

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.