[Rd] Parameter changes and segfault when calling C code through .Call

Seth Falcon sfalcon at fhcrc.org
Thu Jan 4 22:31:01 CET 2007


"Michael Braun" <braunm at MIT.EDU> writes:
> Suppose I want to compute the log density of a multivariate normal
> distribution using C code and the gsl library.  My R program is:

>
> 	dyn.load("mvnorm-logpdf.so")
>
> 	x<-c(0,0,0,0,0,0)
> 	mu<-c(0,0,0,0,0,0)
> 	sig<-diag(6)
> 	print(sig)
> 	w<-.Call("R_mvnorm_logpdf",as.double(x),as.double(mu),sig, as.integer(6))
> 	print(sig) # sig has changed after .Call

Arguments sent via the .Call interface are not copied.  In almost all
cases, they should be treated as read-only data.

You can copy a given SEXP using duplicate.  Something along these
lines (untested):

   SEXP sigma_copy;
   PROTECT(sigma_copy = duplicate(sigmax));

But it seems pretty clear that sigmaView is holding a pointer to
sigmaAr which in turn points to the data in sigmax.

+ seth



More information about the R-devel mailing list