int d;
cin >> d;
int asdf[d];
Is this considered dynamic memory allocation? According to http://www.cplusplus.com/doc/tutorial/dynamic/, it seems like I shouldn’t even be able to compile this, because arrays without using the new operator can only be declared with constant size, maybe I read it wrong. (I’m using CodeBlocks with GNU CCC compiler)
What is the difference between that and
int d;
int *asdf;
cin >> d;
asdf = new int[d];
Is the only difference that array created using new lives until it is deleted, whereas the first array becomes freed as soon as you leave the scope?
int x[n];is a variable-length array (VLA), which has automatic storage duration (i.e. “on the stack”). It is not valid in C89 or C++98/03, but it was introduced in C99 and is commonly available as an extension, e.g. in GCC.The functionality can also be mimicked on some platforms with the non-standard
alloca()function, which was used before VLAs became common.