for code:-
int main(){
int n;
cin>>n;
int arr[n];
}
this is also a kind of DMA (DYNAMIC MEMORY ALLOCATION) as variable n is given at run time… Why do we explicitly need new operator if compiler allocates memory at run time just like above mentioned code?
Dynamic memory allocation
Hi Mohit, there is a difference between declaring a normal array and allocating a block of memory using new. Normal arrays are deallocated by compiler (when function returns or completes). However, dynamically allocated arrays always remain there until either they are deallocated by programmer or program terminates.The most important use of new operator is flexibility provided to programmers. We are free to allocate and deallocate memory whenever we need and whenever we don’t need anymore.
Hey Mohit, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required. And please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.