Today's snippet, courtesy of James Curran, is short. However it has two problems. Can you spot them?
Which do you spot first? The major problem which happens rarely, or the minor problem which happens every call? I actually shouldn't call the second problem minor as I always treat memory leaks as a Big Deal TM.
CString GetTempDirectory() { char* path = new char[256]; int pathLen = GetTempPath(256, path); if (pathLen > 256) { pathLen = GetTempPath(pathLen, path); } return CString(path); }
I particularly like the if statement which shows the original programmer had a problem but had no clue what it was or what to do about it.
Today we'll only have one moral because, as was pointed out, that's the only way to do morals.
Moral of the story? GSBMGI - Garage Sale Books Make Good Investments.
That book on C you almost bought at the garage sale down the block would have been a good investment.