Difference between value and reference args in Rcpp [duplicate]

a 夏天 提交于 2019-12-05 18:51:43

First, both your functions return a NumericVector that is not being assigned to any variable, and therefore is not being used. The code below is equivalent to what you have, as you are discarding the returned NumericVector anyhow.

cppFunction("void func1(NumericVector& x)
            {
            for (int i = 0; i < x.length(); i++)
            x[i] = x[i] * 2;
            }")


cppFunction("void func2(NumericVector x)  // no &
            {
            for (int i = 0; i < x.length(); i++)
            x[i] = x[i] * 2;
            }")

x <- 1:10/5
func1(x)
print(x)

x <- 1:10/5
func2(x)
print(x)

Second, a NumericVector behaves as a pointer in the C++ functions. The pointer gives you the address where the values are stored, and to be able to change the values at that address, you only need to know the address, but you don't need the ability to modify the address itself. Therefore, there is no difference in passing the pointer by value or passing it by reference.

This thread contains useful knowledge on the behavior of NumericVector:

Should I prefer Rcpp::NumericVector over std::vector?

The program below demonstrates the same behavior in C++.

#include <iostream>

void func1(double* a) // The pointer is passed by value.
{
    for (int i=0; i<3; ++i)
        a[i] *= 2;
}

void func2(double*& a) // The pointer is passed by reference.
{
    for (int i=0; i<3; ++i)
        a[i] *= 2;
}

void print(double* a)
{
    std::cout << "Start print:" << std::endl;
    for (int i=0; i<3; ++i)
        std::cout << a[i] << std::endl;
}

int main()
{
    double* x = new double[3];

    // Set the values with 1, 2, and 3.
    for (int i = 0; i<3; ++i)
        x[i] = i+1;

    print(x);
    func1(x);
    print(x);

    // Reset the values with 1, 2, and 3.
    for (int i = 0; i<3; ++i)
        x[i] = i+1;

    // This block shows the same behavior as the block above.
    print(x);
    func2(x);
    print(x);

    delete[] x;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!