2

I've been reading that arrays should have a size known at a compile time. Here is an example piece of code:

int temp = 5;
const int size = temp;
int array[size];

According to the author, the size is known at runtime and results in an error.

However, I compiled and ran the code and there was no error.

Is the author right that in the above piece of code the size of the array is known at runtime? If he is, why was I able to compile and run the code?

In my opinion the size is known at compile-time. Is there something that I don't understand?

Edit. I used the g++ compiler and no extra options: g++ main.C -o main

user2738748
  • 1,106
  • 2
  • 19
  • 36
  • Did you compile with GCC without `-pedantic`? – TartanLlama Feb 18 '16 at 09:06
  • What compiler did you use? What command line arguments did you call it with? Usually compilers extend C++ with their own features. See for example: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html – Felix Bytow Feb 18 '16 at 09:07
  • No, I compiled it with g++. And yes, without *-pedantic*. But I'm not ansking about warnings - I'm asking about errors, so why does this option matter? – user2738748 Feb 18 '16 at 09:08
  • Something that the standard doesn't allow may still be allowed by an implementation / by an extension. This is the case here: arrays with a size known at runtime is not allowed by the standard, but GCC (g++) supports it. – leemes Feb 18 '16 at 09:09
  • 2
    g++ is part of GCC (the GNU Compiler Collection). – Felix Bytow Feb 18 '16 at 09:09
  • 1
    @user2738748 That option matters because it disables extensions. GCC supports variable-length arrays as an extension, hence the lack of errors. – TartanLlama Feb 18 '16 at 09:13
  • @FelixBytow, TartanLiama, thank you for your explanation. I get it now. – user2738748 Feb 18 '16 at 09:15

2 Answers2

6

The size is not known at compile time, even though it looks obvious. temp is not a compile time constant, which is used to initialize size, making it also not one.

It works for you because GCC allows variable length arrays as a non-standard language extension.

Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
  • 1
    Could you explain to me why *temo* isn't a compile time constant? – user2738748 Feb 18 '16 at 09:10
  • temp is of type int, which is not compile-time-constant. What you would need is "static int const temp = 23;" or with C++ >= 11 you could also throw constexpr into the mix. – Felix Bytow Feb 18 '16 at 09:14
  • 1
    @FelixBytow, it may be a dumb and basic question, but I don't understand why int (the variable temp) isn't known at a compile time. Could you explain that to me? – user2738748 Feb 18 '16 at 09:18
  • 1
    It is at the moment, but the compiler wouldn't bet its credibility on the fact. – Bathsheba Feb 18 '16 at 09:19
  • 3
    The value (5) is a literal (const int), which is indeed known at compile time. The variable temp is just of type int and therefor may be modified at runtime. Any optimizations possibly turning temp into a compile-time-constant by detecting that it isn't changed, are completely implementation defined. – Felix Bytow Feb 18 '16 at 09:21
  • @FelixBytow, thank you. Now I get it. – user2738748 Feb 18 '16 at 09:21
3

temp is not known at compile time. You compiler must have a non-standard extension which permits variable length arrays.

A compiler would never assume that temp is immutable despite the fact that you clearly don't change it. (You might do one day which would then break your code horribly, and perhaps you could even conjure up a smart way of changing temp).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483