next up previous contents index
Next: 3.5 Passing Data into Up: 3. Embedding XSB in Previous: 3.3.5 Thread Management from   Contents   Index


3.4 The Variable-length String Data Type

XSB uses variable-length strings to communicate with certain C subroutines when the size of the output that needs to be passed from the Prolog side to the C side is not known. Variable-length strings adjust themselves depending on the size of the data they must hold and are ideal for this situation. For instance, as we have seem the two subroutines xsb_query_string_string(query,buff,sep) and xsb_next_string(buff,sep) use the variable string data type, VarString, for their second argument. To use this data type, make sure that

#include "cinterf.h"
appears at the top of the program file. Variables of the VarString type are declared using a macro that must appear in the declaration section of the program:
XSB_StrDefine(buf);
There is one important consideration concerning VarString with the automatic storage class: they must be destroyed on exit (see XSB_StrDestroy, below) from the procedure that defines them, or else there will be a memory leak. It is not necessary to destroy static VarString's.

The public attributes of the type are int length and char *string. Thus, buf.string represents the actual contents of the buffer and buf.length is the length of that data. Although the length and the contents of a VarString string is readily accessible, the user must not modify these items directly. Instead, he should use the macros provided for that purpose:


next up previous contents index
Next: 3.5 Passing Data into Up: 3. Embedding XSB in Previous: 3.3.5 Thread Management from   Contents   Index
Terrance Swift 2007-10-06