What is the benefit of allocating memory at run time

is it that we can take input of size of array at run time…?

and if so how does the below code work

cin>>n;
int arr[n];

work if this creates array before run time… because n is inputted at run time only,

arrays are always created at run time.
we need dynamic allocation when we don’t know size of array in advance.
so there are 2 ways:
1.int arr[n]
2.int* arr = new int[n];

you can mostly use first way , but second way can be useful in some cases where you have limited memory.
if you have created multiple arrays in a program out of need. and your memory is full but you want to create more arrays. you realized that some of the arrays are not needed any more, and you can delete them from memory. it is only possible in 2nd way using delete keyword.
This is much more useful for language developers as they need everything space and time efficient.
May be you can always manage with first way.

this is comparison for creating arrays. But new has variety of uses in cpp, you will learn about other uses of new in classes and objects concept of cpp.
thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.