• Ryan (unregistered)

    I like that the "i" loop variable is unsigned, but "j" is not. Thats a nice touch.

  • Bart (unregistered)

    Hey who knows, this is C++.
    maybe the indexer has loads of sideeffects that he needed ;)

  • Nat (unregistered)

    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.

  • WanFactory (unregistered)

    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;
    }

  • Ryan (unregistered)

    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.

  • josh (unregistered)

    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.

  • Philus (unregistered)

    I think the solution is very simple.

    BEFORE there were not 2 dimensions but more.

  • foxyshadis (unregistered)

    Not to mention the guy misspelled Disco! =)

  • Anon (unregistered)

    LOL @ foxyshadis!

  • Anon (unregistered)

    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.

  • tito (unregistered)

    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.

  • Manni (unregistered)

    @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...

  • Jeff S (unregistered)

    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
    ...
    }

  • peterchen (unregistered)

    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...

  • josh (unregistered)

    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)

  • Ian (unregistered)

    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.

  • Matt (unregistered)

    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

  • Rainer (unregistered)

    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]);
    }
    ...
    }

  • Changeling (unregistered)

    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.

  • mikey (unregistered)

    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.

  • Jeff S (unregistered)

    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.

  • Nick (unregistered)

    Unless this code is the remanant of some larger code, this guy should be punched in the face.

  • Hassan Voyeau (unregistered)

    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.

  • DCD (unregistered)

    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.

  • josh (unregistered)

    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.

  • mikey (unregistered)

    @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.

  • josh (unregistered)

    Except the second debug statement potentially alters that number as well. :)

  • KC (unregistered)

    It's a Dicos Inefrno

    Brun bbay brun !

  • NhG (unregistered) in reply to Nick

    ok

  • jari karviola (unregistered) in reply to Nick

    some time in the fun

  • Kempeth (unregistered)

    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... ;-)

  • Coward (unregistered) in reply to Kempeth

    maybe I'm wrong but I would change the code to:

    // calculate total num. of measures
         int numMeasures = 0;
         unsigned int scanDataLength;
         scanDataLength  = pmData.scanDataList.length()
         Dicos_debugInfo("scanDataList %d.", scanDataLength); 
     
         for (unsigned int i = 0; i < scanDataLength; i++)
         {
            Dicos_debugInfo("numMeasures %d.", pmData.scanDataList[i].numMeasures); 
            if(pmData.scanDataList[i].numMeasures > 0) {
               numMeasures += pmData.scanDataList[i].numMeasures;
            }
         }

    I think this has the same effect without the nutty loop

Leave a comment on “C++++”

Log In or post as a guest

Replying to comment #:

« Return to Article