we can read “n” from user and then create the array.
like ,
int n;
cin>>n;
int a[n];
this is a way and it does nat waste , then why to use dma ??
But in cpp,we can read n from user and then create the array
hi rahul
this is not the right way to create array of size n
the memory use in this type is static so it may or may not reserve n memory space
but when u do dma then it reserve memory from heap that is larger than the static therefore it surely reserve n that is run time allocation
how you can say it reserves memory statically , we are providing “n” a runtime only. so obviously it is done dynamically
@Rahulkr102 Hey Rahul,
int n;
cin >> n;
int array[n];
Note that this will work only with the newer compilers. According to C++ standard, Variable length arrays are not allowed.
This is not the standard way or the correct way to create an array of size n. It seems dynamic to you as you are giving the value of n but actually it is not. In Dynamic memory allocation, some amount of space will be reserved in the heap memory for the array but here it is not happening.It is just using the stack memory area. By using keyword ‘new’ you actually reserve some space in the heap memory.
So this is the correct and the standard way:
int n;
cin >> n;
int *array = new int[n];
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.