Pranit Kothari
Pranit Kothari

Reputation: 9859

Different behavior in VS 2008 and VS 2017 for static arrary

I have written exact same lines of code in Visual 2008 and 2017. I am getting different output.

int main()
{

 static int  arr[] = {1,2};

 int * ptr = arr;
 int val = ptr[2];

 cout<<val;
 return 0;
}

Output in Visual Studio 2017,

39029

Output in Visual Studio 2008,

0

Also, in debug, I get 0 in both the Visual Studios.

Why both the versions of Visual Studio behaving differently for the Release build?

Does making array non-static makes difference in behavior?

Upvotes: 0

Views: 59

Answers (2)

SSH
SSH

Reputation: 5

ptr[2] is not available and different compilers may show different incorrect answers.

Some compilers will show 0 and some others may show big numbers.

Upvotes: 0

Unefined behaviour is undefined. You're accessing the array out of bounds (only [0] and [1] would be valid indices), which means literally anything can happen.

Upvotes: 3

Related Questions