next up previous contents index
Next: Using GMP Up: User Guide Previous: Input/Output   Contents   Index

Compatibility with C types

Several of libaldor's types are compatible with their C counterparts and can be passed as arguments to C functions. Table 1 lists those types that can safely be exchanged with C functions.

Table 1: Compatibility between libaldor and C types


Since ALDOR does not provide a type that is guaranteed to be compatible with the C int type on all platforms, in order to use C functions having int in their parameters, you must first write a C wrapper that communicates only through the type long. Note that even though some type T can be compatible with a C type TC, it is not always the case that PrimitiveArray T is compatible with TC*, for example PrimitiveArray Character is not compatible with char* (nor is it compatible with String in libaldor). It is always the case however that PackedPrimitiveArray T is compatible with TC*. Note that DoubleFloat is not compatible with the C type double, but with DFlo$Machine instead (there are coercions between DFlo$Machine and DoubleFloat). On the other hand, PackedPrimitiveArray DoubleFloat is compatible with double*. When exchanging objects of type PrimitiveArray, PrimitiveMemoryBlock or String with C functions, always use the array, pointer, string and pointer functions. While those have no effect and no cost in the release version, they are necessary when linking with the debug version in order to use the bound-checking features. When doing this, you must use Pointer in the prototypes for the C functions rather than PrimitiveArray, PrimitiveMemoryBlock or String. For example, a function that uses the C-function unlink to remove a file could be written in the following way:
#include "aldor"
remove(filename:String):() == {
	import { unlink: Pointer -> MachineInteger } from Foreign C;
	unlink pointer filename;
}
While a function that uses the C-function mktemp to get a temporary file name could be written in the following way:
#include "aldor"

temporaryName():() == {
	import { mktemp: Pointer -> Pointer } from Foreign C;
	string mktemp pointer("/tmp/XXXXXX");
}
Note the use of string even though the buffer was allocated within libaldor and not within mktemp.


next up previous contents index
Next: Using GMP Up: User Guide Previous: Input/Output   Contents   Index
Manuel Bronstein 2004-06-28