[Fwd: Re: [clean-list] htoclean examples]

Arjen van Weelden arjenw@cs.kun.nl
Thu, 02 Oct 2003 12:58:58 +0200


Hi,

In order to use HtoClean to use C code in Clean, you really don't need
to know what the -2 and -1 are for, but here is my attempt to explain it.

Clean stores (unboxed) arrays in memory something like the following C
structs:
struct RealArray { unsigned int size; unsigned  int elementDescriptor;
double[size] data };
struct IntArray { unsigned int size; unsigned int elementDescriptor;
int[size] data };
struct CharArray { unsigned int size; char[size] data };

In the C code, you get a pointer to the data field of the struct.
For Int and Real arrays, the size of the array is therefore stored in
memory 2 integers before the address the pointer points to (1 for Char
arrays or Strings). This explains the (unsigned int*), [-2], and [-1].
The elementDescriptor is not used in C, it is just the way the Clean
stores arrays in memory.

hope this removes some of the mystery,
	Arjen

Hugo Rufino wrote:

> Hi, folks,
>  
> i'm studing how to call C functions from Clean. I already know htoclean 
> and i had some problems to understand how the example "example_array1" 
> works. Being more specific why the index [-2] and [-1] in the following 
> code?
>  
> #define CleanIntArraySize(clean_int_array) (((unsigned int 
> *)(clean_int_array))[-2])
> #define CleanRealArraySize(clean_real_array) (((unsigned int 
> *)(clean_real_array))[-2])
> #define CleanCharArraySize(clean_char_array) (((unsigned int 
> *)(clean_char_array))[-1])
> There is a new version of htoclean?
> Anybody is studing how to call C function from Clean too?
>  
> Thanks in advance,
>  
> Hugo Leonardo
>  
>  
>  
> 
> 
> ------------------------------------------------------------------------
> *Yahoo! Mail <http://br.rd.yahoo.com/s/c/m/?http://mail.yahoo.com.br>* - 
> o melhor webmail do Brasil. Saiba mais! 
> <http://br.rd.yahoo.com/s/c/m/?http://br.yahoo.com/info/mail.html>