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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -