next up previous contents index
Next: XDR Methods Up: XDR Functions Previous: Strings

Arrays

There are two functions in the XDR library for handling arrays: xdr_array for arbitrary and xdr_vector for fixed length arrays. You can use xdr_vector (see the g_xdr_vec3 function above) but you should not use xdr_array because it uses malloc/free (like the XDR string function, again consider section 5.7.7).

The GRAPE function for arrays provides much more functionality than the original XDR functions, its syntax is

bool_t g_xdr_array(XDR *xdrp, void **array, int number_of_elements,
                   int max_number_of_elements, char *typename)
Like for the other functions a pointer to the array pointer is the second parameter, the third and fourth parameter are the number of elements and the maximal number of elements in the array.

The last parameter is a string which contains the name of the type of the array, e.g. "int" for an integer array or "unsigned long" for an unsigned long array. The basic C and GRAPE types, "char *" for string arrays and "INSTANCE *" for instance arrays are predefined, you can add you own types with the function

int g_xdr_add_type(char *name, int size, xdrproc_t xdrfunc)
which has the type's name ("type"), it's size (sizeof(type)) and the XDR function which handles variables of this type (xdr_type or g_xdr_type) as parameters.

The g_xdr_array function handles arrays as instances of a special class Array (which is for internal use only), it is responsible for handling

Handling references is the most important feature. For example if you work with time-dependent data on a fixed triangulation (i.e. many Triang2d or subclass instances with the same vertex/neighbour indices) you can use the same vertex and neighbour arrays for all instances (though this might be dangerous, e. g. when data is shared between instances). If you write these instances g_xdr_array is called several times on the same arrays (see section 5.7.6 for the Triang2d "xdr" method). On the first call the array itself is written, on all subsequent calls only a reference is written. When the data is read back in and g_xdr_array finds a reference it just returns the pointer to the already read array -- the real array is always stored ahead of any reference to it.

If on reading the maximal number of elements of the parameter array differs from the maximal number of elements read the given array will be freed and new memory allocated. Therefore if the given array parameter is not NULL (the pointer, not the pointer pointer!) it should have been allocated with mem_alloc or another memory function like int_alloc which internally uses mem_alloc.


next up previous contents index
Next: XDR Methods Up: XDR Functions Previous: Strings

SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.