[Rd] [R] How to preserve data across function calls in a library package

Saptarshi Guha sapsi at pobox.com
Tue Jul 10 20:01:18 CEST 2007


On Jul 10, 2007, at 12:33 PM, Saptarshi Guha wrote:

> Hi,
> 	Some progress: I am using
> 	SEXP retty;
> 	book=Calloc(1,int);
> 	*book=10;
> 	PROTECT(retty=R_MakeExternalPtr(book,R_NilValue,R_NilValue));
> 	
> 	then UNPROTECTING and returning retty.
>
> 	In a another function,
> 	foo(SEXP s){
> 	 int* f=(int *)R_ExternalPtrAddr(p);
> 	 Rprintf("many times %d\n",*f);	
> }
>
> 	When called do_foo(p) where do_foo calls foo and p is the pointer
> returned by the former code snippet, the Rprintf successfully prints
> the correct value but subsequently crashes
> *** caught bus error ***
> address 0x0, cause 'invalid alignment'.
>
> 	I can't figure out why... I would appreciate any advice provided.
> 	Rgds
> 	Saptarshi
>

One quick solution, change the function foo, to
SEXP foo(SEXP s){
...
return(s)
}
and make corresponding changes elsewhere. This should work.
Regards
Saptarshi










>
>
>
> On Jul 10, 2007, at 11:41 AM, Saptarshi Guha wrote:
>
>> Hi,
>> 	I am writing an R package with two functions in C++. So far
>> everything works.
>> 	Now, i would like to write a third function which would use a  
>> pointer
>> (it is a pointer to a class object) created by first function.
>> 	I tried placing this pointer outside of the function definitions
>> (i.e to make it global) but when called in the 3rd function i get
>>> 	 *** caught bus error ***
>> address 0x0, cause 'invalid alignment'"
>>
>> 	I tried Callocing it in the 1st function but to no avail. Here is a
>> quick summary. When foo is called (through do_foo, **after** having
>> called do_kNN_e) i get the aforementioned error.
>> 	Can anyone provide some pointers (no pun intended) on this?
>>
>> 	Thanks
>> 	Saptarshi
>>
>> ANN* book;
>> int* foot;
>>
>> void foo(void){
>>    Rprintf("many times\n");
>>    Rprintf("%p\n",book);
>>    Rprintf("%p\n",foot);
>> }
>>
>> SEXP
>> kNN_e(SEXP data, SEXP Nrow, SEXP Ncol,SEXP K,SEXP Eps)
>> {
>>    int nrow=asInteger(Nrow);
>>    int ncol=asInteger(Ncol);
>>    int k=asInteger(K);
>>    double eps=asReal(Eps);
>>
>>    SEXP ans,distance;
>>    SEXP retlist;
>>    PROTECT(ans=allocMatrix(INTSXP,nrow,k)); //The 2nd argument gives
>> the number of rows, and the last the number of cols see http:// 
>> cran.r-
>> project.org/doc/manuals/R-exts.html
>>    PROTECT(distance=allocMatrix(REALSXP,nrow,k));
>>    ANNpointArray datapoints;
>>    ANNpoint qpoint;
>>    ANNkd_tree* kdTree;
>>    book=Calloc(1,ANN*);
>>    foot=Calloc(1,int);
>>    book=kdTree;
>>   *foot=10;
>>
>>   .......
>> }
>>
>> extern "C" {
>>    void do_foo(void){
>>      foo();
>>    }
>>
>> SEXP
>> do_kNN_e(SEXP data, SEXP Nrow, SEXP Ncol,SEXP k,SEXP eps)
>> {
>>    return kNN_e(data,Nrow, Ncol,
>> 	     k,eps);
>>
>> }
>>
>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> Saptarshi Guha | sapsi at pobox.com | http://www.stat.purdue.edu/~sguha
> Would you people stop playing these stupid games?!?!?!!!!
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

Saptarshi Guha | sapsi at pobox.com | http://www.stat.purdue.edu/~sguha
What ever happened to happily ever after?



More information about the R-devel mailing list