Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what exactly should PROTECT wrap on assignment?

Tags:

c

r

internals

I re-read the bit about garbage collection in writing R extensions multiple times now, and still don't understand the difference between these two usages:

SEXP var = PROTECT(allocVector(STRSXP, 100));

vs

SEXP var;
PROTECT(var = allocVector(STRSXP, 100));

So far I've had worse luck with the first one as my session sometimes crashes with it (yet I see that usage a lot in both real code and in the intro guide itself). Can someone please explain the difference between these two assignments?

edit:

After some experimentation I think I'm coming to the conclusion that there is no difference between the above two and any difference in crashing behavior I see is accidental, but would appreciate a confirmation from someone more experienced.

like image 802
eddi Avatar asked Oct 22 '13 16:10

eddi


People also ask

What is a wrap at work?

WRAP is a way of monitoring wellness, times of being less well and times when experiences are uncomfortable and distressing. It also includes details of how an individual would like others to support them at these different times.”

What does wrap stand for in mental health?

The Wellness Recovery Action Plan (WRAP®) is a SAMHSA evidenced based practice self management tool. WRAP has been widely-acclaimed by behavioral health leaders as an effective means to help people reduce the duration and frequency of distressing feelings and troubling behaviors.

What is the wrap model?

WRAP® is a wellness and recovery approach that helps people to: 1) decrease and prevent intrusive or troubling feelings and behaviors; 2) increase personal empowerment; 3) improve quality of life; and 4) achieve their own life goals and dreams.


1 Answers

It is strictly equivalent. This is the function called by PROTECT (from https://svn.r-project.org/R/trunk/src/main/memory.c)

SEXP protect(SEXP s)
{
    if (R_PPStackTop >= R_PPStackSize)
    R_signal_protect_error();
    R_PPStack[R_PPStackTop++] = CHK(s);
    return s;
}

static R_INLINE SEXP CHK(SEXP x)
{
    /* **** NULL check because of R_CurrentExpr */
    if (x != NULL && TYPEOF(x) == FREESXP)
    error("unprotected object (%p) encountered (was %s)",
          x, sexptype2char(OLDTYPE(x)));
    return x;
}
#else
#define CHK(x) x
#endif

and from.include/Rinternals.h:

#define TYPEOF(x)   ((x)->sxpinfo.type)

As you can see, the pointer argument is returned unchanged, so that

var = PROTECT(p)
PROTECT(var = p)

are equivalent

like image 186
Karl Forner Avatar answered Oct 05 '22 09:10

Karl Forner