[Rcpp-devel] R Session Aborted when calling C++
Gregory Jefferis
jefferis at mrc-lmb.cam.ac.uk
Thu Oct 29 07:56:38 CET 2020
Alexander,
I think your bounds checking code uses bitwise & rather than logical &&
if((neighbor_idx(0) < nr) & (neighbor_idx(1) < nc) & (neighbor_idx(0) >= 0) & (neighbor_idx(1) >= 0)){
GLCM(focal_val,neighbor_val) = GLCM(focal_val,neighbor_val)+1;
GLCM(neighbor_val,focal_val) = GLCM(neighbor_val,focal_val)+1;
}
Best,
Greg.
Sent from my iPhone
> On 29 Oct 2020, at 00:15, Dirk Eddelbuettel <edd at debian.org> wrote:
>
>
> Alexander,
>
> A segmentation fault can take the session down as it is undefined behavior,
> yet is almost always the error of the programmers.
>
> We have written the basic accessors for efficiency so they do not check. But
> there are checking accessor that throw with a proper message:
>
> R> Rcpp::cppFunction("void foo(IntegerVector x) { int n = x.size(); Rcpp::Rcout << x.at(n+1) << std::endl; }")
> R> foo(1:3)
> Error in foo(1:3) : Index out of bounds: [index=4; extent=3].
> R>
>
> No segfault, just a clean error message. RcppArmadillo has something similar.
>
> By contrast if you don't check you can get garbage or bad behavior:
>
> R> Rcpp::cppFunction("void bar(IntegerVector x) { int n = x.size(); Rcpp::Rcout << x[n+1] << std::endl; }")
> R> bar(1:3)
> 1358954573
> R>
>
> No crash but not exactly 'right' either.
>
> The other thing you can do is to couple R with gdb to run under the debugger
> to get access to your indexing variables. There are some writeups in
> different places as it takes two steps---but may be worth it. Here are two
> quick hits from StackOverflow:
>
> https://stackoverflow.com/questions/21226337/what-are-productive-ways-to-debug-rcpp-compiled-code-loaded-in-r-on-os-x-maveri
> https://stackoverflow.com/questions/11345537/debugging-line-by-line-of-rcpp-generated-dll-under-windows
>
> Dirk
>
> --
> https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
More information about the Rcpp-devel
mailing list