[Rcpp-devel] Trailing Index issues, handled as pointer
John Laing
john.laing at gmail.com
Mon Jul 7 16:59:17 CEST 2014
Mario,
Your vector matched is 1-indexed, but by passing an element of this vector
(match_id) to rownames[] you're treating it as if it's 0-indexed.
-John
On Mon, Jul 7, 2014 at 10:33 AM, Mario Deng <mariodeng at googlemail.com>
wrote:
> Hello everyone,
>
> I am trying to implement a data structure using Rcpp. But I am already
> struggling with my first method.
> How things (should) work: I want to my function ("createVectorList") a
> data frame or matrix. Each column of the matrix gets sorted. This also
> holds for the rownames - storing the sorted rownames for each sort column
> gives me big memory footprint, but log(n) access while reading later.
> Basically everything is fine, but when accessing some data through a for
> loop I get "address 0x0, cause 'memory not mapped'".
>
> Here is my Code:
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List createVectorList(NumericMatrix df, CharacterVector rownames) {
> List sorted_rownames(df.ncol());
> for(int i = 0; i < df.ncol(); i++){
> NumericVector sorted_vec = df(_,i);
> std::sort(sorted_vec.begin(), sorted_vec.end());
> NumericVector one_col = df(_,i);
> df(_,i)=sorted_vec;
> IntegerVector matched = match( sorted_vec, one_col);
> CharacterVector one_sorted_row(df.nrow());
> for(int j = 0; j < one_sorted_row.length(); j++){
> int match_id=matched[j];
> String r_name=rownames[match_id];
> one_sorted_row[j]=r_name;
> }
> sorted_rownames[i]=one_sorted_row;
> }
> return Rcpp::List::create(Rcpp::Named("Matrix") = df,
> Rcpp::Named("rowIDX")=sorted_rownames);
> }
>
> There trouble is within these lines:
>
> for(int j = 0; j < one_sorted_row.length(); j++){
> int match_id=matched[j];
> String r_name=rownames[match_id];
> one_sorted_row[j]=r_name;
> }
>
> When executing this, I get the error named above.
> What I thought about: rownames should be of the type "Proxy" or so, maybe
> thats the point?
>
> Maybe one of you has a hint.
>
> With beste regards,
>
> Mario
> __
> MD
> mariodeng at googlemail.com
>
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140707/d1226cad/attachment.html>
More information about the Rcpp-devel
mailing list