0

From what I've learned, arrays cannot be variably sized in C++. For example,

#include <iostream>
using namespace std;

int main() {
    int size;
    cout << "How big is the array?\n";
    cin >> size;
    
    int array[size];
    array[0] = 2;
    cout << array[0] << endl;

    return 0;
}

The code above should not compile, because the size of an object in stack-based memory must be constant, and it should be known at compile-time... right? However, the code I have here compiles and runs successfully.

Why is it possible for me to set the size of an array to a non-constant variable/user input?

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
dunlop
  • 21
  • 4

0 Answers0