Optimization problem with strcmp

Started by yarper, December 04, 2014, 06:52:55 PM

Previous topic - Next topic

yarper


#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

frankie

#1
The problem is always the same for inlined functions (see)
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
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

yarper

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.

frankie

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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide