fork download
  1. import Data.Char
  2. import Foreign
  3. -- import System.IO
  4.  
  5. f589_1 :: IO ()
  6. f589_1 = do
  7. a <- addr (12 :: Int)
  8. p_a <- addr a
  9. b <- addr (3.45 :: Float)
  10. p_b <- addr b
  11. prn "a" a
  12. prn "p_a" p_a
  13. prn "b" b
  14. prn "p_b" p_b
  15. free a >> free p_a >> free b >> free p_b
  16. where
  17. addr a = malloc >>= \p -> poke p a >> return p
  18. prn x i = do
  19. v <- peek i
  20. putStrLn $ x ++ "のアドレスは、" ++ show i
  21. putStrLn $ x ++ "の値は、" ++ show v
  22.  
  23. f589_2 :: IO ()
  24. f589_2 = do
  25. let c = 'A'
  26. p <- malloc
  27. poke p c
  28. peek p >>= putStrLn . (:[])
  29. peek p >>= poke p . chr . (+2) . ord
  30. peek p >>= putStrLn . (:[])
  31. free p
  32.  
  33. main :: IO ()
  34. main = -- hSetEncoding stdout utf8 >>
  35. f589_1 >> f589_2
  36.  
Success #stdin #stdout 0s 6324KB
stdin
Standard input is empty
stdout
aのアドレスは、0x09090758
aの値は、12
p_aのアドレスは、0x09090768
p_aの値は、0x09090758
bのアドレスは、0x09090778
bの値は、3.45
p_bのアドレスは、0x09090788
p_bの値は、0x09090778
A
C