Hi,
I guess I'll answer your original question too, in case you ever come
across another C library that needs a char** or an int*.
To create a pointer to an int allocated in the Factor heap, use the
<int> word:
FUNCTION: void foo ( int* bar ) ;
123 <int> foo
A char** is just a pointer to an array of pointers of char*. You can
do something like this:
REQUIRES: libs/alien ; ! for the >void*-array utility word...
{ "hello" "world" } [ malloc-char-string ] map >void*-array
Some notes about this:
1) <int> and >void*-array allocate their structures in the Factor
heap. So the C function must not retain pointers to them in global
variables, because they might be moved by the Factor GC after the
function returns (the GC won't run while the C function is executing,
though). For more permanent data, you can use the malloc words (see
the alien documentation).
2) We call malloc-char-string, which allocates a pointer in fixed
storage, because you cannot store a pointer to an object in the
Factor heap in a C array. So in the above example, you'd have to free
the data later.
3) Passing data between Factor and C can be very tricky, almost as
tricky as programming in C -- for example, you have to manage your
own memory sometimes, just as if you were calling the library from C.
You could do worse than read all of
http://www.factorcode.org/responder/help/show-help?topic=c%2ddata
before embarking on writing C library bindings. Generally once a
binding is written, we try to abstract away the low level detail by
wrapping it in a layer of words which map library features to Factor
idioms.
Also note the syntax for FUNCTION: -- the correct way to write what
you did below is the following:
FUNCTION: void glutInit( int* argcp, char** argv ) ;
Slava
On 23-May-07, at 12:04 AM, Jeff Ervin wrote:
> I'm using the Factor 0.89 OpenGL library and wish to call glut
> functions
> from Factor in order run redbook examples. I created a factor file
> with
> the following code for a start:
>
> IN: glut
> USING: kernel alien ;
>
> LIBRARY: glut
>
> FUNCTION: void glutInit( int *argcp, char **argv ) ;
>
> I then loaded the file so the binding word is compiled. How do I
> create
> the argcp and argv parameters to pass to the function? The
> documentation
> states
> that glutInit may modify these parameters by extracting glut specific
> command line flags. I am not sure how to create the int* char**
> parameters in Factor.
>
>
>
> ----------------------------------------------------------------------
> ---
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk