[Rcpp-devel] simple Rcpp function produces: "AddressSanitizer: attempting free on address which was not malloc"

Darren Cook darren at dcook.org
Tue Jul 9 04:42:47 CEST 2013


> The C++ function, though, is quite simple -- I wonder if anyone might be
> able to spot easily what is going on?
> ...
>     double Cnm;
>     Cnm = nChoosek(*n, *m);
>     ...
>     int* combmat;
>     combmat = new int[(int)Cnm**(m+0)];

I'm not sure if this is the cause of the complaint, but in a code review
I'd have red flags all over that last line:

  * Cnm is a double, but you truncate it to an int.
    Rounding *down* to then do a memory allocation makes me nervous.
    If Cnm is only ever an integer, keep it as int and cast the return
value from nChoosek() instead.
    (You also do the same cast to int ncols later on.)

  * Can  *(m+0) be rewritten as *m ?

  * I assume it should read:   Cnm * nrows
    where nrows is *m?
    Very minor but ** also means double dereference, so I'd like at
least a space to make the intention clear.

Darren



More information about the Rcpp-devel mailing list