[plt-scheme] FFI and pointer manipulations

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Feb 10 17:44:38 EST 2007

On Feb  9, Jens Axel Søgaard wrote:
> Hi all,
> 
> With the FFi how do I add an integer to a c-pointer?
> 
> More specifically I am interested in getting a pointer
> into the middle of a byte-string.

It's possible to do arbitrary conversions using the foreign interface,
just like you do in C -- except that you have to put something in a
malloced space, and reference it as something else.  No "typecast"
function yet.  (Partly because foreign types are not completely first
class, which is yet to be hacked.)  But:

> I couldn't find anything on pointer arithmetic besides
> ptr-equal? in the FFI manual.

This is intentional.  Having a pointer to the middle of a malloced
object will not work right with 3m.  In fact, I think that it may
cause damage even if you use it in some harmless way.  One way to deal
with this would be to provide a kind of pointer-with-offset, and
another would be to add a new kind of "dangerous pointer" type.  Both
will not be easy to understand and to use, and nothing is implemented
yet.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.