I’m trying to build a function that compacts the sprintf function, but somehow I ran into the following problem:
The first line after calling the class (It used to be a function, but that didn’t work either) I get the correct result: http://puu.sh/1m1Bw
But the line after I get something completely different, while I didn’t even touch the class or the variable: http://puu.sh/1m1BR
Can someone explain to me what’s happening here?
Edit: Forgot the actual class:
StringCreator::StringCreator(char* _parten, ...) {
char buff[255];
va_list args;
va_start (args, _parten);
vsprintf (buff,_parten, args);
va_end(args);
this->str = buff;
}
And in the .h file:
class StringCreator {
public:
StringCreator(char* _parten, ...);
char* str;
};
After the
StringCreator()constructor is complete the member variablethis->stris a dangling pointer as it is pointing tobuffwhich is a local variable of the constructor. Accessingthis->strafter the constructor is undefined behaviour. Changingstrfrom achar*to astd::stringis a solution and if you need access to aconst char*you can usestr.c_str(). Using astd::stringalso means the default copy constructor and assignment operator are correct.If, as indicated in the comment to this answer, you
new char[255]instead of using astd::string, then you need to either makeStringCreatornon-copyable or implement a copy constructor and assignment operator that copies the content ofstr. See What is The Rule of Three? Alternatively, you could havechar str[255];and avoid dynamic allocation and default copying ofStringCreatorwould be correct.To avoid potential buffer overrun on the call to
vsprintf()usevsnprintf()instead (if your compiler supports C99) which accepts as an argument the size of the buffer being populated and does not write more than the specified size.