H
HappyHippy
Hi,
I'm wondering what you think about this piece of code:
#include<iostream>
int main()
{
int size;
std::cin >> size;
int array[size];
std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}
Is it legal or not (according to C/C++ standard)?
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure
The
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:
sizeof(arr) = size * sizeof(int)
P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand
I'm wondering what you think about this piece of code:
#include<iostream>
int main()
{
int size;
std::cin >> size;
int array[size];
std::cout<<sizeof(int)<<" "<<sizeof(array);
return 0;
}
Is it legal or not (according to C/C++ standard)?
A couple of days ago I would say it is not, only constant expression can
be used to specify the size, of the array, but now I'm not sure
reason is that gcc 3.4.4 compiles at with no errors and warnings.
And the result printed by the program is correct:
sizeof(arr) = size * sizeof(int)
P.S.
MS Visual C++ 7.1 does not compile this code. It gives the following errors:
main.cpp(7) : error C2057: expected constant expression
main.cpp(7) : error C2466: cannot allocate an array of constant size 0
main.cpp(7) : error C2133: 'array' : unknown size
main.cpp(9) : error C2070: 'int []': illegal sizeof operand