Search code examples
c++visual-studiooptimizationvisual-c++compilation

Comparison error with Visual Studio 16.11.27 in C++


The following code is having some very weird behavior when (not) entering in the second if block when compiling with Visual Studio 2019 16.11.27 (latest version) in Release build. Does it has any error?

The expected output is 2, but it does only occurs in Debug build.

The input should be "2 0 1".

// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
​
#include <iostream>
#include <string>
​
int main(int argc, char* argv[])
{

  if (argc!=4)
  {
    printf("Call with 2 0 1 as argument") ;
    return 1;
  } 
  // planeprev=2 cplane=0 cplane_id=1
  int ninfl = 0;
  int planeprev = std::stoi(argv[1]);
  int cplane = std::stoi(argv[2]);
  int cplane_id = std::stoi(argv[3]);
  std::cout << "PP=" << planeprev << " CP=" << cplane << " CPID=" << cplane_id << std::endl;
  if (cplane_id > planeprev && cplane_id < cplane)
  {
    ninfl = 2;
  }
  else if (cplane_id < planeprev && cplane_id > cplane)
  {
    ninfl = 2;
  }
  else if ((cplane == cplane_id && cplane > planeprev) ||
    (planeprev == cplane_id && cplane < planeprev))
  {
    ninfl = 1;
  }
  else if ((cplane == cplane_id && cplane < planeprev) ||
    (planeprev == cplane_id && cplane > planeprev))
  {
    ninfl = 1;
  }
​
  if(ninfl!=2)
  printf("ERROR OUTPUT! \n");
  else
  printf("CORRECT OUTPUT\n");
}

Solution

  • I posted in MSVC forum community at this link. It is an optimization bug.

    They did answer:

    A fix for this issue has been internally implemented and is being prepared for release. We’ll update you once it becomes available for download.