[Rd] Problem using raw vectors with inline cfunction

Karl Forner karl.forner at gmail.com
Fri Feb 1 10:25:09 CET 2013


Hello,

>From what I understood from the documentation I found, when using the
inline cfunction with convention=".C",
R raw vectors should be given as unsigned char* to the C function.

But consider the following script:

library(inline)

testRaw <- cfunction(signature(raw='raw', len='integer')
    , body='
        int l = *len;
        int i = 0;
        Rprintf("sizeof(raw[0])=%i\\n", sizeof(raw[0]));
        for (i = 0; i < l; ++i) Rprintf("%i, ", (int)raw[i]);
        for (i = 0; i < l; ++i) raw[i] = i*10;
        '
    , convention=".C", language='C', verbose=TRUE
)

tt <- as.raw(1:10)
testRaw(tt, length(tt))


When I execute it:

$ R --vanilla --quiet < work/inline_cfunction_raw_bug.R

sizeof(raw[0])=1
192, 216, 223, 0, 0, 0, 0, 0, 224, 214,
 *** caught segfault ***
address (nil), cause 'unknown'

Traceback:
 1: .Primitive(".C")(<pointer: 0x7eff8bd605c0>, raw =
as.character(raw),     len = as.integer(len))
 2: testRaw(tt, length(tt))
aborting ...
Segmentation fault (core dumped)


I was expecting to get in the C function a pointer on a byte array of
values (1,2,3,4,5,6,7,8,9,10).
Apparently that is not the case. I guess that the "raw =
as.character(raw)," printed in the traceback is responsible for the
observed behavior.

If it is expected behavior, how can I get a pointer on my array of bytes ?


Thanks.

Karl



More information about the R-devel mailing list