News:

Download Pelles C here: http://www.pellesc.se

Main Menu

Maybe a optimization bug.

Started by ander_cc, April 14, 2026, 03:38:30 PM

Previous topic - Next topic

Vortex

Hi Pelle,

This code below outputs the correct result. Tested with Pelles C v14 :

#include <stdio.h>

int main(void)
{
    int i;
    int s=0;
    for (i=1;i<11;i++)
    {
        s=s+i;
    }
    printf("Sum = %d\n",s);
    return 0;
}

Sum = 55
Optimizations : Maximize speed
Code it... That's all...

John Z


Are you sure because adding the print of i to check shows maybe not-
If i becomes corrupt but it is not looked at - is it really corrupt  ;D
Schrödinger's cat according to quantum theory, i is simultaneously good and bad until viewed
 ;)

#include <stdio.h>

int main(void)
{
    int i;
    int s=0;
    for (i=1;i<11;i++)
    {
        s=s+i;
    }
    printf("i = %d\n",i);
    printf("Sum = %d\n",s);
    return 0;
}

This also appears to be OK as long as not looking at the value of i -
#include <stdio.h>

int main(void)
{
    int i = 1;
    int s = 0;
   
    do {
        s = s + i;
        i++;
    } while (i <= 10);
    printf("i = %d\n", i);
    printf("Sum = %d\n", s);
    return 0;
}

using volatile int i has 'fixed' it every time...

John Z

John Z

Quote from: Pelle on April 18, 2026, 05:52:29 PMI my experience, enabling the optimizer on poorly written C code is more of a problem than the optimizer itself.

I agree with this also. In general I am seeing less and less issues as I program more...

John Z