should we prefer taking a dynamic array ?
or static is also fine .
In the problem ? counting sort
In this case you can use anyone
yes
Benifits of dynamic Arrray
int *arr=new int[n]
-
avoid wastage of memory
as memory is allocated during runtime
if n is 5 it will make array of size 5 only
whereas in static array we have to provide the size of array before compilation -
dynamic array is allocated in heap memory so remains till program ending
so if you want to make array inside function and need to return back then dynamic arrays are used
But dynamic array allocation is litile bit slower than static array
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course
please elaborate the 2 point.
int * fun(){
int arr[1000];
// do some changes in arraay
return arr;
}
it will return garbage if you use this array in main() it may give segmentation fault
but if you make dynamic array there is no problem