<div dir="ltr">I would strongly lean towards std::string if possible.<div><br></div><div>I'm not sure how a realloc on the heap is any less safe than a std::string and strings can have memory reserved at construction time (or ::reserve()).  Or in crazy cases, a custom allocator can be used that has a preallocated pool of memory available for when the system is OOM. </div><div><br></div><div>A related question... any idea what the '0' char is for?  It looks like you introduced the zero char after \n in <a href="https://trac.osgeo.org/gdal/changeset/14601">https://trac.osgeo.org/gdal/changeset/14601</a>  and I don't see '0' in <a href="https://trac.osgeo.org/gdal/browser/trunk/gdal/port/cpl_error.cpp?annotate=blame&rev=14600">https://trac.osgeo.org/gdal/browser/trunk/gdal/port/cpl_error.cpp?annotate=blame&rev=14600</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 16, 2017 at 11:01 AM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@spatialys.com" target="_blank">even.rouault@spatialys.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u>
<div style="font-family:'Sans Serif';font-size:9pt;font-weight:400;font-style:normal"><span class="">
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">On mardi 16 mai 2017 10:26:09 CEST Kurt Schwehr wrote:</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">> I now know this is referred to as the "struct hack."</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">> </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">> This really is undefined behavior in the C++11 standard...  </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> </p>
</span><p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">Well, I don't think the hack is really needed and char szLastErrorMsg[] could be replaces by a char* pszLastErrorMsg that would be malloc'ated.</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">The current situation comes from the fact that originally no custom free function could be associated with a TLS member, but now that we have a</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">CPLSetTLSWithFreeFunc() we could use it to free the pszLastErrorMsg member before freeing the context.</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">(or use std::string if you prefer, but this code portion can be run under very low memory conditions, so great care is needed)</p><span class="">
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">-- </p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">Spatialys - Geospatial professional services</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><a href="http://www.spatialys.com" target="_blank">http://www.spatialys.com</a></p></span></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--<div><a href="http://schwehr.org" target="_blank">http://schwehr.org</a></div></div>
</div>