2

In my university script it's written that we're not allowed to create a local array at runtime with size only known at runtime.

float x[size][2];
That doesn't work because declared arrays can't have runtime sizes. Try a vector:

From: C++ expected constant expression

However this code compiles under Apple LLVM 8.0.0

#include <iostream>

int main(){
     int i = 5;

    int x;
    std::cin >> x;

    int array[x];

    for(int i = 0; i<x; ++i){
        std::cout << array[i] << "\n";
    }

}

Edit: And works fine. Prints out garbage as expected.

Reminder: This program is not meant to make any sense.

Community
  • 1
  • 1
Felix Crazzolara
  • 982
  • 9
  • 24
  • 1
    It's a non-standard compiler extension. If you compile with `-pedantic-errors` it should refuse it. – Galik Nov 04 '16 at 08:39

1 Answers1

2

This feature is called "variable length arrays", and is a compiler extension; it is not part of the standard.

If you compile with -pedantic, Clang gives you this warning:

main.cpp:9:14: warning: variable length arrays are a C99 feature [-Wvla-extension]
    int array[x];

Don't use this feature if you need your code to be portable.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193