NO

Author Topic: 5.00 Optimization Bug?  (Read 2703 times)

Francesco

  • Guest
5.00 Optimization Bug?
« on: June 07, 2008, 02:18:33 PM »
If I switch on optimization in the IDE (Optimizations: Maximum Speed), the following (sample-)code gives unexpected results. But it seems to work fine without optimization (Optimizations: None). System: Windows 2000 (on AMD Athlon 64 Dual Core). Pelles C 5.00.

Input (say): test
Output: tst                   <--- ???

If I replace my_getchar() with getchar() in getword() no problem shows up.

Code: [Select]
#include <stdio.h>
#include <ctype.h>

void getword(char *);

int main(void)
{
    char buffer[1000];

    getword(buffer);
    printf("%s\n", buffer);

    return 0;
}

int my_getchar(void)
{
    return getchar();
}

void getword(char *word)
{
    char *w = word;
 
    while (1)
    {
        *w = my_getchar();
       
        if (*w == '\n') break;

        w++;
    }
    *w = '\0';
   
    return;
}

« Last Edit: June 07, 2008, 04:42:19 PM by Francesco »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: 5.00 Optimization Bug?
« Reply #1 on: June 07, 2008, 08:34:45 PM »
Yes, a bug: getchar() is normally a macro, and it contains a conditional operator ( ?: ) and a fetch with a post-increment (like *p++). If you put a conditional operator and a fetch with post-increment in a *return statement*, the fetch is acting like *++p, "thanks" to a new return optimization. Pity this wasn't found during beta testing; guess I should just skip that phase in the future...

Another way to "fix" this until I can upload a new version is just to undefine the getchar macro, somewhere after #include <stdio.h>:
like so:
Code: [Select]
#include <stdio.h>
#undef getchar
/Pelle

Francesco

  • Guest
Re: 5.00 Optimization Bug?
« Reply #2 on: June 07, 2008, 11:07:49 PM »
Never mind, Pelle. I'll "test" your compiler/IDE now by (extensively) using it.  :)
Keep on the good work!

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: 5.00 Optimization Bug?
« Reply #3 on: June 08, 2008, 01:11:10 PM »
Thanks. I have this fixed now, and will upload it later - unless you find something else...  ;)
/Pelle