What is the reason for a behavior change observed in a C program in VS2019 (compared to VS2017)?

schroeder 0 Reputation points
2024-12-06T20:16:08.1366667+00:00

Given is the following well defined C90/C99 program test.c (please assume C, not C++!) :

#include <stdio.h>
void* p1; 
void* p2; 
int i[2][2]={{1,2},{3,4}}; 
int main(void) { 
  p1 = (void*)&i[0]; 
  p2 = (void*)&*(int(*)[2])p1; 
  printf("%d", p1==o2); 
  return 0; 
}

Compiling this in a "x64 Native Tools Command Prompt for VS 2017" (cl.exe Version 19.16.27051 for x64) with "cl test.c" and executing the resulting program with ".\test.exe" induces the expected output "1".

In contrast, compiling this in a "x64 Native Tools Command Prompt for VS 2019" (cl.exe Version 19.29.30157 for x64) with "cl test.c" and executing the resulting program with ".\test.exe" induces the unexpected output "0".

When compiling instead with option '/Za' ("Disable Language Extensions"), also VS 2019 produces the expected output "1".

Which language extension induces this changed, unexpected behavior in VS 2019?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,449 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,894 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,411 Reputation points Microsoft External Staff
    2024-12-09T02:12:53.15+00:00

    Hi, @schroeder

    This problem also exists in VS2022. Tested with cl version 19.22, the output is 1. cl 19.23 outputs 0.

    Please report the problem to Developer Community and post link here so that we can follow up.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.