0

If n is taken as input at runtime then how are we able to create an array with n elements at compile time? I understand that it will run perfectly fine in input[] case but am I not getting any error for input2?

I tried following code and it works fine:

int main(){
    int n;
    cin >> n;
  
    int *input = new int[n];
    for(int i=0;i<n;i++)
        input[i] = i;
    for(int i=0;i<n;i++)
        cout<<input[i]<<" ";
    
    cout<<endl;
    
    int input2[n];
    cout<<n<<endl;
    for(int i=0;i<n;i++)
        input2[i] = i;
    for(int i=0;i<n;i++)
        cout<<input2[i]<<" ";
}
sakshi singhal
  • 101
  • 2
  • 9
  • 1
    Some compilers allow the latter as an non-standard extension, but not all of them. You can configure your compiler to reject those. – HolyBlackCat Jul 25 '22 at 06:46
  • 1
    with gcc use `-pedantic`. In default settings gcc is rather lax and allows certain constructs that are not standard c++ – 463035818_is_not_an_ai Jul 25 '22 at 06:50
  • @HolyBlackCat thanks a lot for your response. Means we should create latter one only when we have fixed size n otherwise we should go for first half right? – sakshi singhal Jul 25 '22 at 06:50
  • btw it is important to understand that incorrect code does not necessarily cause a compiler error. For example you will not get a compiler error for `input[n+42] = 0;` – 463035818_is_not_an_ai Jul 25 '22 at 06:51
  • 2
    Yes. And I suggest configuring the compiler to reject the latter automatically, so you don't have to think about this. Since your compiler is probably GCC or Clang, the right flags would be something like `-std=c++20 -Wall -Wextra -pedantic-errors`. – HolyBlackCat Jul 25 '22 at 06:51
  • 1
    you should use `std::vector` and for fixed size `std::array` – 463035818_is_not_an_ai Jul 25 '22 at 06:52
  • @HolyBlackCat i wonder what is understood by "non-standard extension" in this context. Provided that C++ was considered an extension to C, and C99 supports this feature, shouldn't omitting this feature from standard be considered reduction? And if not, what is the canonical understanding for "C++ supports C programs as is", like which version of C in this case? And is it supposed to remain, say C89 until new C++ standard says otherwise? – The Dreams Wind Jul 25 '22 at 07:06
  • @TheDreamsWind *"what is understood by "non-standard extension""* Something that's illegal according to the C++ standard, but is allowed by a compiler regardless. AFAIK this is an estabilished phrase. *"C++ supports C programs as is"* This was never strictly true. Consider the implicit `void *` -> `T *` conversion, which C++ doesn't have (which is why we have to cast the result of `malloc`). *"is it supposed to remain, say C89 until new C++ standard says otherwise"* AFAIK the C++ standard doesn't reference the C standard, except for its standard library headers. – HolyBlackCat Jul 25 '22 at 07:18
  • @sakshisinghal *I tried following code and it works fine:* -- [No, it does not work fine](https://godbolt.org/z/qrb8xavjM). `input[n]` is not valid C++. Note that the compiler used is Microsoft C++, which rejects that syntax. – PaulMcKenzie Jul 25 '22 at 07:50
  • 1
    if you write standard conforming code you can be assured that it compiles and has expected behavior with any standard conforming compiler. Thats the point of having a standard in the first place. If you use non-standard extensions then what works with one compiler might not work with a different compiler. Afaik vlas are also only optionally available in C, nevertheless gcc does support them – 463035818_is_not_an_ai Jul 25 '22 at 08:13

0 Answers0