我正在研究一些编程语言如何为结构化数据分配内存(在这种情况下我正在研究数组)。
我正在第3节创建如here所示的数组。
import Data.Array.IO
arr <- newArray (1,10) 37 :: IO (IOArray Int Int) --Sets default to 37
我正在尝试做的是打印每个元素的内存地址,如下所示:
Array Start: <dec addr> | <hex addr> --Shows where the array itself is
Array 1: <dec addr> | <hex addr> --Memory address of the first element
Array 2: <dec addr> | <hex addr| --Memory address of the second element
我遇到的问题是我不知道如何获取Haskell中元素的内存地址值。
是否存在类似于Python id(object)
或Ruby object.object_id
的函数?
答案 0 :(得分:10)
您可以使用我从ghc-heap-view
包借来的以下代码段(它还包含使用foreign import prim
的替代解决方案):
{-# LANGUAGE MagicHash, BangPatterns #-}
import GHC.Exts
-- A datatype that has the same layout as Word and so can be casted to it.
data Ptr' a = Ptr' a
-- Any is a type to which any type can be safely unsafeCoerced to.
aToWord# :: Any -> Word#
aToWord# a = let !mb = Ptr' a in case unsafeCoerce# mb :: Word of W# addr -> addr
unsafeAddr :: a -> Int
unsafeAddr a = I# (word2Int# (aToWord# (unsafeCoerce# a)))
首先将a
包装在Ptr'
构造函数中,然后将Ptr' a
转换为Word
。由于a
字段表示为指针,因此生成的单词现在包含对象的地址。通常的警告适用:这是不安全的,GHC特定的,打破参考透明度等。
测试:
main :: IO ()
main = do
arr <- newListArray (1,10) [1,2..] :: IO (IOArray Int Int)
a1 <- readArray arr 1
a2 <- readArray arr 2
a1' <- readArray arr 1
putStrLn $ "a1 : " ++ (show . unsafeAddr $! a1)
putStrLn $ "a1 : " ++ (show . unsafeAddr $! a1)
putStrLn $ "a2 : " ++ (show . unsafeAddr $! a2)
putStrLn $ "a2 : " ++ (show . unsafeAddr $! a2)
putStrLn $ "a1': " ++ (show . unsafeAddr $! a1')
输出:
a1 : 16785657
a1 : 16785657
a2 : 16785709
a2 : 16785709
a1': 16785657
请注意,您应将unsafeAddr
与$!
一起使用,否则您将获得评估为a
而不是a
的thunk地址对象本身:
let a = 1
b = 2
c = a + b
putStrLn $ "c: " ++ (show . unsafeAddr $ c)
putStrLn $ "c: " ++ (show . unsafeAddr $! c)
putStrLn $ "c: " ++ (show . unsafeAddr $! c)
输出:
c: 9465024
c: 9467001
c: 9467001