I'm trying to understand what the c++ sizeof
does when operating on an RCpp vector. As an example:
library(Rcpp)
cppFunction('int size_of(NumericVector a) {return(sizeof a);}')
size_of(1.0)
# [1] 16
this returns the value 16 for any numeric or integer vector passed to it.
As also does
cppFunction('int size_of(IntegerVector a) {return(sizeof a);}')
size_of(1)
# [1] 16
I thought that numerics in R were 8 bytes and integers 4 bytes. So what is going on here? The motivation is to use memcpy
on RCpp vectors, for which the size needs to be known.
答案 0 :(得分:3)
Everything 我们从R传递给C(++)并返回SEXP
类型 - 指针到S表达式。
因此,如果我们推广您的函数并实际让SEXP
进入,我们可以看到一些有趣的事情:
R> Rcpp::cppFunction('int size_of(SEXP a) {return(sizeof a);}')
R> size_of(1L) ## single integer -- still a pointer
[1] 8
R> size_of(1.0) ## single double -- still a pointer
[1] 8
R> size_of(seq(1:100)) ## a sequence ...
[1] 8
R> size_of(help) ## a function
[1] 8
R> size_of(globalenv) ## an environment
[1] 8
R>
简而言之,您遇到了编译时C ++类型分析运算符(sizeof
)和运行时功能,即所有内容都变为SEXP
类型。对于实际向量,您可能需要size()
或length()
成员函数等等。
答案 1 :(得分:2)
You would have to get into how NumericVector
and IntegerVector
are implemented to discover why they statically take up a certain number of bytes.
Based on your observation of the size of a "numeric" or "integer" in this context, it is likely that the value 16 accounts for any/all of the following:
Ideally, don't use memcpy
to transfer the state of one object to another, unless you are absolutely certain that it is a trivial object with only members of built-in type. If I have correctly guessed the layout of a NumericVector
, using memcpy
on it will violate its ownership semantics and thus be incorrect. There are other ways to copy R vectors.