From murdoch.duncan at gmail.com  Mon Mar  2 15:09:25 2015
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Mon, 02 Mar 2015 09:09:25 -0500
Subject: [Rd] R-devel does not update the C++ returned variables
In-Reply-To: <CAHyAz7QvAyL-KgKp0B6pk=RDHAQs2LHeGS48HvATqZnbuoAfww@mail.gmail.com>
References: <CAHyAz7TOELUyX2XW=8Abvuz2tU0_ZXVuyx-B84RBk7TQyM-mAA@mail.gmail.com>	<54F45105.40608@gmail.com>
	<CAHyAz7QvAyL-KgKp0B6pk=RDHAQs2LHeGS48HvATqZnbuoAfww@mail.gmail.com>
Message-ID: <54F46F15.3030901@gmail.com>

On 02/03/2015 8:46 AM, sarah manderni wrote:
> Thanks! I went through the online posts which supports the power of 
> .Call over .C. But my probably naive question is why does this work 
> for my code with R but not R-devel?

Because of the change mentioned in the NEWS file.

> And another question is related to using .Call. Based on the manual 
> page, I do not need to change the function parameters when using 
> .Call. So I can run like this:
> .Call("sppedUp", D, S, pD, pS, nrow(D), as.integer(N), length(N),
>            ssq[i], i, as.integer(B), overlaps, overlaps.P)
>
> But I am receiving the memory(?) related error:
> terminate called after throwing an instance of 'std::bad_alloc'   
> what():  std::bad_alloc
> Now that I am running the code using .Call.

Code using .Call is quite different from code using .C.  My guess would 
be that you didn't get all the details right.

I generally recommend that people use Rcpp, which hides a lot of the 
details.  It will generate your .Call calls for you, and generate the 
C++ code that receives them; you just need to think about the real 
problem, not the interface.  It has its own learning curve, but I think 
it is easier than using the low-level code that you need to work with .Call.

Duncan Murdoch
>
> Thanks.
>
> On Mon, Mar 2, 2015 at 2:01 PM, Duncan Murdoch 
> <murdoch.duncan at gmail.com <mailto:murdoch.duncan at gmail.com>> wrote:
>
>     On 02/03/2015 3:50 AM, sarah manderni wrote:
>     > Hi,
>     >
>     > Within my R code, I am using a C++ function as below:
>     >
>     >      overlaps <- matrix(0, nrow=B, ncol=length(N))
>     >         overlaps.P <- matrix(0, nrow=B, ncol=length(N))
>     >
>     >        .C("speedUp", D, S, pD, pS, nrow(D), as.integer(N),
>     length(N),
>     >            ssq[i], i, as.integer(B), overlaps, overlaps.P,
>     DUP=FALSE)
>     >
>     >
>     > the function "speedUp", is supposed to update matrices overlaps and
>     >  overlaps.P and it works with official R versions.
>     > However, using the same code in R-devel, it does not update
>     matrices and
>     > they remain all zero without returning any errors.
>
>     See the NEWS:  DUP=FALSE is now ignored.  It led to too many
>     bugs.  Use
>     the .Call interface if duplication causes problems.
>
>     Duncan Murdoch
>
>     > But, if I store the return values from C function in a variable
>     lets say
>     > "test" as follows:
>     >
>     > test <-        .C("speedUp", D, S, pD, pS, nrow(D), as.integer(N),
>     > length(N),
>     >            ssq[i], i, as.integer(B), overlaps, overlaps.P,
>     DUP=FALSE)
>     >
>     > then the corresponding element of test to matrix "overlaps"
>     > (test[["overlaps"]]) again has the updated values (correct
>     non-zero values)
>     > though the overlaps matrix itself is still empty.
>     >
>     > I mean in official R, the "overlaps" matrix is updated after
>     calling the
>     > function but not in R-devel. Also it works in both environment
>     and return
>     > correct values to variable "test" in R-devel as well.
>     > Did you face any similar problem so that R-devel does not update the
>     > variable returned by C++?
>     >
>     > Thanks for the help.
>     >
>     >       [[alternative HTML version deleted]]
>     >
>     > ______________________________________________
>     > R-devel at r-project.org <mailto:R-devel at r-project.org> mailing list
>     > https://stat.ethz.ch/mailman/listinfo/r-devel
>     >
>
>



[Rd] R-devel does not update the C++ returned variables
Duncan Murdoch