Based on the answers here to the question of how to format numbers with a comma, I am using the following code:
#include <locale>
#include <stringstream>
namespace
{
class comma_numpunct : public std::numpunct<char>
{
protected:
virtual char do_thousands_sep() const
{
return ',';
}
virtual std::string do_grouping() const
{
return "\03";
}
};
}
/* Convert number to string using a comma as the thousands separator. */
string thousands(const int x) {
/* custom locale to ensure thousands separator is comma */
comma_numpunct* comma_numpunct_ptr = new comma_numpunct();
std::locale comma_locale(std::locale(), comma_numpunct_ptr);
stringstream ss;
ss.imbue(comma_locale); // apply locale to stringstream
ss << x;
/* garbage collection */
delete comma_numpunct_ptr;
return ss.str();
}
GDB gives the following backtrace:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000021 in ?? ()
(gdb) bt
#0 0x0000000000000021 in ?? ()
#1 0x00007ffff7701535 in std::locale::_Impl::~_Impl() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff770166d in std::locale::~locale() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x000000000044b93e in thousands (x=358799) at ../globals_utilities.cpp:104
#4 0x000000000045650d in main (argc=1, argv=0x7fffffffdf58) at ../main.cpp:67
So, the problem is (I believe) with my attempt at freeing the new
'd memory. But I don't know how to work around this. (I can't use std::unique_ptr
because I'm not always compiling with C++11 support.)
How can I fix the segfault without leaking memory?