I'm learning now c ++, such a question: if I write
#include <iostream> using namespace std; int main(int argc, const char * argv[]) { int n; cin >> n; int a[n]; return 0; }
what size will the array be: n or 2 ^ 32-1?
And why should he be 2 ^ 32 -1? It will be exactly how much you enter. Another thing is that this is not the standard C ++ extension. In gcc this will work, I don't know about other compilers.
if i write
why don't you try and write this and see that this nonsense of yours doesn't even compile, since the size of the array should be known at the compilation stage?
ZY since I’ve been authoritatively corrected ( @KoVadim , thanks), I’ll add that if some compilers eat it (word, surprise), then to check the size of the array, you can make sizeof (a)
ZZY What a shame
Source: https://ru.stackoverflow.com/questions/191929/
All Articles
2^32 - 1,
frankly, it is not clear. - According to the standard,C++
Variable Length Arrays
prohibited (see8.3.4.1
).
- There are compilers that support them asextension'ы
, see stackoverflow.com/questions/4151254/wheres-gs-vla-extension - Accordingly, depending on the compiler you are using, you will receive either a compilation error or an array of sizen.
- Costantino Rupert:)
- Costantino Rupert