<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
        {mso-style-priority:99;
        mso-style-link:"Plain Text Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
span.PlainTextChar
        {mso-style-name:"Plain Text Char";
        mso-style-priority:99;
        mso-style-link:"Plain Text";
        font-family:"Calibri","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoPlainText">My $0.02.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">Dirk is right - pick something and stick with it, but avoid the raw pointers, especially if your experience is limited. It's much easier to learn the smart pointers std::shared_ptr and std::unique_ptr, or their boost equivalents, rather
 than deal with raw pointers in new code.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoNormal"><a href="http://www.codeproject.com/Articles/541067/Cplusplus11-Smart-Pointers">http://www.codeproject.com/Articles/541067/Cplusplus11-Smart-Pointers</a><o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">std::vector is your friend; learn to use it. Just allocate enough space with resize, use operator(...)[] to access/set the elements of the vector (do not use .push_back as this appends to the end of the vector) and you will not suffer
 the overhead of extending the size of the vector (some implementations copy the entire vector).<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText"><a href="http://www.codeproject.com/Articles/5378/A-Presentation-of-the-STL-Vector-Container">http://www.codeproject.com/Articles/5378/A-Presentation-of-the-STL-Vector-Container</a><o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoNormal">"After reading this article, the reader should be able to use the vector container effectively and should find no use for dynamic C-style arrays again." In fact, you should learn the Standard Template Library very well as the algorithms
 and containers it implements are highly optimized and very efficient.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">I have made good use of Armadillo, have avoided any explicit memory allocation at all, and even append columns to matrices when necessary.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">Dale Smith, Ph.D.<o:p></o:p></p>
<p class="MsoPlainText">Senior Financial Quantitative Analyst<o:p></o:p></p>
<p class="MsoPlainText">Risk & Compliance<o:p></o:p></p>
<p class="MsoPlainText">Fiserv<o:p></o:p></p>
<p class="MsoPlainText">Office: 678-375-5315<o:p></o:p></p>
<p class="MsoPlainText">www.fiserv.com<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">-----Original Message-----<br>
From: rcpp-devel-bounces@r-forge.wu-wien.ac.at [mailto:rcpp-devel-bounces@r-forge.wu-wien.ac.at] On Behalf Of Dirk Eddelbuettel<br>
Sent: Tuesday, July 09, 2013 2:46 AM<br>
To: Darren Cook<br>
Cc: rcpp-devel@r-forge.wu-wien.ac.at<br>
Subject: Re: [Rcpp-devel] simple Rcpp function produces: "AddressSanitizer: attempting free on address which was not malloc"</p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">On 9 July 2013 at 11:42, Darren Cook wrote:<o:p></o:p></p>
<p class="MsoPlainText">| > The C++ function, though, is quite simple -- I wonder if anyone
<o:p></o:p></p>
<p class="MsoPlainText">| > might be able to spot easily what is going on?<o:p></o:p></p>
<p class="MsoPlainText">| > ...<o:p></o:p></p>
<p class="MsoPlainText">| >     double Cnm;<o:p></o:p></p>
<p class="MsoPlainText">| >     Cnm = nChoosek(*n, *m);<o:p></o:p></p>
<p class="MsoPlainText">| >     ...<o:p></o:p></p>
<p class="MsoPlainText">| >     int* combmat;<o:p></o:p></p>
<p class="MsoPlainText">| >     combmat = new int[(int)Cnm**(m+0)];<o:p></o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">| I'm not sure if this is the cause of the complaint, but in a code
<o:p></o:p></p>
<p class="MsoPlainText">| review I'd have red flags all over that last line:<o:p></o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">|   * Cnm is a double, but you truncate it to an int.<o:p></o:p></p>
<p class="MsoPlainText">|     Rounding *down* to then do a memory allocation makes me nervous.<o:p></o:p></p>
<p class="MsoPlainText">|     If Cnm is only ever an integer, keep it as int and cast the return
<o:p></o:p></p>
<p class="MsoPlainText">| value from nChoosek() instead.<o:p></o:p></p>
<p class="MsoPlainText">|     (You also do the same cast to int ncols later on.)<o:p></o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">|   * Can  *(m+0) be rewritten as *m ?<o:p></o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">|   * I assume it should read:   Cnm * nrows<o:p></o:p></p>
<p class="MsoPlainText">|     where nrows is *m?<o:p></o:p></p>
<p class="MsoPlainText">|     Very minor but ** also means double dereference, so I'd like at
<o:p></o:p></p>
<p class="MsoPlainText">| least a space to make the intention clear.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">Adding to this:<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">    * you use 'new' to allocatate space, but DO NOT call delete -- memory leak.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">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.<o:p></o:p></p>
<p class="MsoPlainText">Safer. Easier. Better.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">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.<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">Hth,  Dirk<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">| Darren<o:p></o:p></p>
<p class="MsoPlainText">| <o:p></o:p></p>
<p class="MsoPlainText">| _______________________________________________<o:p></o:p></p>
<p class="MsoPlainText">| Rcpp-devel mailing list<o:p></o:p></p>
<p class="MsoPlainText">| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">
<span style="color:windowtext;text-decoration:none">Rcpp-devel@lists.r-forge.r-project.org</span></a><o:p></o:p></p>
<p class="MsoPlainText">| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve">
<span style="color:windowtext;text-decoration:none">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-deve</span></a><o:p></o:p></p>
<p class="MsoPlainText">| l<o:p></o:p></p>
<p class="MsoPlainText"><o:p> </o:p></p>
<p class="MsoPlainText">--<o:p></o:p></p>
<p class="MsoPlainText">Dirk Eddelbuettel | <a href="mailto:edd@debian.org"><span style="color:windowtext;text-decoration:none">edd@debian.org</span></a> |
<a href="http://dirk.eddelbuettel.com"><span style="color:windowtext;text-decoration:none">http://dirk.eddelbuettel.com</span></a> _______________________________________________<o:p></o:p></p>
<p class="MsoPlainText">Rcpp-devel mailing list<o:p></o:p></p>
<p class="MsoPlainText"><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org"><span style="color:windowtext;text-decoration:none">Rcpp-devel@lists.r-forge.r-project.org</span></a><o:p></o:p></p>
<p class="MsoPlainText"><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel"><span style="color:windowtext;text-decoration:none">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</span></a><o:p></o:p></p>
</div>
</body>
</html>