[Rcpp-devel] Keep variables in C++ environment

Dirk Eddelbuettel edd at debian.org
Wed Jan 27 18:09:37 CET 2010


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