[Rcpp-devel] Rcpp & static variables

Giuseppe Milicia Giuseppe.Milicia at makoglobal.com
Thu May 6 13:12:23 CEST 2010


Unfortunately the pieces of code that use static variables are lost in the depth of my libraries.

I used to mix R and Java using rJava, when you do that you can keep object references in R rather easily, that is not immediately possible with Rcpp. Somehow I didn't think about static variables (after all we just have a shared library loaded by the R process), so now I'm using static variables to make my system stateful, it seems to work rather nicely :)

In a Rcpp document I read (can't find it anymore, perhaps an old vignette?) there was a paragraph about Rcpp being stateless, perhaps the vignette could mention that you can use static variables to keep state around? It's obvious on hindsight, but somehow I didn't connect the dots when I started to using Rcpp...



From: Dirk Eddelbuettel [edd at debian.org]
Sent: 30 April 2010 18:14
To: Giuseppe Milicia
Cc: rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Rcpp & static variables

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?

  Regards, Dirk

---- 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>>

More information about the Rcpp-devel mailing list