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

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

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

Postposted on Tue Dec 18, 2012 11:57 pm

Hello everybody, I've been adding a loop to a bit of code. It looks a bit like this:
Code: Select all
  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.
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2246
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

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

Postposted on Wed Dec 19, 2012 12:02 am

You don't need a semicolon after the count + of iterator ++

edit: spelling
i7 860 - GA-P55-USB3 - 8GiB - HD7850 - SSD - 3.64TB HDD - Xonar D1 - U2410 - Win7 Pro x64.
DancinJack
Minister of Gerbil Affairs
 
Posts: 2050
Joined: Sat Nov 25, 2006 3:21 pm
Location: Austin, TX

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

Postposted on Wed Dec 19, 2012 12:51 am

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.
2600K @ 4.8GHz; XSPC Rasa/RX240/RX120 Phobya Xtreme 200; Asus P8Z68-V Pro; 16GB Corsair Vengeance 1333 C9; 2x7970 OC w/ Razor 7970; Force GT 120GB; 3x F3 1TB; Corsair HX750; X-Fi Titanium; Corsair Obsidian 650D; Dell 2408WFP Rev. A01; 2x Dell U2412m
mortifiedPenguin
Gerbil Elite
 
Posts: 812
Joined: Mon Oct 08, 2007 7:46 pm

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

Postposted on Wed Dec 19, 2012 1:39 am

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):

Code: Select all
for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); ++iterator)
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4204
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

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

Postposted on Wed Dec 19, 2012 1:40 am

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):

Code: Select all
for(iterator=inputfilenames.begin(); iterator!=inputfilenames.end(); ++iterator)

Does it still do that? I thought modern compilers would be able to tell.
ChronoReverse
Gerbil Elite
 
Posts: 736
Joined: Wed Dec 12, 2007 4:20 pm

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

Postposted on Wed Dec 19, 2012 9:21 am

My guess is that most modern compilers will catch the pre/post increment optimization, provided you've got optimization enabled.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37854
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

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

Postposted on Wed Dec 19, 2012 10:29 am

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.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Grand Admiral Gerbil
Silver subscriber
 
 
Posts: 10029
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

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

Postposted on Wed Dec 19, 2012 10:49 am

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?
i7 860 - GA-P55-USB3 - 8GiB - HD7850 - SSD - 3.64TB HDD - Xonar D1 - U2410 - Win7 Pro x64.
DancinJack
Minister of Gerbil Affairs
 
Posts: 2050
Joined: Sat Nov 25, 2006 3:21 pm
Location: Austin, TX

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

Postposted on Wed Dec 19, 2012 10:51 am

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).
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Grand Admiral Gerbil
Silver subscriber
 
 
Posts: 10029
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

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

Postposted on Wed Dec 19, 2012 10:59 am

would -o3 catch that?
i7 860 - GA-P55-USB3 - 8GiB - HD7850 - SSD - 3.64TB HDD - Xonar D1 - U2410 - Win7 Pro x64.
DancinJack
Minister of Gerbil Affairs
 
Posts: 2050
Joined: Sat Nov 25, 2006 3:21 pm
Location: Austin, TX

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

Postposted on Wed Dec 19, 2012 11:08 am

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.
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2246
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

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

Postposted on Wed Dec 19, 2012 11:15 am

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:
Code: Select all
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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37854
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

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

Postposted on Wed Dec 19, 2012 2:05 pm

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):

Code: Select all
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.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4204
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

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

Postposted on Wed Dec 19, 2012 2:07 pm

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.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4204
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

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

Postposted on Wed Dec 19, 2012 9:39 pm

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
notfred
Grand Gerbil Poohbah
 
Posts: 3750
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

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

Postposted on Wed Dec 19, 2012 11:47 pm

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.
Buub
Maximum Gerbil
Silver subscriber
 
 
Posts: 4204
Joined: Sat Nov 09, 2002 11:59 pm
Location: Seattle, WA

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

Postposted on Thu Dec 20, 2012 8:41 am

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.
notfred
Grand Gerbil Poohbah
 
Posts: 3750
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

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

Postposted on Thu Dec 20, 2012 1:09 pm

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.
Image
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.
Flying Fox
Gerbil God
 
Posts: 24497
Joined: Mon May 24, 2004 2:19 am


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 0 guests