From saptarshi.guha at gmail.com  Mon Mar 30 04:28:12 2009
From: saptarshi.guha at gmail.com (Saptarshi Guha)
Date: Sun, 29 Mar 2009 22:28:12 -0400
Subject: [Rd] Setting the names attribute of a list?
In-Reply-To: <6ph8wmnrcnm.fsf@gopher4.fhcrc.org>
References: <1e7471d50903291845n1d37356i57ec2124ebc5047e@mail.gmail.com>
	<6ph8wmnrcnm.fsf@gopher4.fhcrc.org>
Message-ID: <1e7471d50903291928x3164f1a3yf10b180daee5fb6d@mail.gmail.com>

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
>



[Rd] Setting the names attribute of a list?
Saptarshi Guha