Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 489
Vinicius Almada Avatar asked Dec 11 '25 11:12

Vinicius Almada


1 Answers

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.

like image 53
Vinicius Almada Avatar answered Dec 13 '25 00:12

Vinicius Almada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!