- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I like that the "i" loop variable is unsigned, but "j" is not. Thats a nice touch.
Admin
Hey who knows, this is C++.
maybe the indexer has loads of sideeffects that he needed ;)
Admin
It was so because he uses pmData.scanDataList.length() which returns unsigned int. If he uses int, it will return a warning. He just tried to eliminate the warning.
Admin
Nat, how does the following create a warning?
int numMeasures = 0;
for (unsigned int i = 0; i < pmData.scanDataList.length(); i++)
{
numMeasures += pmData.scanDataList[i].numMeasures;
}
Admin
I think Nat's point was that the pmData.scanDataList.length() function returns an unsigned int, and when the compiler compared the types it saw "int < unsigned int" in the for condition, so it spit up a warning. I don't do any C++ so I thought it was an optimization or something.
Admin
Yeah, it's probably for the warning. They should all be unsigned though, really. When will a negative numMeasures ever make sense? But using an int anyway is pretty common.
Admin
I think the solution is very simple.
BEFORE there were not 2 dimensions but more.
Admin
Not to mention the guy misspelled Disco! =)
Admin
LOL @ foxyshadis!
Admin
Well, at least this code actually works. Although it is pretty silly, it's nowhere near the level of a lot of the other WTFs on here.
Admin
The warning in a signed/unsigned mismatch, which will appear on W4 under VC++ and Wall under gcc.
Using signed numerals when unsigned will do is generally a bad idea, but unfortunately common. However, if the author wanted to get rid of the warning a simple static_cast<int>() would have sufficed. boost::numeric_cast<int>() would have been better, to catch the overflow condition.
Admin
@Anon: Almost all the code we see on TheDailyWTF actually works, it's the nature of the code at which we poke fun.
@Everyone else: I think you're missing the point with the unsigned int thing. The guy put in a For loop to manually count something in increments of 1. Why not just use the solution posted by WanFactory and avoid going through a loop possibly a few thousand times...
Admin
if the guy who wrote the ISBN function teamed up with this guy, this particular piece of code might have ended up like this:
int numMeasures = 0;
for (unsigned int i = 0; i < pmData.scanDataList.length(); i++)
{
if (pmData.scanDataList[i].numMeasures > 1) numMeasures++;
if (pmData.scanDataList[i].numMeasures > 2) numMeasures++;
if (pmData.scanDataList[i].numMeasures > 3) numMeasures++;
if (pmData.scanDataList[i].numMeasures > 4) numMeasures++;
if (pmData.scanDataList[i].numMeasures > 5) numMeasures++;
if (pmData.scanDataList[i].numMeasures > 6) numMeasures++;
...
etc
...
}
Admin
But Jeff - that's would be an unrolled loop at it's finest!
probably the guy just had a code template for running through all the measures in the scan data list, and changing thetwo nested loops to one was to much bother...
Admin
The j loop is too obviously demented for comment IMO. The signed/unsigned issue remains without it. (and so would the warning, were i signed)
Admin
Re the signed unsigned. the checking of the length each time round the for loop is pretty inefficient anyway.
if he's going to keep the for loop, at least :
int numMeasures = 0;
unsigned int nLength =pmData.scanDataList.length();
for (unsigned int i = 0; i < nLength; i++)
would be better.
Actually, I didn't really look at the inner loop until just now. thats frickin funny! I'm pretty sure that its un-needed tautological redundancy.
Admin
Jeff, have you ever had your code published here? You just turned bad code that worked into bad code that doesn't work anymore!
Nice one
Admin
About using 'i' and 'j' for loops: I found an example that told me not to use them:
for (i = 0; i < num; i++, free_area += SIZE(free_area_struct)) {
...
for (j = 0; j < 3; j++) {
free_area_buf[i] = vas_endian32(free_area_buf[i]);
}
...
}
Admin
One possible explanation for this code: pmData.scanDataList[i].numMeasures might not be the same every time it is referenced (if there's something scary going on with the [] operator).
But that would be horrifying for a whole different set of reasons.
Admin
What's wrong with this code? Seems OK to me.
The purpose of the code is to check how many more measures need to be taken, as in "there are several measures we may decide to take to address this issue".
i is unsigned because length() returns unsigned. Fine.
j is signed because numMeasures is signed -- in fact, it can be negative because the number of necessary measures can be negative if you've already taken too many measures. See the UN plans for addressing farm subsidies for examples of this.
Since the code runs in a real-time multi-threaded environment and the mutex is implemented in the overloaded array operator (as per changeling's comment), the number of measures still to be taken may actually be reduced while the code is executing.
The reason for this is that the system is modeling a political process in which the answer to a question changes each time you ask it. In order to reasonably estimate the number of measures to be taken, then, you must ask as many times as there are measures outstanding.
Really, the only fair criticism is on the absence of comments for the code, but I'm sure it's all noted in the class header files. As usual in multi-threaded politics, this code is being taken out of context.
Admin
Matt -- I hope you understand I was not presenting what I felt was a good solution but a joke incorporating the "ISBN" function from the last WTF. Try reading the posts a little more carefully before you insult people.
Admin
Unless this code is the remanant of some larger code, this guy should be punched in the face.
Admin
Jeff, Matt is correct you made (bad???) code that works into worst code that doesn't work. And I don't think he was trying to insult you, he was merely joking with you.
Admin
re: C++++ 9/22/2004 9:09 AM Nick
Unless this code is the remanant of some larger code, this guy should be punched in the face.
after that, he should be punched in the Dicos.
Admin
mikey: Don't stop yet, you still need to explain why the debug code only looks at one sample value, in fact a sample value that is then thrown away.
Admin
@Josh: Sorry, my bad.
The first debug statement simply logs how many programs (referenced in the scanData list)exist for which measures may need to be taken.
The second debug statement provides an objective view of the number of possible measures before the loop evaluation spurs an introspection process that may alter that number.
Admin
Except the second debug statement potentially alters that number as well. :)
Admin
It's a Dicos Inefrno
Brun bbay brun !
Admin
ok
Admin
some time in the fun
Admin
You guys don't understand: He's allowing for multithreading. If pmData.scanDataList[i].numMeasures changes during the iteration then code can adapt to it... ;-)
Admin
maybe I'm wrong but I would change the code to:
I think this has the same effect without the nutty loop