Skip to content

[Rcpp-devel] Rcpp & static variables

2 messages · Giuseppe Milicia, Dirk Eddelbuettel

#
Guys,

I posted this on R-help and Dirk kindly pointed out that this list is a much better place for this sort of questions...

I was wondering whether anyone experimented with Rcpp and static variables. I remember reading that Rcpp is essentially stateless. That makes sense. However I just run a piece of code that contains a static STL structure, surprisingly  it seems that those static variables are preserved...

The question is, can we use static variable to reliably preserve state between Rcpp calls? If this is the case, this should be a better way to store state than by passing state variables between R and C++.

Cheers,

//Giuseppe

---- MAKO ----
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender immediately by e-mail and delete this e-mail from your system. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the Mako Group. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action with respect to the contents of this information is strictly prohibited. Finally, the recipient should check this email and any attachments for the presence of viruses. The Mako Group accepts no liability for any damage caused by any virus transmitted by this email.
For important disclosures please click <<http://www.makoglobal.com/UKRegulatoryNotice.htm>>
#
Hi Guiseppe,

Thanks for reposting here!
On 30 April 2010 at 16:07, Giuseppe Milicia wrote:
| Guys,
| 
| I posted this on R-help and Dirk kindly pointed out that this list is a much better place for this sort of questions...
| 
| I was wondering whether anyone experimented with Rcpp and static
| variables. I remember reading that Rcpp is essentially stateless. That
| makes sense. However I just run a piece of code that contains a static STL
| structure, surprisingly  it seems that those static variables are
| preserved... 

I have not used it explicitly.  

But I do use code in production that instantiates singletons etc to keep
e.g. connection handles to a backend.  I initialize this on package load --
using a nice clean hook provided by R and then hold on to it until the
package unloads, which in practice is when the R session ends.  That works
cleanly via constructor and deconstructor calls.
 
| The question is, can we use static variable to reliably preserve state
| between Rcpp calls? If this is the case, this should be a better way to
| store state than by passing state variables between R and C++. 

I don't see why not. Can you mock up an example of what you are trying to do?