From I.J.Wilson at ncl.ac.uk  Mon Dec  3 15:45:21 2007
From: I.J.Wilson at ncl.ac.uk (I.J.Wilson at ncl.ac.uk)
Date: Mon,  3 Dec 2007 15:45:21 +0100 (CET)
Subject: [Rd] interaction with C++ code (PR#10487)
Message-ID: <20071203144521.35ADA2834158@mail.pubhealth.ku.dk>

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.  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]);
    }
}

# 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"



[Rd] interaction with C++ code (PR#10487)
I.J.Wilson at ncl.ac.uk