[Rd] Problem using raw vectors with inline cfunction

Romain Francois romain at r-enthusiasts.com
Fri Feb 1 13:54:13 CET 2013


Hello,

That is a bug in inline indeed. I just commited a fix in r-forge.

The fix is to obviously replace this as.character by an as.raw.

Thanks for teh report.

Romain

Le 01/02/13 10:25, Karl Forner a écrit :
> 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


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible



More information about the R-devel mailing list