Are arrays not dynamically allocated in C++? -
this question has answer here:
- vlas when compiling c++ source g++ 1 answer
i read tutorial dynamic memory in c++ , states follows:
...the size of regular array needs constant expression, , size has determined @ moment of designing program, before run...
however, ran program test this:
#include <cstdlib> #include <iostream> using namespace std; int main() { int y; cout << "enter number of elements of array: "; cin >> y; int x[y]; // declared array using variable size instead of constant expression x[y-1] = 3; cout << x[y-1]; return 0; }
...and there no errors. statement made tutorial incorrect or misinterpreting it?
no, they're not.
what you're seeing here a gnu extension called "variable length arrays" (which still stack-based).
the quotation correct in context of actual c++ language itself.
if used proper compilation flags (-wall -wextra -pedantic
) your compiler tell this.
Comments
Post a Comment