Bad feeling about piece of code
I have been handled some code to update/maintain and there is this function that gets me very nervous.
Back in school, I was taught that returning a pointer to a local variable was a very bad thing to do. Here, it works, probably because the char* -> string implicit casting is made in the caller stack, so the pointer/string is still valid. But I'm getting some strange behaviours and I suspect that this function is at the core.
const string ltos(const long val)
sprintf(buffer, "%ld", val);
return (const char *)buffer;
First, what do you think about the code? And, if you think (as I do) that it is wrong, any suggestions on how to change it (without changing too much, it is a library and changing all the code that calls this function is not affordable?