Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development version broken for STRICT_R_HEADERS #612

Closed
jeroen opened this issue Dec 14, 2016 · 11 comments
Closed

Development version broken for STRICT_R_HEADERS #612

jeroen opened this issue Dec 14, 2016 · 11 comments

Comments

@jeroen
Copy link
Contributor

jeroen commented Dec 14, 2016

For some packages we need to #define STRICT_R_HEADERS because macros like Calloc, Free and ERROR conflict with identically named macros in other libraries.

This is working fine in Rcpp from CRAN, however in the development version of Rcpp I now get errors on Windows. It looks like a recent change in sugar/functions/sample.h has started using S-style Calloc and Free instead of stdlib calloc and free.

Is this intentional? It will break many packages that use <Rcpp.h> in conjunction with STRICT_R_HEADERS.

devtools::install_github("RcppCore/Rcpp")
devtools::install_github("ropensci/tesseract")
@eddelbuettel
Copy link
Member

It is an oversight by our newest team member. We'll fix. Thanks for the heads up.

@nathan-russell
Copy link
Contributor

@jeroenooms Apologies, I was trying to be consistent with the implementation in R's random.c file, but was unaware of this. Wrapping those sections with the appropriate macro guards in sample.h, e.g.

#ifdef STRICT_R_HEADERS
        HL = static_cast<int*>(R_Calloc(n, int));
        q = static_cast<double*>(R_Calloc(n, double));
#else
        HL = static_cast<int*>(Calloc(n, int));
        q = static_cast<double*>(Calloc(n, double));
#endif

seemed to work, as I was able to compile tesseract successfully. Will this be sufficient to address the problem?

@jeroen
Copy link
Contributor Author

jeroen commented Dec 14, 2016

Why can't you use stdlib malloc?

@eddelbuettel
Copy link
Member

eddelbuettel commented Dec 14, 2016

Because it is data that may go back to R.

But do a recursive grep (or my fav, ag from the silversearcher) for alloc. We do use Rf_allocVector a lot. Why not use that? The #ifdef above is ... weird. We're not forced to translate the code from R base line by line.

@eddelbuettel
Copy link
Member

My code reviews skills clearly need honing too.

@jeroen
Copy link
Contributor Author

jeroen commented Dec 14, 2016

I have no idea what you're trying to do in sample.h but it looks like this code could benefit from some rcpp-ification. Much code in base-r is old messy C code that nobody wants to touch because it will break stuff. The nice thing about Rcpp code is that it is more elegant and maintainable.

@nathan-russell
Copy link
Contributor

@jeroenooms Again, trying to follow the conventions used in the R source, but I also got the impression from Writing R Extensions that these function were preferred to their stdlib counterparts:

[...] providing analogues of calloc, realloc and free. If there is an error during allocation it is handled by R, so if these routines return the memory has been successfully allocated or freed.

However, if my interpretation of that passage is incorrect and you are confident that calloc, free are sufficient then I will use those instead.

@eddelbuettel
Copy link
Member

Hm, I could swear I had uses of Rf_Calloc in code of mine but I don't find it now.

To me either explicit prefix with Rf_ offered by R works, or what we did in the sugar random functions and do it ourselves via R::....

@eddelbuettel
Copy link
Member

Of course if it is really just temp storage then either malloc() / free() or some C++ containers are preferable.

@nathan-russell
Copy link
Contributor

@eddelbuettel Understood; I'll make these changes.

@eddelbuettel
Copy link
Member

@jeroenooms Just FYI this all goes back to the earlier and useful variant in RcppArmadillo which also stayed "close" to how R does things to offer a replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants