From sfalcon at fhcrc.org  Thu Jan  4 22:31:01 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Thu, 04 Jan 2007 13:31:01 -0800
Subject: [Rd] Parameter changes and segfault when calling C code through
	.Call
In-Reply-To: <20070104160936562.00000002744@braunm1> (Michael Braun's message
	of "Thu, 4 Jan 2007 16:09:36 -0500")
References: <20070104160936562.00000002744@braunm1>
Message-ID: <m27iw232ei.fsf@fhcrc.org>

"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



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