char szPassword; GenerateRandomPassword(szPassword, 16); char szText; sprintf(szText, "User: %s, ID: %d, Password: %s", szUser, int(userid++), szPassword);
Don’t increment and use a variable on the same line. We know, you’re very tricky, you saved a line. You also made sure that beginners to C++ don’t know what the result will be. Keep it simple stupid. Create the simplest most readable code possible, it makes skimming over code and debugging code much easier. Fixed length arrays are very prone to buffer overruns, in this example szPassword is probably only 8 characters long after calling GenerateRandomPassword, but szUser could be any length and could definitely overrun 255 characters. The best way to mitigate this problem is to use a real string class such as
std::string. We can also avoid using
sprintf by using a type safe string writing class,
std::ostringstream. Code using
std::ostringstream is also slightly more human readable.
userid++; const std::string sPassword = GenerateRandomPassword(16); std::ostringstream oText; oText<<"User: "<<sUser<<", ID: "<<userid<<", Password: "<<sPassword; const std::string sText = oText.str();
There, type safe, buffer overflow safe, future proof and slightly more readable, what’s not to like?