
Hi, I'm trying to learn about Haskell's FFI (running 6.0.1 on linux) and see the following weird behavior with ghci: Prelude> :module Data.Word Foreign.Storable Foreign.Ptr Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf nullPtr 4 Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf (nullPtr :: Ptr Word64) 4 Shouldn't "sizeOf nullPtr" return an error? And sizeOf a Ptr Word64 should be 8 I think. Also this program prints "4", when it seems it should print "8": module Main where import Data.Word import Foreign.Storable import Foreign.Ptr import Foreign.StablePtr x :: Word64 x = 5 main = do x_sptr <- newStablePtr 5 putStrLn $ show $ sizeOf x_sptr These are artificial examples, but I originally noticed this when trying to decode a C structure. Is this a bug in ghc or am I misunderstanding what sizeOf is supposed to do? -- Ben Escoto

On Sat, Nov 08, 2003 at 06:32:35PM -0800, Ben Escoto wrote:
Hi, I'm trying to learn about Haskell's FFI (running 6.0.1 on linux) and see the following weird behavior with ghci: Prelude> :module Data.Word Foreign.Storable Foreign.Ptr Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf nullPtr 4 Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf (nullPtr :: Ptr Word64) 4 Shouldn't "sizeOf nullPtr" return an error? And sizeOf a Ptr Word64 should be 8 I think. Also this program prints "4", when it seems it should print "8":
module Main where import Data.Word import Foreign.Storable import Foreign.Ptr import Foreign.StablePtr
x :: Word64 x = 5
main = do x_sptr <- newStablePtr 5 putStrLn $ show $ sizeOf x_sptr
These are artificial examples, but I originally noticed this when trying to decode a C structure. Is this a bug in ghc or am I misunderstanding what sizeOf is supposed to do?
sizeof returns the size of its argument, since the argument you passed is a pointer, so it properly returns the size of a pointer, 32 bits or 4 bytes on your architecture. to find the size of the actual datatype do something like sizeOf (undefined :: Word16) => 2 sizeOf (undefined :: Word64) => 8 sizeOf never evaluates its arguments so it is okay to pass anything to it of the type you are interested in. John -- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@foo.net ---------------------------------------------------------------------------

sön 2003-11-09 klockan 03.32 skrev Ben Escoto:
Hi, I'm trying to learn about Haskell's FFI (running 6.0.1 on linux) and see the following weird behavior with ghci:
Prelude> :module Data.Word Foreign.Storable Foreign.Ptr Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf nullPtr 4 Prelude Foreign.Ptr Foreign.Storable Data.Word> sizeOf (nullPtr :: Ptr Word64) 4
Shouldn't "sizeOf nullPtr" return an error? And sizeOf a Ptr Word64 should be 8 I think.
The size of a pointer is not the same as the size of what it points at. On ia32 (x86) architectures, the size of a pointer is always 4 bytes (32 bits). Regards, Martin

Hi, I'm trying to learn about Haskell's FFI (running 6.0.1 on linux) and see the following weird behavior with ghci:
You're seeing the right behaviour.
Shouldn't "sizeOf nullPtr" return an error? And sizeOf a Ptr Word64 should be 8 I think.
The size of a Word64 value is 8 but the size of a pointer is always the same no matter what the pointer points to. Since most machines have 32-bit addresses, the size of a pointer will be 4 on most machines. A null pointer is just another machine address and has to be the same size as any other machine address so, again, it has to be the same size.
Also this program prints "4", when it seems it should print "8":
module Main where import Data.Word import Foreign.Storable import Foreign.Ptr import Foreign.StablePtr
x :: Word64 x = 5
main = do x_sptr <- newStablePtr 5 putStrLn $ show $ sizeOf x_sptr
This is a stable pointer to an Int. Again, the type and, in particular, the size of the thing pointed to is irrelevant. I can't be bothered looking it up but the size returned is either the size of a machine address (void* in C) or the size of int in C. Either way it is 4 on most machines. It looks like you intended to create a stable pointer to x which is a Word64. This would not have affected the result. Indeed, you could have created a stable pointer to a complex Haskell data structure like a binary tree or a function (i.e., something the FFI doesn't directly support) and the size would still be the same because all you are doing is passing a pointer to the object. -- Alastair Reid www.haskell-consulting.com

On Sun, 9 Nov 2003 08:51:11 +0000
Alastair Reid
Hi, I'm trying to learn about Haskell's FFI (running 6.0.1 on linux) and see the following weird behavior with ghci:
You're seeing the right behaviour. ... The size of a Word64 value is 8 but the size of a pointer is always the same no matter what the pointer points to. Since most machines have 32-bit addresses, the size of a pointer will be 4 on most machines.
Yes, I was confused and thought it would give me the size of what it pointed instead of its size. Thanks for all replies. -- Ben Escoto
participants (4)
-
Alastair Reid
-
Ben Escoto
-
John Meacham
-
Martin Norbäck