[Rcpp-devel] Keep variables in C++ environment
Mattias Nyström
Mattias.Nystrom at srh.slu.se
Thu Jan 28 13:44:24 CET 2010
Static variables was a simple and working solution for my problem! Thanks!
-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org]
Sent: den 27 januari 2010 18:10
To: Romain Francois
Cc: Mattias Nyström; rcpp-devel at lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Keep variables in C++ environment
On 27 January 2010 at 17:35, Romain Francois wrote:
| On 01/27/2010 05:29 PM, Mattias Nyström wrote:
| > Hi,
| >
| > I'm using the Rcpp package to do some operations in C++. For example reading large files and do some calculations. I call the C++ function several times, so I would like the C++ environment keep the objects and variables in the memory when I'm going back to R. So my question; is it possible to set variables, read in files, etc. in C++ and keep them in the memory until I call the function next time?
|
| With the new api, i.e. the classes in the Rcpp namespace it is
| definitely possible. This was one of the goals.
|
| So if you do something like this :
|
| Rcpp::CharacterVector x(2) ;
| x[0] = "foo" ;
| x[1] = "bar" ;
|
| You can then put x wherever you like and use it when you come back.
|
| With the classic api, it is not possible I think. Dirk might prove me wrong.
Aren't we talking about standard static variables?
edd at ron:/tmp> cat staticVar.cpp
#include <iostream>
void foo(void) {
static int ctr;
std::cout << "Ctr now " << ++ctr << std::endl;
}
int main(void) {
foo();
foo();
foo();
}
edd at ron:/tmp> g++ -o staticVar staticVar.cpp
edd at ron:/tmp> ./staticVar
Ctr now 1
Ctr now 2
Ctr now 3
edd at ron:/tmp>
Seems to fit the bill, no?
Dirk
--
Three out of two people have difficulties with fractions.
More information about the Rcpp-devel
mailing list