From saptarshi.guha at gmail.com  Mon Aug 24 15:10:13 2009
From: saptarshi.guha at gmail.com (Sapsi)
Date: Mon, 24 Aug 2009 09:10:13 -0400
Subject: [Rd] Basic Question regarding PROTECT
In-Reply-To: <4A928511.2040607@stats.uwo.ca>
References: <1e7471d50908232052y1f4ae6e5p57ad8c9f213ef611@mail.gmail.com>
	<4A928511.2040607@stats.uwo.ca>
Message-ID: <A094ED1F-4CF6-46CD-8972-22D57C65D0D2@gmail.com>

Hello
Thank you for the response. So if my call is

y=foo()
z=malloc ( by memory allocations , do you mean via R_alloc and  
allocVector and malloc or just the former two)
Other statements

Then I need  to protect y. And in my case I don't return to R since I  
have embedded it.

Why is this the case I.e if I perform mem allocs , I need to protect y
On Aug 24, 2009, at 8:18 AM, Duncan Murdoch <murdoch at stats.uwo.ca>   
wrote:r
C
> On 8/23/2009 11:52 PM, Saptarshi Guha wrote:
>> 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 allocation which
>> does not get freed
>> when foo returns.
>
> Whether y needs protecting depends on what happens between the y =  
> foo() call and the time you return to R.  If nothing happens, i.e.  
> you just return y to R, then you're safe.  If you do any memory  
> allocations after that call before returning to R then y will need  
> to be protected.
>
> Duncan Murdoch



[Rd] Basic Question regarding PROTECT
Sapsi