[Rcpp-devel] Zero length vectors in R

Dirk Eddelbuettel edd at debian.org
Wed Jun 10 13:52:21 CEST 2020


On 10 June 2020 at 01:21, Leonardo Silvestri wrote:
| The discussion you mention is most likely relevant here - other people 
| more versed than me in valgrind and R's memory allocation could probably 
| attest to that.
| 
| Another idea to detect these errors would be to create a modified Rcpp 
| package where the C++ subscript operator [] is modified to check for 
| size and report an incorrect dereferencing.

There is a very cute and less-than-a-dozen-lines class Vec in Stroustrup's "A
Tour of C++" (2nd ed, 2018, section 11.2). The std::Vec inherits "everything"
from std::vector but then redefines 'T& operator[](int i)' and 'const R&
operator[](int i) const' to use .at(i) and provide range checking.

The discussion there is brief, but good. Range checking introduces about 10%
overhead. Quoting: "However, experience shows that such overhead can lead
people to prefer the far more unsafe builtin arrays.". Well put. He also
notes that "some implementations" offer this via a compile-time option.

Seems like an opportunity for someone to get famous and add a #define to Rcpp
to support this too.  Well written PRs, ideally supported by more than merely
superficial testing, are always welcome.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list