Pelles C forum

Pelles C => Bug reports => Topic started by: yarper on December 04, 2014, 06:52:55 PM

Title: Optimization problem with strcmp
Post by: yarper on December 04, 2014, 06:52:55 PM

#include <stdio.h>
#include <windows.h>

int main(int argc, char** argv)
{
  char *x = malloc(500);
  char *y = malloc(500);
  strcpy(x, "abc");
  strcpy(y, "abc");
 
  printf("strcmp(%s,%s) = %d\n", x, y, strcmp(x,y));

  memset(x, 0, 500);
  strcpy(x, "xyz");
 
  printf("strcmp(%s,%s) = %d\n", x, y, strcmp(x,y));
}


when running the above code with the optimizer OFF (through project options) I get the following output

strcmp(abc, abc) = 0
strcmp(xyz, abc) = (not zero)

when I turn it on (maximise speed) I get

strcmp(abc, abc) = 0
strcmp(xyz, abc) = 0

Can anyone explain this? I put it as a bug because generally I'd expect code to behave the same with optimisation on and off.

I'm using Version 7.00.355 Win64
Title: Re: Optimization problem with strcmp
Post by: frankie on December 04, 2014, 10:18:24 PM
The problem is always the same for inlined functions (see (http://forum.pellesc.de/index.php?topic=6357.0))
Sigh!  :-X
As workaround:

#include <stdio.h>
#include <windows.h>

#pragma function(strcmp)    // Disable inlining

int main(int argc, char** argv)
{
  char *x = malloc(500);
  char *y = malloc(500);

  ....


P.S. Current version is V8.00 RC6 ... but the bug is still there ... :-X
Title: Re: Optimization problem with strcmp
Post by: yarper on December 08, 2014, 01:01:52 PM
Thanks for the update frankie.

This is pretty serious and although I like pellesc I'm probably going to have to switch compilers now. There's no easy way to know all functions that are inlined and could be affected like this.
Title: Re: Optimization problem with strcmp
Post by: frankie on December 08, 2014, 03:45:45 PM
Quote from: yarper on December 08, 2014, 01:01:52 PM
Thanks for the update frankie.

This is pretty serious and although I like pellesc I'm probably going to have to switch compilers now. There's no easy way to know all functions that are inlined and could be affected like this.
Yes it's quite serious, but but not solutionless.
In the link I added on the previous post, that you probably have not read yet, I pointed out the solution that is to use the switch /Ob0 that disables all the inlined functions removing the problem.