[Rd] interaction with C++ code (PR#10487)
Simon Urbanek
simon.urbanek at r-project.org
Mon Dec 3 16:38:11 CET 2007
This is a bug, but not in R, it's in your program:
On Dec 3, 2007, at 9:45 AM, I.J.Wilson at ncl.ac.uk wrote:
> Full_Name: Ian Wilson
> Version: 2.6.1
> OS: linux
> Submission from: (NULL) (128.240.229.7)
>
>
> The problem is new to R2.6.?. The code works as expected in R-2.5.0.
it still does in 2.6.x - but it depends on what you expect. You fail
to terminate the strings, so you get trailing garbage - it just may
have happened that by chance the trailing memory was zero in 2.5.
> I get the
> problem with two different operating systems - an older redhat and
> new ubuntu
> and with both g++4.1 and g++3.4.
>
> I have a problem with character data that is passed back from C++
> code. A small
> example is the following C++ code and R functions.
>
> #include <sstream>
> extern "C" {
> void extracttxt( char **txt, int *nchars) {
> std::ostringstream oss;
> oss << "abcdefghij";
> const char *ltxt=oss.str().c_str();
> for (int j=0;j<*nchars;j++) *txt[j]=static_cast<char>(ltxt[j]);
As said above, you fail to terminate the string, you want to add
something like:
txt[j][1]=0;
here.
Cheers,
Simon
>
> }
> }
>
> # begin R code
> dyn.load("test.so")
>
> "testtxt" <- function() {
> nchars <- 80
> txt <- .C("extracttxt"
> ,character(nchars)
> ,as.integer(nchars))[[1]]
>
> txt
> }
> a=testtxt()
> a[1:10]
>
> # typically the results are something like
> [1] "a" "b\033\xba\bfaul\030" "cz#\b"
> [4] "d" "e" "f"
> [7] "g" "h" "i"
> [10] "j"
>
> or
> [1] "az#\b" "b" "c" "d"
>
> [5] "e" "f" "g" "h"
>
> [9] "i" "j1\xba\brese\030"
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
More information about the R-devel
mailing list