[Rd] (PR#11054) "Writing R Extensions": bad example with CAR

anders at ebi.ac.uk anders at ebi.ac.uk
Mon Mar 31 19:45:18 CEST 2008


Dear Prof Ripley,

Prof Brian Ripley wrote:
> But it is not taken 'verbatim from src/main/print.c' (at least not in 
> that version of R), and the code is not run with USE_RINTERNALS defined 
> when write-barrier checking is enabled.
> 
> The example has been updated to match the current code in 2.7.0 alpha.

I just assumed that it is copied verbatim without checking because it 
says so in the sentence above the code ("As a more comprehensive example 
of constructing an R call in C code and evaluating, consider the 
following fragment of printAttributes in src/main/print.c.")

I don't understand, however, what you mean by stating that 
USE_RINTERNALS is not needed (and I have to admit that I have no 
knowledge of this 'write-barrier checking' mechanism.)

May I ask you to have a look at the following code snippet?

--8<-- file foo.c ---
#import <R.h>
#import <Rinternals.h>

int main() {
    SEXP a, b;
    // ...
    CAR(a) = b;
}
--8<---

If I compile this with "gcc -I <path to R 2.6.2> foo.c", I get the error 
"invalid lvalue in assignment", while it compiles without error if I 
specify the compiler option "-DUSE_RINTERNALS".

Looking at the output of the preprocessor output reveals that without 
USE_RINTERNALS, the line "CAR(a)=b" stays as it is, i.e. presents the 
compiler with a function call as l-value, while with USE-RINTERNALS, it 
gets macro-expanded to "((a)->u.listsxp.carval) = b", which is now a 
valid expression.

 From this observation, I assumed that CAR is implemented as a function 
usually and as a macro only when USE_RINTERNALS is defined. Further, I 
assumed that the proper way for extensions to set car values is to write 
"SETCAR(a,b)", while R internal code, compiled with USE_RINTERNALS, may 
use "CAR(a)=b", which expands to the more efficient macro.

Now, you say that this should have nothing to do with R_USEINTERNALS, 
and that the code snippet in the manual should compile without it as well.

This leaves me confused. Should the code in the manual really compile 
without setting any compiler options? If so, what obvious point am I 
overlooking?

Best regards
   Simon Anders




+---
| Dr. Simon Anders, Dipl. Phys.
| European Bioinformatics Institute, Hinxton, Cambridgeshire, UK
| office phone +44-1223-494478, mobile phone +44-7505-841692
| preferred (permanent) e-mail: sanders at fs.tum.de



More information about the R-devel mailing list