When I try to find the sizeof(A) where A is of type int with size as 'n', n is an int that is not defined. I get an output of 496 and when I give a value to n and then check it, sizeof(A) gives me the same values of 496. I know Array is a static data type so it will have memory irrespective of 'n' but can anyone explain me where the value 496 came from?
int main()
{
int n;
int A[n];
cout<<sizeof(A)<<"\n";
cin>>n;
cout<<sizeof(A);
return 0;
}
where A is of type int with size as 'n'
int n; int A[n];
The type of A is not "int with size as 'n'". The type of A is int[n] which is array of n integers. However, since n is not a compile time constant, the program is ill-formed. If we were to look past the ill-formedness, the value of n
is indeterminate. Reading an indeterminate value results in undefined behaviour.
anyone explain me where the value 496 came from?
It came from undefined behaviour. You can find more details by reading the assembly of the compiled program that produced that result.