converting between a Ptr a and an Integer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

converting between a Ptr a and an Integer

keydana@gmx.de
Hi,

is there any way to convert between a Ptr a and an Integer?
(I would like to attach to shared memory and dereference pointers to certain memory addresses, as you would do in C.)

If there is no way at all, I think I could still get along by defining c_shmat as

c_shmat :: CInt ->  CInt -> CInt -> IO (Ptr a)

and perform calculations using plusPtr,

but then how do I check if I got back the correct address, and not (void *) -1, which is what shmat returns on error?

Any help very much appreciated!

Thanks
Sigrid
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: converting between a Ptr a and an Integer

Sylvain HENRY
Hi,

You can use:
ptrToIntPtr :: Ptr a -> IntPtr
intPtrToPtr :: IntPtr -> Ptr a

Cheers
Sylvain


Le 07/12/2013 18:58, [hidden email] a écrit :

> Hi,
>
> is there any way to convert between a Ptr a and an Integer?
> (I would like to attach to shared memory and dereference pointers to certain memory addresses, as you would do in C.)
>
> If there is no way at all, I think I could still get along by defining c_shmat as
>
> c_shmat :: CInt ->  CInt -> CInt -> IO (Ptr a)
>
> and perform calculations using plusPtr,
>
> but then how do I check if I got back the correct address, and not (void *) -1, which is what shmat returns on error?
>
> Any help very much appreciated!
>
> Thanks
> Sigrid
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: converting between a Ptr a and an Integer

Carter Schonwald
There is a module in base that let's you do pointer arith in Haskell. I forget where it is.  But it's there.  

No need to cast your pointer to an int and back. 

On Saturday, December 7, 2013, Sylvain HENRY wrote:
Hi,

You can use:
ptrToIntPtr :: Ptr a -> IntPtr
intPtrToPtr :: IntPtr -> Ptr a

Cheers
Sylvain


Le 07/12/2013 18:58, [hidden email] a écrit :
Hi,

is there any way to convert between a Ptr a and an Integer?
(I would like to attach to shared memory and dereference pointers to certain memory addresses, as you would do in C.)

If there is no way at all, I think I could still get along by defining c_shmat as

c_shmat :: CInt ->  CInt -> CInt -> IO (Ptr a)

and perform calculations using plusPtr,

but then how do I check if I got back the correct address, and not (void *) -1, which is what shmat returns on error?

Any help very much appreciated!

Thanks
Sigrid
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: converting between a Ptr a and an Integer

keydana@gmx.de
In reply to this post by keydana@gmx.de
Hi,

thanks for your answers, I've found a way (or rather, was pointed to it ;-) ) in  the meantime, using something like

  nullPtr  `plusPtr` <desired address>

Thanks all the same,
Sigrid

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe