[Rcpp-devel] simple Rcpp function produces: "AddressSanitizer: attempting free on address which was not malloc"
Dirk Eddelbuettel
edd at debian.org
Tue Jul 9 08:46:03 CEST 2013
On 9 July 2013 at 11:42, Darren Cook wrote:
| > 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.
Adding to this:
* you use 'new' to allocatate space, but DO NOT call delete -- memory leak.
In short, these days you almost never ever have a reason to use malloc/free
or new/delete. Ever. Learn more about STL vectors, and use those.
Safer. Easier. Better.
Pick one representation: arma, or Rcpp, or STL vectors. Stick with it. Maybe
develop your core computation in a standalone program on fixed data. The
connect to R.
Hth, Dirk
|
| Darren
|
| _______________________________________________
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list