Your code will not swap x
and y
.
Note that we don't need Rcpp to swap x
and y
without copying. We can do it in straight R as the following illlustrates. x
originally has the address 0x16d9fa08 and y
originally has the address 0x170291d8 and after the swap, done in R, their addresses are exchanged and everything under them stays with the addresses originally associated with the same parent addresses, i.e. the content is at the original addresses and has not been copied.
library(pryr)
x <- data.frame(a = 1:2)
y <- data.frame(y = 3:4)
inspect(x)
## <VECSXP 0x16d9fa08>
## <INTSXP 0x1459a5b0>
## attributes:
## <LISTSXP 0x1203a7c0>
## ...snip...
inspect(y)
## <VECSXP 0x170291d8>
## <INTSXP 0x12039288>
## attributes:
## <LISTSXP 0x14894a10>
## ...snip...
tmp <- x
x <- y
y <- tmp
inspect(x)
## <VECSXP 0x170291d8>
## <INTSXP 0x12039288>
## attributes:
## <LISTSXP 0x14894a10>
## ...snip...
inspect(y)
## <VECSXP 0x16d9fa08>
## <INTSXP 0x1459a5b0>
## attributes:
## <LISTSXP 0x1203a7c0>
## ...snip...