regarding array declaration in c++ -
int main(){ int n; scanf("%d",&n); int a[n]; }
in above space array a[] , allocated ? in stack or heap ?
if compiler compiles that, it's going on stack. in standard parlance, if care apply construct that's not standard-conforming, has automatic storage duration, meaning don't have clean , become invalid @ end of scope.
what have there vla (variable length array), construct c allows have arrays dimensions known @ runtime. usually, way work similar "function" alloca
, decreases stack pointer amount known @ runtime , "returns" pointer it. put "function" in quotes because doing requires low-level hackery that's not provided normal function scope semantics.
vlas don't exist in c++, you're using compiler extension, , precise semantics of vlas in extension depend on compiler. since gcc, i'll leave link the relevant part of documentation.
Comments
Post a Comment