[gambit-list] Re: C pointers

Eduardo Cavazos wayo.cavazos at gmail.com
Fri Dec 31 17:30:46 EST 2004


Hello Thomas,

Below you provide the example function 'inc_int' and ask what you
should pass to it. You should pass it a pointer to an int of course.
:-) And where do you get such an object? You can make one with another
c-lambda based function. For example:

(define make-int-box
  (c-lambda () int* "___result = malloc(sizeof( int ));"))

So now you can do:

(define i (make-int-box))
(inc_int i)

There is a problem... the int box doesn't contain anything to
increment. So we should have a way to set the value inside the box. We
also need a way to access the contents of the box. These functions do
these:

(define set-int-box
  (c-lambda (int* int) void "*___arg1 = ___arg2;"))

(define val-int-box
  (c-lambda (int*) int "___result = *___arg1;"))

That should get you started.

The atomic code I announced a few days ago contains a macro called
'c-box' that works like this:

(c-box int)

Now you can do this:

;; Make a new int box
(define i (new-int-box))
;; Set the value of the box:
[i val: 0]
;; Call your inc_int to increment the value in the box
(inc_int [i box:])
;; Print the new value:
[i val:]

Happy Hacking

Ed Cavazos

On 31 Dec 2004 17:39:20 +0100, Thomas Hafner <thomas at hafner.nl.eu.org> wrote:
> Some given C functions receive pointers and manipulate the memory
> where the pointers are pointing to. How should my c-lamda expression
> look like to mach that requirement?
>
> Here's a simple example (not real, but to show my problem). If the
> given C Funktion looks like that:
>   void inc_int(int *pI)
>   {
>     (*pI)++;
>   }
>
> Is it then Ok to use it from Gambit 4.0 beta 11 like follows?
>   (define inc-int
>     (c-lambda ((pointer int))
>               void
>               "inc_int"))
>
> If yes: what can I pass to inc-int? How to express the Scheme
> equivalent to the C pointer?
>
> Otherwise: how should my Scheme procedure definition look like? Of
> course the example above is a very simple one. A good solution should
> also work with more complicated C functions, e.g. of type
> int () (int *, char **).
>
> Regards
>   Thomas Hafner
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> http://mailman.iro.umontreal.ca/mailman/listinfo/gambit-list
>


More information about the Gambit-list mailing list