[Rd] Setting the names attribute of a list?

Saptarshi Guha saptarshi.guha at gmail.com
Mon Mar 30 04:28:12 CEST 2009


Much thanks. I was protecting the pointer and not what it was pointing to.
The reason I wasn't protecting kxp,usar was because the R external
manual mentioned

5.9.1 Handling the effects of garbage collection
"Protecting an R object automatically protects all the R objects
pointed to in the corresponding SEXPREC, for example all elements of a
protected list are automatically protected."

Before protecting res in the  correct manner, I tried
a) not protecting usar, kxp
b) protecting usar,kxp
In (a), R hung, i.e nothing happened, just got stuck in a call, with
(b) I succesffuly ran it 4MM times.

Have I mis-understood the 5.9.1?

Thanks for your inputs.
Saptarshi Guha



On Sun, Mar 29, 2009 at 10:02 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:

>> // kxp and usar are two SEXP's which have not been protected
>> SEXP res
>> PROTECT(res);
>> res= allocVector(VECSXP, 2);
>
> usually you would not want to allocate when there are unprotected
> variables, so kxp, usar should be protected before this...
>
> You want to protect the result of the allocation, not the location of
> the variable, i.e.,
>
> PROTECT(res = allocVector(VECSXP, 2));
>
>> SET_VECTOR_ELT(res,0,kxp);
>> SET_VECTOR_ELT(res,1,usar);
>>
>> names=allocVector(VECSXP,2);PROTECT(names); //A
>> SET_VECTOR_ELT(names, 0, mkChar("key")); //A
>> SET_VECTOR_ELT(names, 1, mkChar("value")); //A
>> setAttrib(res, R_NamesSymbol,names); //A
>> UNPROTECT(2);
>> return(res);
>>
>>
>> Saptarshi Guha
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> --
> Martin Morgan
> Computational Biology / Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N.
> PO Box 19024 Seattle, WA 98109
>
> Location: Arnold Building M2 B169
> Phone: (206) 667-2793
>



More information about the R-devel mailing list