question 1:
Is it possible to write gambit code to produce the equiv of: int foo[200]; ?
the current best I have in mind is: (let ((foo (malloc (* 200 8)))) ... )
question 2: suppose I have:
(c-define-type bar "bar")
is there a way to create equiv of: bar foo[200]; ?
again, current best I have in mind is: (let ((foo (malloc (* 200 sizeof_bar)))) ... )
question 3:
in question 1, is there a way to access foo[i] ?
question 4:
in question 2, is there a way to access foo[i] ?
thanks!
Afficher les réponses par date
On 14-Feb-09, at 11:58 PM, lowly coder wrote:
question 1:
Is it possible to write gambit code to produce the equiv of: int foo[200]; ?
the current best I have in mind is: (let ((foo (malloc (* 200 8)))) ... )
question 2: suppose I have:
(c-define-type bar "bar")
is there a way to create equiv of: bar foo[200]; ?
again, current best I have in mind is: (let ((foo (malloc (* 200 sizeof_bar)))) ... )
question 3:
in question 1, is there a way to access foo[i] ?
question 4:
in question 2, is there a way to access foo[i] ?
thanks!
Here's how I would do it:
(c-declare #<<end-of-c-declare
#include <stdlib.h>
int *malloc_int_array( int len ) { return ___CAST( int*, malloc( len * sizeof(int) ) ); }
/*
for pure but slow version:
int int_array_ref( int *array, int i ) { return array[i]; }
void int_array_set( int *array, int i, int val ) { array[i] = val; }
*/
end-of-c-declare )
(c-define-type int* (pointer int))
(define malloc-int-array (c-lambda (int) int* "malloc_int_array"))
; pure but slow version: ; ;(define int-array-ref ; (c-lambda (int* int) int "int_array_ref")) ; ;(define int-array-set! ; (c-lambda (int* int int) void "int_array_set"))
; faster version:
(define int-array-ref (c-lambda (int* int) int "___result = ___arg1[___arg2];"))
(define int-array-set! (c-lambda (int* int int) void "___arg1[___arg2] = ___arg3;"))
;; test it:
(define a (malloc-int-array 5))
(int-array-set! a 1 111) (int-array-set! a 2 222) (pp (+ (int-array-ref a 1) (int-array-ref a 2))) ;; 333