Page 1 of 1

for loop missing ()s? Don't think so

Posted: Tue Dec 18, 2012 11:57 pm
by Crayon Shin Chan
Hello everybody, I've been adding a loop to a bit of code. It looks a bit like this:
  OFListConstIterator(OFString) iterator;
  for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); iterator++;)
  {
    std::cout << "STUFF" <<std::endl;
    // Read and insert pixel data
    cond = readAndInsertPixelData(inputPlug, resultDset, proposedTS);
    if (cond.bad())
    {
      delete resultDset; resultDset = NULL;
      return cond;
    }
  }


But gcc fails on the second line. My for loop looks perfectly formed. I don't see what the problem with it is. Do you see anything wrong with it?
2d.cc:105:81: error: expected ')'
for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end();iterator++;)
^
i2d.cc:105:6: note: to match this '('
for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end();iterator++;)
^
i2d.cc:105:82: error: expected expression
for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end();iterator++;)
^
i2d.cc:119:36: error: use of undeclared identifier 'srcIsLossy'
if (inputPlug->getLossyComprInfo(srcIsLossy, comprMethod).good()) //TODO)
^
i2d.cc:121:9: error: use of undeclared identifier 'srcIsLossy'
if (srcIsLossy)
^
4 errors generated.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 12:02 am
by DancinJack
You don't need a semicolon after the count + of iterator ++

edit: spelling

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 12:51 am
by mortifiedPenguin
I sometimes find that simply retyping a line of code from scratch fixes the "I can't see what I did wrong and its not compiling" problem. Or, like you just did, ask somebody else.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 1:39 am
by Buub
Right, the final semicolon is extraneous. Also, since nobody is using the pre-increment value of your incremented variable, you should use the pre-increment form (it compiles to ever so slightly faster code):

for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); ++iterator)

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 1:40 am
by ChronoReverse
Buub wrote:
Right, the final semicolon is extraneous. Also, since nobody is using the pre-increment value of your incremented variable, you should use the pre-increment form (it compiles to ever so slightly faster code):

for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); ++iterator)

Does it still do that? I thought modern compilers would be able to tell.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 9:21 am
by just brew it!
My guess is that most modern compilers will catch the pre/post increment optimization, provided you've got optimization enabled.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 10:29 am
by morphine
Dunno if compilers will catch this too, but assigning a iterator.end() to a "tmp" variable and using that in the loop comparison part might be faster as you avoid the function call.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 10:49 am
by DancinJack
The way he did it was the way I was taught in college. Depending on how many times that for loop has to run, I can't imagine we're talking about much time here right?

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 10:51 am
by morphine
DancinJack wrote:
The way he did it was the way I was taught in college. Depending on how many times that for loop has to run, I can't imagine we're talking about much time here right?

And you were taught correctly, this is merely an optimization (and only so if the compiler's not smart enough to do it already).

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 10:59 am
by DancinJack
would -o3 catch that?

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 11:08 am
by Crayon Shin Chan
It makes me look like an idiot, asking somebody else when the solution is so trivial, but I find that at least I know what I did wrong in the end.

EDIT: I knew about the pre-increment, but didn't think it would be useful at all, so I always use the post-increment to keep things simple in my head. The loop will probably run... for as many times as one needs frames in a DICOM Secondary Capture Multiframe file. At max 1000 I'm sure.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 11:15 am
by just brew it!
DancinJack wrote:
would -o3 catch that?

Probably, but I have no reason for saying so other than a hunch. (Also keep in mind that the actual semantics of -O3 may depend on the compiler and the CPU architecture...)

As far as which is more "correct" from a coding standpoint, there isn't really a right or wrong answer. IMO the only reason the post-increment syntax is considered the "normal" one is because it is fairly common to use the old value of the index for something. E.g., the following idiom is typical for clearing the first n elements of array a:
for (i = 0; i < n; a[i++] = 0);

Edit:
Crayon Shin Chan wrote:
It makes me look like an idiot, asking somebody else when the solution is so trivial, but I find that at least I know what I did wrong in the end.

No need to feel bad, trivial mistakes happen even to seasoned developers. And sometimes when you read a piece of your own code, your brain sees what you *thought* you wrote, not what you *actually* wrote; all it takes is a second pair of eyes.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 2:05 pm
by Buub
ChronoReverse wrote:
Buub wrote:
Right, the final semicolon is extraneous. Also, since nobody is using the pre-increment value of your incremented variable, you should use the pre-increment form (it compiles to ever so slightly faster code):

for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); ++iterator)

Does it still do that? I thought modern compilers would be able to tell.

In this specific case, possibly. It can depend on the context, though. It really just depends on how good the compiler is.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 2:07 pm
by Buub
DancinJack wrote:
The way he did it was the way I was taught in college. Depending on how many times that for loop has to run, I can't imagine we're talking about much time here right?

The point is it's a nearly free optimization. 99% of your loops it will not be measurable, but it also doesn't cost anything to do anywhere other than developing a habit.

You should not prematurely optimize code, especially if it comes at a cost to normal development, either in development time or more complex code. But if it's literally choosing to put symbols on one side of a variable or the other, there's no reason not to do it.

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 9:39 pm
by notfred
I find pre-increment less readable. Post-increment is the standard and preincrement makes me think that there is something special being done here. From a performance point of view, I suspect the pre-increment is one of those things that may have been faster on one compiler and processor architecture but is no win or even slower on another. Optimisation rules of thumb often become obsolete, see for example how the removal of Duff's Device from the X server made things run faster: http://lkml.indiana.edu/hypermail/linux ... /0171.html

Re: for loop missing ()s? Don't think so

Posted: Wed Dec 19, 2012 11:47 pm
by Buub
notfred wrote:
I find pre-increment less readable. Post-increment is the standard and preincrement makes me think that there is something special being done here. From a performance point of view, I suspect the pre-increment is one of those things that may have been faster on one compiler and processor architecture but is no win or even slower on another. Optimisation rules of thumb often become obsolete, see for example how the removal of Duff's Device from the X server made things run faster: http://lkml.indiana.edu/hypermail/linux ... /0171.html

Not quite. The reason pre-increment is faster is because the compiler knows that you do not need the pre-incremented value of the variable, and can omit any actions before the increment. When you post-increment, that guarantee cannot be assumed, and the compiler must (unless optimized away) emit the current value of the variable before doing the increment.

Now, no argument that modern compilers might be able to optimize to the same code, but the fact remains that it's a relatively costless habit to form and can result in measurable performance benefits in some cases. And even if the compiler can optimize the code to be identical for simple variables, it still might not be the case if the ++ operators are methods on a class object, where assumptions cannot be made ahead of time, and optimization needs to happen within the ++ operator method.

Re: for loop missing ()s? Don't think so

Posted: Thu Dec 20, 2012 8:41 am
by notfred
Ah, now you are talking C++, I was coming at this from a pure C perspective. I suspect in C the compiler finds it easier to determine that it can optimise emitting the variable away.

Re: for loop missing ()s? Don't think so

Posted: Thu Dec 20, 2012 1:09 pm
by Flying Fox
notfred wrote:
Ah, now you are talking C++, I was coming at this from a pure C perspective. I suspect in C the compiler finds it easier to determine that it can optimise emitting the variable away.

Well, the OP was using std:: so it was in C++. ;) But yes, C would be easy to make that determination with no operator overload.