1

Pseudo Code:

int n;
cin>>n;
int ar[n];

Why is this not giving an error? And if this is allowed why do we need new and delete operators? The only difference I can think of is that new will allocate the memory in the heap where this will allocate the memory in the stack.

  • This is a compiler extension... it's not standard C++ – StoryTeller - Unslander Monica Apr 05 '18 at 05:12
  • you should use the stack wherever possible. use the heap only when the amount of memory you need is too much to fit on the stack or you need persistent memory that is available after the scope returns – Abdul Ahad Apr 05 '18 at 05:16
  • @AbdulAhad most of the time I would agree with you, but remember that the stack is small, tightly controlled resource with no guaranteed overflow defence and the user could specify an `n` as large as a few trillion. This is safer in dynamic storage where you can get a nice `bad_alloc` exception. – user4581301 Apr 05 '18 at 05:47

0 Answers0