c++ - Converting double to string function - memory issues? -
i find myself having std::cout various double variables. i've made simple function convert double std::string, can use std::cout etc.
// convert double string. std::string dtos(double x) { std::stringstream s; s << x; return s.str(); }
the function seems work ok, question is: approach have (bad) memory implications ie. allocating unecessary memory, or leaving 'dangling'?
thanks guys pete
no, code ok, read comments on code:
std::string dtos(double x) { std::stringstream s; // allocates memory on stack s << x; return s.str(); // returns s.str() string value // frees allocated memory of s }
in addition, can pass double
cout
directly.
Comments
Post a Comment