NO

Author Topic: Optimization problem with strcmp  (Read 3575 times)

yarper

  • Guest
Optimization problem with strcmp
« on: December 04, 2014, 06:52:55 PM »
Code: [Select]
#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

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Optimization problem with strcmp
« Reply #1 on: December 04, 2014, 10:18:24 PM »
The problem is always the same for inlined functions (see)
Sigh!  :-X
As workaround:
Code: [Select]
#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
« Last Edit: December 04, 2014, 10:20:54 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

yarper

  • Guest
Re: Optimization problem with strcmp
« Reply #2 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.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Optimization problem with strcmp
« Reply #3 on: December 08, 2014, 03:45:45 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