Displaying 20 results from an estimated 10000 matches similar to: "Memory Protection & calling C-fun from C"
2010 Oct 27
2
must .Call C functions return SEXP?
For using R's .Call interface to C functions, all the examples I've
seen have the C function return type SEXP. Why? What does R actually
do with this return type? What happens if I *don't* return a SEXP?
Reason I ask, is I've written some R code which allocates two long
lists, and then calls a C function with .Call. My C code writes to
those two pre-allocated lists, thus, I
2004 Apr 26
1
Segfault: .Call and classes with logical slots
Hi,
the following example aiming at a class containing a logical slot
segfaults under R-1.9.0 when `gctorture(on = TRUE)' is used:
Code code (dummy.c):
#include <Rdefines.h>
SEXP foo() {
SEXP ans;
PROTECT(ans = NEW_OBJECT(MAKE_CLASS("test")));
SET_SLOT(ans, install("lgl"), allocVector(LGLSXP, 1));
LOGICAL(GET_SLOT(ans,
2005 Mar 16
1
returning NULL from .Call call
Dear R developers,
I've just encountered one "feature" of R-C extensions. If it is known, I
would be thankful for any hints why it works this way (I found the way
around, which is also mentioned here, but maybe it is not the best one).
If it is however unknown, it might be considered for a "wish list".
Consider a simple "aka" C function (the original
2009 Aug 24
1
Basic Question regarding PROTECT
Hello,
Suppose I have the function
SEXP foo(){
SEXP s;
PROTECT(s=allocVector(...))
....
UNPROTECT(1);
return(s)
}
y=foo() // foo is a recusrive call
Q: Am i correct in understanding that one does not need to write
PROTECT(y=foo()) ?(and a corresponding unprotect later on)
since it is the object that is protected , SEXP is an alias for
SEXPREC* and allocVector probably does some memory
2006 Mar 27
1
Safe to UNPROTECT() when object is assigned to a list?
Hi,
I'm troubleshooting some native code on Windows that very occationally
(and semi-randomly) crashes R. Already looked at "everything", I just
want to double check that it is safe to UNPROTECT() allocated
variables as soon as they are assigned to, say, a PROTECTed list.
>From (R v2.3.0) Section 5.7.1 "Handling the effects of garbage
collection" in "Writing R
2014 Jun 24
2
using C code to create data frame but always return as list
there is my code, expect return value is a data frame but R say it is list:
SEXP Julia_R_MD_NA_DataFrame(jl_value_t* Var)
{
SEXP ans,names,rownames;
char evalcmd[4096];
int i;
const char* dfname="DataFrameName0tmp";
jl_set_global(jl_main_module, jl_symbol(dfname), (jl_value_t*)Var);
//Get Frame cols
sprintf(evalcmd,"size(%s,2)",dfname);
jl_value_t*
2009 Jan 08
1
Callbacks seems to get GCed.
Dear list,
I am trying to implement a publish-subscribe mechanism in for an embedded
R interpreter. But somehow my registered closures seem to get collected by
the GC, even though I have protected them. I have reducted my code to the
following sample. Sorry if it is a little verbose.
The first couple of call of calls still work, but at some point one of the
callbacks (callback1 in my
2010 Apr 14
2
Why no race condition when returning UNPROTECT-ed memory from C?
Consider the C (or C++) code called from the .Call interface:
SEXP foo() {
SEXP *p = PROTECT(allocVector(REALSXP, 10));
...
UNPROTECT(1);
return p;
}
Why is there no danger that the allocated memory will be garbage
collected after the UNPROTECT, but before the return of p?
I have used code like this for some time and have never had a
problem, but I'm not sure if/why it is guaranteed
2007 Aug 03
2
How to properly finalize external pointers?
Dear R .Call() insiders,
Can someone enlighten me how to properly finalize external pointers in C code (R-2.5.1 win)? What is the relation between R_ClearExternalPtr and the finalizer set in R_RegisterCFinalizer?
I succeeded registering a finalizer that works when an R object containing an external pointer is garbage collected. However, I have some difficulties figuring out how to do that in an
2008 Jan 16
1
Pb with defineVar() example in the "Writing R Extensions" manual
Hi,
I'm wondering if this code from the "Writing R Extensions" manual
is really safe:
SEXP mkans(double x)
{
SEXP ans;
PROTECT(ans = allocVector(REALSXP, 1));
REAL(ans)[0] = x;
UNPROTECT(1);
return ans;
}
double feval(double x, SEXP f, SEXP rho)
{
defineVar(install("x"), mkans(x), rho);
2008 Mar 05
1
SEXP size management.
Hi,
Trying to decrease the size of a SEXP variable without reassigning
values individually in a loop.
So far, I've tried using Realloc, as the follow source demonstrates:
SEXP dothis() {
SEXP Rblah;
PROTECT(Rblah = NEW_INTEGER(6));
int* blah = INTEGER(Rblah);
blah[0] = 1;
blah[1] = 2;
blah[2] = 3;
Realloc(Rblah, 3, INTEGER);
UNPROTECT(1);
return(Rblah);
}
According to the
2020 Sep 11
4
Garbage collection of seemingly PROTECTed pairlist
I want to write an R function using R's C interface that takes a 2-column
matrix of increasing, non-overlapping integer intervals and returns a list
with those intervals plus some added intervals, such that there are no
gaps. For example, it should take the matrix rbind(c(5L, 6L), c(7L, 10L),
c(20L, 30L)) and return list(c(5L, 6L), c(7L, 10L), c(11L, 19L), c(20L,
30L)). Because the output is
2005 Nov 07
3
R thread safe
Dear R-dev,
I would like to accelerate my R computation by using parallel OpenMP compilers
(e.g from Pathscale) on a 2-processor AMD server and I would like to know
whether R is a tread safe library. The main kernel of the OpenMP
parallelization is a C SEXP function that performs the computational routine in
parallel with:
*******************
SEXP example(SEXP list, SEXP expr, SEXP rho)
{
2014 Mar 06
1
Create dataframe in C from table and return to R
Hi ,
I am trying to create a dataframe in C and sebd it back to R. Can anyone
point me to the part of the source code where it is doing , let me explain
the problem I am having .
--------------------------------------------------------------------
My simple implementation is like this
SEXP formDF() {
SEXP dfm ,df , dfint , dfStr,lsnm;
char *ab[3] =
2012 Sep 15
1
Question about copying arguments in C.
Hi List,
I'd imagine this is a question that has been answered before, but I
can't seem to track it down, sorry for the duplication if it has.
I am writing an interface for a C library and want to return an S4
class from the 'constructing' method. One of the slots of the argument
to be returned will be filled with one of the arguments passed to the
function. My question is about
2003 Jan 13
1
Evaluating expressions from within C code
Dear R experts!
I want to evaluate expressions from within C code. For instance,
I want this code:
/* ============ -- C code -- ============== */
#include <R.h>
#include <Rinternals.h>
SEXP foo(SEXP expr, SEXP rho)
{
SEXP x;
PROTECT(x = allocVector(REALSXP, 1));
REAL(x)[0] = 1;
UNPROTECT(1);
return
2010 Sep 10
2
[xts, quantmod] segfault probelm when I work with memcpy function
Hi,
I work with SEXP C code and with xts and quantmod packages. I try to
touch how xts internal works.
So we have R session and:
> ls()
character(0)
> getSymbols('AAPL') # quantmod package
[1] "AAPL"
> ls()
[1] "AAPL"
> str(AAPL)
An ?xts? object from 2007-01-03 to 2010-09-09 containing:
Data: num [1:929, 1:6] 86.3 84 85.8 86 86.5 ...
- attr(*,
2007 May 14
1
Best Practise
Hello,
Just a quick question on best practise. I am converting quite a bit of
legacy C code into R packages and have the following situation:
(1) Legacy object with a double* array in, all over code so don't want to
change any more than I have to.
(2) Do something like:
SEXP arrayToPassToR;
PROTECT( arrayToPassToR = allocVector( REALSXP, n ) );
for(i=0; i < n; i++) {
2004 Jun 16
1
off topic: C/C++ codes for pseudo inverse
Hi,
I am looking for C/C++ codes for computing generalized
inverse of a matrix. Can anyone help me in this
regard?
Thanks,
Mahbub.
2008 Apr 05
2
Adding a Matrix Exponentiation Operator
Hi all
I recently started to write a matrix exponentiation operator for R (by
adding a new operator definition to names.c, and adding the following code
to arrays.c). It is not finished yet, but I would like to solicit some
comments, as there are a few areas of R's internals that I am still feeling
my way around.
Firstly:
1) Would there be interest in adding a new operator %^% that performs