Rev 61433 | Rev 65017 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Foreign.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2012 R Core Team% Distributed under GPL 2 or later\name{Foreign}\alias{Foreign}\alias{.C}\alias{.Fortran}\title{Foreign Function Interface}\description{Functions to make calls to compiled code that has been loaded into \R.}\usage{.C(.NAME, \dots, NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING).Fortran(.NAME, \dots, NAOK = FALSE, DUP = TRUE, PACKAGE, ENCODING)}\arguments{\item{.NAME}{a character string giving the name of a C function orFortran subroutine, or an object of class\code{"\link{NativeSymbolInfo}"}, \code{"\link{RegisteredNativeSymbol}"}or \code{"\link{NativeSymbol}"} referring to such a name.}\item{\dots}{arguments to be passed to the foreign function. Up to 65.}\item{NAOK}{if \code{TRUE} then any \code{\link{NA}} or\code{\link{NaN}} or \code{\link{Inf}} values in the arguments arepassed on to the foreign function. If \code{FALSE}, the presence of\code{NA} or \code{NaN} or \code{Inf} values is regarded as an error.}\item{DUP}{if \code{TRUE} then arguments are duplicated beforetheir address is passed to C or Fortran.}\item{PACKAGE}{if supplied, confine the search for a character string\code{.NAME} to the DLL given by this argument (plus theconventional extension, \file{.so}, \file{.dll}, \dots).This is intended to add safety for packages, which can ensure byusing this argument that no other package can override their externalsymbols, and also speeds up the search (see \sQuote{Note}).}\item{ENCODING}{For back-compatibility, accepted but ignored.}}\details{These functions can be used to make calls to compiled C and Fortran 77code. Later interfaces are \code{\link{.Call}} and\code{\link{.External}} which are more flexible and have betterperformance.These functions are both \link{primitive}, and \code{.NAME} is alwaysmatched to the first argument supplied (which should not be named).The other named arguments follow \code{\dots} and so cannot beabbreviated. For clarity, should avoid using names in the argumentspassed to \code{\dots} that match or partially match \code{.NAME}.}\value{A list similar to the \code{\dots} list of arguments passed in(including any names given to the arguments), but reflecting anychanges made by the C or Fortran code.}\section{Argument types}{The mapping of the types of \R arguments to C or Fortran arguments is\tabular{lll}{\R \tab C \tab Fortran\crinteger \tab int * \tab integer\crnumeric \tab double * \tab double precision\cr-- or -- \tab float * \tab real\crcomplex \tab Rcomplex * \tab double complex\crlogical \tab int * \tab integer \crcharacter \tab char ** \tab [see below]\crraw \tab unsigned char * \tab not allowed\crlist \tab SEXP *\tab not allowed\crother \tab SEXP\tab not allowed\cr}Numeric vectors in \R will be passed as type \code{double *} to C(and as \code{double precision} to Fortran) unless the argument hasattribute \code{Csingle} set to \code{TRUE} (use\code{\link{as.single}} or \code{\link{single}}). This mechanism isonly intended to be used to facilitate the interfacing of existing Cand Fortran code.The C type \code{Rcomplex} is defined in \file{Complex.h} as a\code{typedef struct {double r; double i;}}. It may or may not beequivalent to the C99 \code{double complex} type, depending on thecompiler used.Logical values are sent as \code{0} (\code{FALSE}), \code{1}(\code{TRUE}) or \code{INT_MIN = -2147483648} (\code{NA}, but only if\code{NAOK = TRUE}), and the compiled code should return one of thesethree values: however non-zero values other than \code{INT_MIN} aremapped to \code{TRUE}.\emph{Note:} The C types corresponding to \code{integer} and\code{logical} are \code{int}, not \code{long} as in S. Thisdifference matters on most 64-bit platforms, where \code{int} is32-bit and \code{long} is 64-bit (but not on 64-bit Windows).\emph{Note:} The Fortran type corresponding to \code{logical} is\code{integer}, not \code{logical}: the difference matters on someFortran compilers.Missing (\code{NA}) string values are passed to \code{.C} as the string"NA". As the C \code{char} type can represent all possible bit patternsthere appears to be no way to distinguish missing strings from thestring \code{"NA"}. If this distinction is important use \code{\link{.Call}}.\code{.Fortran} passes the first (only) character string of a charactervector is passed as a C character array to Fortran: that may be usableas \code{character*255} if its true length is passed separately. Onlyup to 255 characters of the string are passed back. (How well thisworks, and even if it works at all, depends on the C and Fortrancompilers and the platform.)Lists, functions are other \R objects can (for historical reasons) bepassed to \code{.C}, but the \code{\link{.Call}} interface is muchpreferred. All inputs apart from atomic vectors should be regarded asread-only, and all apart from vectors (including lists), functions andenvironments are now deprecated.}%%-- This note by Thomas Lumley, (minimally edited by MM,%% edited by BDR for 1.2.0's non-moving garbage collector):\section{Warning}{\emph{\code{DUP = FALSE} is dangerous.}People concerned about memory usage are strongly recommended to usethe \code{\link{.Call}} interface instead of these interfaces.If you pass a local variable to \code{.C}/\code{.Fortran} with\code{DUP = FALSE}, your compiled code can alter the local variable andnot just the copy in the return list. Worse, if you pass a localvariable that is a formal parameter of the calling function, you maybe able to change not only the local variable but the variable onelevel up. This will be very hard to trace.With \code{DUP = FALSE}, character vectors cannot be used, and singleprecision values will not be returned.It is safe and useful to set \code{DUP = FALSE} if you do not change anyof the variables that might be affected, e.g.,\code{.C("Cfunction", input = x, output = numeric(10))}.In this case the \code{output} variable did not exist before the callso it cannot cause trouble (but as from \R 2.15.1 it is not copiedeven with \code{DUP = TRUE}). If the \code{input} variable is notchanged in the C code of \code{Cfunction} you are safe.}\section{Fortran symbol names}{All Fortran compilers known to be usable to compile \R map symbol namesto lower case, and so does \code{.Fortran}.Symbol names containing underscores are not valid Fortran 77 (althoughthey are valid in Fortran 9x). Many Fortran 77 compilers will allowthem but may translate them in a different way to names not containingunderscores. Such names will often work with \code{.Fortran} (sincehow they are translated is detected when \R is built and theinformation used by \code{.Fortran}), but portable code should not useFortran names containing underscores.Use \code{.Fortran} with care for compiled Fortran 9x code: it may notwork if the Fortran 9x compiler used differs from the Fortran 77 compilerused when configuring \R, especially if the subroutine name is notlower-case or includes an underscore. It is also possible to use\code{.C} and do any necessary symbol-name translation yourself.}\section{Copying of arguments}{If \code{DUP = TRUE} there are up to two copies made of each argumentin \code{\dots}.Prior to \R 2.15.1 there were always two for vectors (one beforecalling the compiled code and one to collect the results), and this isstill the case for character vectors. For other atomic vectors, theargument is not copied before calling the compiled code if it is nototherwise used in the calling code (such as \code{output} in theexample above). Non-atomic-vector objects are read-only to the C codeand are never copied.This behaviour can be changed by setting\code{\link{options}(CBoundsCheck = TRUE)}. In that case raw,logical, integer, double and complex vector arguments are copied bothbefore and after calling the compiled code. The first copy made isextended at each end by guard bytes, and on return it is checked thatthese are unaltered. For \code{.C}, each element of a charactervector uses guard bytes.}\note{If one of these functions is to be used frequently, do specify\code{PACKAGE} (to confine the search to a single DLL) or pass\code{.NAME} as one of the native symbol objects. Searching forsymbols can take a long time, especially when many namespaces are loaded.You may see \code{PACKAGE = "base"} for symbols linked into \R. Donot use this in your own code: such symbols are not part of the APIand may be changed without warning.\code{PACKAGE = ""} used to be accepted (but was undocumented): it isnow an error.The way \link{pairlist}s were passed by \code{.C} prior to \R 2.15.0was not as documented. This has been corrected, but the\code{\link{.Call}} and \code{\link{.External}} interfaces are muchpreferred for passing anything other than atomic vectors.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.}\seealso{\code{\link{dyn.load}}, \code{\link{.Call}}.The \sQuote{Writing R Extensions} manual.}\keyword{programming}