how am i able to declare an array with variable length determined at runtime in C++?-Collection of common programming errors
Please check this code out it compiles and runs absolutely fine.. The question is that when i started learning c++ (turbo c++) i never was able to declare an array of any type as ..
datatype var[variable_set_at_runtime];
and i took it for granted that this cant be possible in latest gcc compilers…but surprisingly this is possible…
So my related question is that whats the need of new operator then??
I know that new operator does a lot of things including dynamically allocating memory at runtime in heap,returning an address to that resource…etc..
What difference i feel is that my code dynamically allocates the memory on stack while new does it on heap?? is this the only difference…
i am really astonished after writing something like this as i could not do this when i started learning c++ and …to add to it i can do this for custom data types too… :-O
#include
using namespace std;
struct A
{
int a;
int b;
};
int main()
{
int a;
cin>>a;
int ch[a];
for(int i=0;i