Skip to content
Back to formatted view

Raw Message

Message-ID: <1953CCCF2DA16149AF5026503DC2B19F121C59145E@exmbx2.ad.slu.se>
Date: 2010-01-28T12:44:24Z
From: Mattias Nyström
Subject: [Rcpp-devel] Keep variables in C++ environment
In-Reply-To: <19296.29521.851736.167558@ron.nulle.part>

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.