anders at ebi.ac.uk
2008-Mar-31 17:45 UTC
[Rd] (PR#11054) "Writing R Extensions": bad example with CAR
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
anders at ebi.ac.uk
2008-Mar-31 20:55 UTC
[Rd] (PR#11054) "Writing R Extensions": bad example with CAR
Prof Brian Ripley wrote:> I didn't say 'USE_RINTERNALS is not needed' for your code: I said it was > not used for the code that was run.I see. My original point was that the text of the "Writing R extension" manual advises against using 'USE_RINTERNALS' but then gives an example that only works when this macro is defined, without even pointing out this requirement. I merely wanted to suggest adding a little note to the manual at this point to make the matter easier for the reader. Best regards Simon Anders
Seemingly Similar Threads
- (PR#11054) "Writing R Extensions": bad example with CAR /
- manual 'Writing R Extensions': bug in example
- "Writing R Extensions": bad example with CAR / CDR as lvalues (PR#11054)
- 'merge' function: behavior w.r.t. NAs in the key column
- Possible bug: How does libR.so find RHOME?