Does the Rcpp parameter area extend upward?

I was doing a lot of Rcpp coding recently and came across something that confuses me. I was under the impression that any given function will make copies of its "specific parameters when called, and then those variables will be destroyed after the function completes. However, I find that when I work with types List

that don't seem to be the case." In some cases I may need to change the list items in different functions, but want the list in the "higher" scope to remain unchanged Here is a very simplified example to demonstrate the problem:

test.cpp

//[[Rcpp::export]]
int list_length(List myList){

    // some sort of modification that could be needed locally
    for (int w=0; w < myList.size(); w++) {
        myList[w] = 13;
    }

    // return whatever metric
    return myList.size();
}

//' @export
//[[Rcpp::export]]
SEXP list_int(List myList){

  // only want the int output from function
  int out = list_length(myList);

  return(List::create(myList, 
                      wrap(out)));
}

      

test.r

# create simple list
myList <- as.list(seq(4))

# call Rcpp function
list_int(myList)

# output
[[1]]
[[1]][[1]]
[1] 13

[[1]][[2]]
[1] 13

[[1]][[3]]
[1] 13

[[1]][[4]]
[1] 13


[[2]]
[1] 4

      

As you can see, the list has changed in the area above the function. Did I miss something?

+3


source to share


1 answer


Yes, you miss SEXP passing pointers.



It's pretty well documented, we usually call our types "shallow proxies".

+2


source







All Articles