NO

Author Topic: opti bug  (Read 4792 times)

czerny

  • Guest
opti bug
« on: February 14, 2012, 04:01:15 PM »
Hallo,

this should replace '.' with ',' for all lines without the first two.

Code: [Select]
#include <stdio.h>
#define MAX 4096

int main(void)
{
char *p, line[MAX];
    int i=0;

while (fgets(line, MAX, stdin))
{
i++;
if(i>2)
{
p = line;
while (*p)
{
if (*p == '.') *p = ',';
p++;
}
}
fputs(line, stdout);
}

return 0;
}

with -Ot this works on line 3 alone.

czerny

George99

  • Guest
Re: opti bug
« Reply #1 on: February 15, 2012, 11:04:15 PM »
Hi czerny,

this is the same bug I reported some time ago:
http://forum.pellesc.de/index.php?topic=3741.0

looking at the machine code you'll see that the compiler rewrites the code to something like this:

i=3;
...
--i;
if(i==0){



CommonTater

  • Guest
Re: opti bug
« Reply #2 on: February 16, 2012, 01:24:21 AM »
Hallo,

this should replace '.' with ',' for all lines without the first two.

Code: [Select]
#include <stdio.h>
#define MAX 4096

int main(void)
{
   char *p, line[MAX];
    int i=0;

   while (fgets(line, MAX, stdin))
   {
      i++;
      if(i>2)
      {
         p = line;
         while (*p)
         {
            if (*p == '.') *p = ',';
            p++;
         }
      }
      fputs(line, stdout);
   }

   return 0;
}

with -Ot this works on line 3 alone.

czerny


Is there some reason you're not processing the first two lines ...  if (i>2) ... ?

fgets() returns a pointer to the beginning of it's buffer ... or NULL if there's an error.  I don't know the rest of your code but wouldn't it be better to use ... p = fgets(... and base your loop exit on something else?  strlen() < 2 perhaps?

Also note that fgets() will put a trailing linefeed in the buffer... which may or may not matter in this case.

I don't know if any of these things is causing your bug or not... but it might be worth some experimenting.
Also does it work if your turn Optimizations off?

George99

  • Guest
Re: opti bug
« Reply #3 on: February 16, 2012, 02:07:54 PM »
I don't know if any of these things is causing your bug or not... but it might be worth some experimenting.
Also does it work if your turn Optimizations off?

This bug does not occur if optimizations are turned off.

After all, it's just an optimization bug in Pelles C and not a penalty for poorly written code  :)

CommonTater

  • Guest
Re: opti bug
« Reply #4 on: February 16, 2012, 04:06:52 PM »
This bug does not occur if optimizations are turned off.

After all, it's just an optimization bug in Pelles C and not a penalty for poorly written code  :)

Yes, I'm aware of that George... The obvious answer is to compile with optimzations off but sometimes a little bit of rearranging can prevent it also.  I was not criticizing czerny's code...


czerny

  • Guest
Re: opti bug
« Reply #5 on: February 16, 2012, 05:49:09 PM »
Quote
Is there some reason you're not processing the first two lines ...  if (i>2) ... ?

Yes! The first 2 lines contain data which should not be processed.

CommonTater

  • Guest
Re: opti bug
« Reply #6 on: February 16, 2012, 06:26:26 PM »
Quote
Is there some reason you're not processing the first two lines ...  if (i>2) ... ?

Yes! The first 2 lines contain data which should not be processed.

Okay... sounds about right then...  I guess all that remains is to test with optimizations off.

This is a known bug that I and others have reported ... it's actually been there since version 6.0 ....
 
You will likely find that -Ot makes little difference in perfomance anyway... I did some testing and found very little value in enabling it.
 

 
« Last Edit: February 16, 2012, 06:28:07 PM by CommonTater »

czerny

  • Guest
Re: opti bug
« Reply #7 on: April 17, 2012, 09:11:28 AM »
It seems that this bug hase not gone with 7.00.2 RC1.

czerny